blob: 4bfc0419ed96dabd5ff5c957a0539346b1339f9d [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 Moolenaaraf9aeb92013-02-13 17:35:04 +0100691#ifdef FEAT_CRYPT
692static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
693#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000694static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200695static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200699static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000702static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000703static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
707static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
709#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000710static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200711static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712#ifdef HAVE_STRFTIME
713static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
715static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200721static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200722static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000728static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200729static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000731static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000732static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000733static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000734static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000735static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000737static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200738#ifdef FEAT_FLOAT
739static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000745#ifdef FEAT_FLOAT
746static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200749static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200750static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static 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 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static 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 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000811static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static 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 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200841
842#ifdef EBCDIC
843static int compare_func_name __ARGS((const void *s1, const void *s2));
844static void sortFunctions __ARGS(());
845#endif
846
847
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000848/* Character used as separated in autoload function/variable names. */
849#define AUTOLOAD_CHAR '#'
850
Bram Moolenaar33570922005-01-25 22:26:29 +0000851/*
852 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000853 */
854 void
855eval_init()
856{
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 int i;
858 struct vimvar *p;
859
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200860 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
861 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200862 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000863 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865
866 for (i = 0; i < VV_LEN; ++i)
867 {
868 p = &vimvars[i];
869 STRCPY(p->vv_di.di_key, p->vv_name);
870 if (p->vv_flags & VV_RO)
871 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
872 else if (p->vv_flags & VV_RO_SBX)
873 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
874 else
875 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000876
877 /* add to v: scope dict, unless the value is not always available */
878 if (p->vv_type != VAR_UNKNOWN)
879 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000880 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000881 /* add to compat scope dict */
882 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000883 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000884 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200885 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886
887#ifdef EBCDIC
888 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100889 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200890 */
891 sortFunctions();
892#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000893}
894
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000895#if defined(EXITFREE) || defined(PROTO)
896 void
897eval_clear()
898{
899 int i;
900 struct vimvar *p;
901
902 for (i = 0; i < VV_LEN; ++i)
903 {
904 p = &vimvars[i];
905 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000907 vim_free(p->vv_str);
908 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000909 }
910 else if (p->vv_di.di_tv.v_type == VAR_LIST)
911 {
912 list_unref(p->vv_list);
913 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915 }
916 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000917 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918 hash_clear(&compat_hashtab);
919
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100921# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200922 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100923# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924
925 /* global variables */
926 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000927
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000928 /* autoloaded script names */
929 ga_clear_strings(&ga_loaded);
930
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 /* script-local variables */
932 for (i = 1; i <= ga_scripts.ga_len; ++i)
933 {
934 vars_clear(&SCRIPT_VARS(i));
935 vim_free(SCRIPT_SV(i));
936 }
937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1161 FALSE, FALSE, FALSE, FNE_CHECK_START);
1162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001864 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001867 /*
1868 * ":let" without "=": list variables
1869 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 if (*arg == '[')
1871 EMSG(_(e_invarg));
1872 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_glob_vars(&first);
1879 list_buf_vars(&first);
1880 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001881#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001883#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001884 list_script_vars(&first);
1885 list_func_vars(&first);
1886 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 eap->nextcmd = check_nextcmd(arg);
1889 }
1890 else
1891 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 op[0] = '=';
1893 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001894 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 {
1896 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1897 op[0] = expr[-1]; /* +=, -= or .= */
1898 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (eap->skip)
1902 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 {
1906 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --emsg_skip;
1909 }
1910 else if (i != FAIL)
1911 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917}
1918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919/*
1920 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1921 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * When "nextchars" is not NULL it points to a string with characters that
1923 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1924 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 * Returns OK or FAIL;
1926 */
1927 static int
1928ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1929 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 int copy; /* copy values from "tv", don't move */
1932 int semicolon; /* from skip_var_list() */
1933 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935{
1936 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 listitem_T *item;
1940 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941
1942 if (*arg != '[')
1943 {
1944 /*
1945 * ":let var = expr" or ":for var in list"
1946 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 return OK;
1950 }
1951
1952 /*
1953 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1954 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001955 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 {
1957 EMSG(_(e_listreq));
1958 return FAIL;
1959 }
1960
1961 i = list_len(l);
1962 if (semicolon == 0 && var_count < i)
1963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001964 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 return FAIL;
1966 }
1967 if (var_count - semicolon > i)
1968 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001969 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 }
1972
1973 item = l->lv_first;
1974 while (*arg != ']')
1975 {
1976 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 item = item->li_next;
1979 if (arg == NULL)
1980 return FAIL;
1981
1982 arg = skipwhite(arg);
1983 if (*arg == ';')
1984 {
1985 /* Put the rest of the list (may be empty) in the var after ';'.
1986 * Create a new list for this. */
1987 l = list_alloc();
1988 if (l == NULL)
1989 return FAIL;
1990 while (item != NULL)
1991 {
1992 list_append_tv(l, &item->li_tv);
1993 item = item->li_next;
1994 }
1995
1996 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001997 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 ltv.vval.v_list = l;
1999 l->lv_refcount = 1;
2000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2002 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 clear_tv(&ltv);
2004 if (arg == NULL)
2005 return FAIL;
2006 break;
2007 }
2008 else if (*arg != ',' && *arg != ']')
2009 {
2010 EMSG2(_(e_intern2), "ex_let_vars()");
2011 return FAIL;
2012 }
2013 }
2014
2015 return OK;
2016}
2017
2018/*
2019 * Skip over assignable variable "var" or list of variables "[var, var]".
2020 * Used for ":let varvar = expr" and ":for varvar in expr".
2021 * For "[var, var]" increment "*var_count" for each variable.
2022 * for "[var, var; var]" set "semicolon".
2023 * Return NULL for an error.
2024 */
2025 static char_u *
2026skip_var_list(arg, var_count, semicolon)
2027 char_u *arg;
2028 int *var_count;
2029 int *semicolon;
2030{
2031 char_u *p, *s;
2032
2033 if (*arg == '[')
2034 {
2035 /* "[var, var]": find the matching ']'. */
2036 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002037 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 {
2039 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2040 s = skip_var_one(p);
2041 if (s == p)
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 ++*var_count;
2047
2048 p = skipwhite(s);
2049 if (*p == ']')
2050 break;
2051 else if (*p == ';')
2052 {
2053 if (*semicolon == 1)
2054 {
2055 EMSG(_("Double ; in list of variables"));
2056 return NULL;
2057 }
2058 *semicolon = 1;
2059 }
2060 else if (*p != ',')
2061 {
2062 EMSG2(_(e_invarg2), p);
2063 return NULL;
2064 }
2065 }
2066 return p + 1;
2067 }
2068 else
2069 return skip_var_one(arg);
2070}
2071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002073 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 static char_u *
2077skip_var_one(arg)
2078 char_u *arg;
2079{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002080 if (*arg == '@' && arg[1] != NUL)
2081 return arg + 2;
2082 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2083 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084}
2085
Bram Moolenaara7043832005-01-21 11:56:39 +00002086/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 * List variables for hashtab "ht" with prefix "prefix".
2088 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 hashitem_T *hi;
2098 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 int todo;
2100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002101 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 di = HI2DI(hi);
2108 if (empty || di->di_tv.v_type != VAR_STRING
2109 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 }
2112 }
2113}
2114
2115/*
2116 * List global variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_glob_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002123}
2124
2125/*
2126 * List buffer variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_buf_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 char_u numbuf[NUMBUFLEN];
2133
Bram Moolenaar429fa852013-04-15 12:27:36 +02002134 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136
2137 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2139 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140}
2141
2142/*
2143 * List window variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_win_vars(first)
2147 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002149 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151}
2152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153#ifdef FEAT_WINDOWS
2154/*
2155 * List tab page variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_tab_vars(first)
2159 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002160{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002161 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163}
2164#endif
2165
Bram Moolenaara7043832005-01-21 11:56:39 +00002166/*
2167 * List Vim variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_vim_vars(first)
2171 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174}
2175
2176/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002177 * List script-local variables, if there is a script.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_script_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2185 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
2189 * List function variables, if there is a function.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_func_vars(first)
2193 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194{
2195 if (current_funccal != NULL)
2196 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198}
2199
2200/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 * List variables in "arg".
2202 */
2203 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 exarg_T *eap;
2206 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
2209 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u *name_start;
2213 char_u *arg_subsc;
2214 char_u *tofree;
2215 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216
2217 while (!ends_excmd(*arg) && !got_int)
2218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002221 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2223 {
2224 emsg_severe = TRUE;
2225 EMSG(_(e_trailing));
2226 break;
2227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 /* get_name_len() takes care of expanding curly braces */
2232 name_start = name = arg;
2233 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2234 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* This is mainly to keep test 49 working: when expanding
2237 * curly braces fails overrule the exception error message. */
2238 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 emsg_severe = TRUE;
2241 EMSG2(_(e_invarg2), arg);
2242 break;
2243 }
2244 error = TRUE;
2245 }
2246 else
2247 {
2248 if (tofree != NULL)
2249 name = tofree;
2250 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* handle d.key, l[idx], f(expr) */
2255 arg_subsc = arg;
2256 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'g': list_glob_vars(first); break;
2265 case 'b': list_buf_vars(first); break;
2266 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002268 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 case 'v': list_vim_vars(first); break;
2271 case 's': list_script_vars(first); break;
2272 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 default:
2274 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
2277 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 {
2279 char_u numbuf[NUMBUFLEN];
2280 char_u *tf;
2281 int c;
2282 char_u *s;
2283
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002284 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 c = *arg;
2286 *arg = NUL;
2287 list_one_var_a((char_u *)"",
2288 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 tv.v_type,
2290 s == NULL ? (char_u *)"" : s,
2291 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 *arg = c;
2293 vim_free(tf);
2294 }
2295 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299
2300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
2305
2306 return arg;
2307}
2308
2309/*
2310 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2311 * Returns a pointer to the char just after the var name.
2312 * Returns NULL if there is an error.
2313 */
2314 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002317 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321{
2322 int c1;
2323 char_u *name;
2324 char_u *p;
2325 char_u *arg_end = NULL;
2326 int len;
2327 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
2330 /*
2331 * ":let $VAR = expr": Set environment variable.
2332 */
2333 if (*arg == '$')
2334 {
2335 /* Find the end of the name. */
2336 ++arg;
2337 name = arg;
2338 len = get_env_len(&arg);
2339 if (len == 0)
2340 EMSG2(_(e_invarg2), name - 1);
2341 else
2342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 if (op != NULL && (*op == '+' || *op == '-'))
2344 EMSG2(_(e_letwrong), op);
2345 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002348 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 {
2350 c1 = name[len];
2351 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 p = get_tv_string_chk(tv);
2353 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 {
2355 int mustfree = FALSE;
2356 char_u *s = vim_getenv(name, &mustfree);
2357
2358 if (s != NULL)
2359 {
2360 p = tofree = concat_str(s, p);
2361 if (mustfree)
2362 vim_free(s);
2363 }
2364 }
2365 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (STRICMP(name, "HOME") == 0)
2369 init_homedir();
2370 else if (didset_vim && STRICMP(name, "VIM") == 0)
2371 didset_vim = FALSE;
2372 else if (didset_vimruntime
2373 && STRICMP(name, "VIMRUNTIME") == 0)
2374 didset_vimruntime = FALSE;
2375 arg_end = arg;
2376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380 }
2381 }
2382
2383 /*
2384 * ":let &option = expr": Set option value.
2385 * ":let &l:option = expr": Set local option value.
2386 * ":let &g:option = expr": Set global option value.
2387 */
2388 else if (*arg == '&')
2389 {
2390 /* Find the end of the name. */
2391 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002392 if (p == NULL || (endchars != NULL
2393 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 EMSG(_(e_letunexp));
2395 else
2396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 long n;
2398 int opt_type;
2399 long numval;
2400 char_u *stringval = NULL;
2401 char_u *s;
2402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 c1 = *p;
2404 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405
2406 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 s = get_tv_string_chk(tv); /* != NULL if number or string */
2408 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 {
2410 opt_type = get_option_value(arg, &numval,
2411 &stringval, opt_flags);
2412 if ((opt_type == 1 && *op == '.')
2413 || (opt_type == 0 && *op != '.'))
2414 EMSG2(_(e_letwrong), op);
2415 else
2416 {
2417 if (opt_type == 1) /* number */
2418 {
2419 if (*op == '+')
2420 n = numval + n;
2421 else
2422 n = numval - n;
2423 }
2424 else if (opt_type == 0 && stringval != NULL) /* string */
2425 {
2426 s = concat_str(stringval, s);
2427 vim_free(stringval);
2428 stringval = s;
2429 }
2430 }
2431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 if (s != NULL)
2433 {
2434 set_option_value(arg, n, s, opt_flags);
2435 arg_end = p;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
2440 }
2441
2442 /*
2443 * ":let @r = expr": Set register contents.
2444 */
2445 else if (*arg == '@')
2446 {
2447 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (op != NULL && (*op == '+' || *op == '-'))
2449 EMSG2(_(e_letwrong), op);
2450 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 EMSG(_(e_letunexp));
2453 else
2454 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 char_u *s;
2457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 p = get_tv_string_chk(tv);
2459 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002461 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 if (s != NULL)
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(s);
2466 }
2467 }
2468 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 arg_end = arg + 1;
2472 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002485 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 arg_end = p;
2494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498
2499 else
2500 EMSG2(_(e_invarg2), arg);
2501
2502 return arg_end;
2503}
2504
2505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2507 */
2508 static int
2509check_changedtick(arg)
2510 char_u *arg;
2511{
2512 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2513 {
2514 EMSG2(_(e_readonlyvar), arg);
2515 return TRUE;
2516 }
2517 return FALSE;
2518}
2519
2520/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 * Get an lval: variable, Dict item or List item that can be assigned a value
2522 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2523 * "name.key", "name.key[expr]" etc.
2524 * Indexing only works if "name" is an existing List or Dictionary.
2525 * "name" points to the start of the name.
2526 * If "rettv" is not NULL it points to the value to be assigned.
2527 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2528 * wrong; must end in space or cmd separator.
2529 *
2530 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002531 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 */
2534 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002535get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 typval_T *rettv;
2538 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 int unlet;
2540 int skip;
2541 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002542 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 char_u *expr_start, *expr_end;
2546 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 dictitem_T *v;
2548 typval_T var1;
2549 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558
2559 if (skip)
2560 {
2561 /* When skipping just find the end of the name. */
2562 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002563 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 }
2565
2566 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002567 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (expr_start != NULL)
2569 {
2570 /* Don't expand the name when we already know there is an error. */
2571 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2572 && *p != '[' && *p != '.')
2573 {
2574 EMSG(_(e_trailing));
2575 return NULL;
2576 }
2577
2578 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2579 if (lp->ll_exp_name == NULL)
2580 {
2581 /* Report an invalid expression in braces, unless the
2582 * expression evaluation has been cancelled due to an
2583 * aborting error, an interrupt, or an exception. */
2584 if (!aborting() && !quiet)
2585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 EMSG2(_(e_invarg2), name);
2588 return NULL;
2589 }
2590 }
2591 lp->ll_name = lp->ll_exp_name;
2592 }
2593 else
2594 lp->ll_name = name;
2595
2596 /* Without [idx] or .key we are done. */
2597 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2598 return p;
2599
2600 cc = *p;
2601 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002602 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (v == NULL && !quiet)
2604 EMSG2(_(e_undefvar), lp->ll_name);
2605 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 if (v == NULL)
2607 return NULL;
2608
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 /*
2610 * Loop until no more [idx] or .key is following.
2611 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002612 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2616 && !(lp->ll_tv->v_type == VAR_DICT
2617 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_("E689: Can only index a List or Dictionary"));
2621 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_("E708: [:] must come last"));
2627 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 len = -1;
2631 if (*p == '.')
2632 {
2633 key = p + 1;
2634 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2635 ;
2636 if (len == 0)
2637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (!quiet)
2639 EMSG(_(e_emptykey));
2640 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642 p = key + len;
2643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 else
2645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 if (*p == ':')
2649 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 else
2651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 empty1 = FALSE;
2653 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002655 if (get_tv_string_chk(&var1) == NULL)
2656 {
2657 /* not a number or string */
2658 clear_tv(&var1);
2659 return NULL;
2660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 }
2662
2663 /* Optionally get the second index [ :expr]. */
2664 if (*p == ':')
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670 if (!empty1)
2671 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (rettv != NULL && (rettv->v_type != VAR_LIST
2675 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = skipwhite(p + 1);
2684 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 else
2687 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 if (get_tv_string_chk(&var2) == NULL)
2696 {
2697 /* not a number or string */
2698 if (!empty1)
2699 clear_tv(&var1);
2700 clear_tv(&var2);
2701 return NULL;
2702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719
2720 /* Skip to past ']'. */
2721 ++p;
2722 }
2723
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
2726 if (len == -1)
2727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (*key == NUL)
2731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!quiet)
2733 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 lp->ll_list = NULL;
2739 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002740 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002742 /* When assigning to a scope dictionary check that a function and
2743 * variable name is valid (only variable name unless it is l: or
2744 * g: dictionary). Disallow overwriting a builtin function. */
2745 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 int prevval;
2748 int wrong;
2749
2750 if (len != -1)
2751 {
2752 prevval = key[len];
2753 key[len] = NUL;
2754 }
2755 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2756 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002757 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002758 || !valid_varname(key);
2759 if (len != -1)
2760 key[len] = prevval;
2761 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762 return NULL;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 /* Can't add "v:" variable. */
2768 if (lp->ll_dict == &vimvardict)
2769 {
2770 EMSG2(_(e_illvar), name);
2771 return NULL;
2772 }
2773
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002774 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 if (len == -1)
2780 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 }
2783 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (len == -1)
2788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 p = NULL;
2791 break;
2792 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 /* existing variable, need to check if it can be changed */
2794 else if (var_check_ro(lp->ll_di->di_flags, name))
2795 return NULL;
2796
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 }
2801 else
2802 {
2803 /*
2804 * Get the number and item for the only or first index of the List.
2805 */
2806 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 else
2809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002810 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var1);
2812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_list = lp->ll_tv->vval.v_list;
2815 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2816 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002818 if (lp->ll_n1 < 0)
2819 {
2820 lp->ll_n1 = 0;
2821 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2822 }
2823 }
2824 if (lp->ll_li == NULL)
2825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002828 if (!quiet)
2829 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832
2833 /*
2834 * May need to find the item or absolute index for the second
2835 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 * When no index given: "lp->ll_empty2" is TRUE.
2837 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 {
2848 if (!quiet)
2849 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002851 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2856 if (lp->ll_n1 < 0)
2857 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2858 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 {
2860 if (!quiet)
2861 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002864 }
2865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002868 }
2869
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return p;
2871}
2872
2873/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002874 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 */
2876 static void
2877clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879{
2880 vim_free(lp->ll_exp_name);
2881 vim_free(lp->ll_newkey);
2882}
2883
2884/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002887 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 */
2889 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 listitem_T *ri;
2899 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900
2901 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 cc = *endp;
2906 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 if (op != NULL && *op != '=')
2908 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002912 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002913 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 {
2915 if (tv_op(&tv, rettv, op) == OK)
2916 set_var(lp->ll_name, &tv, FALSE);
2917 clear_tv(&tv);
2918 }
2919 }
2920 else
2921 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 else if (tv_check_lock(lp->ll_newkey == NULL
2926 ? lp->ll_tv->v_lock
2927 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2928 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 else if (lp->ll_range)
2930 {
2931 /*
2932 * Assign the List values to the list items.
2933 */
2934 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002935 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 if (op != NULL && *op != '=')
2937 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2938 else
2939 {
2940 clear_tv(&lp->ll_li->li_tv);
2941 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 ri = ri->li_next;
2944 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2945 break;
2946 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002949 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 ri = NULL;
2952 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002953 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 lp->ll_li = lp->ll_li->li_next;
2956 ++lp->ll_n1;
2957 }
2958 if (ri != NULL)
2959 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 else if (lp->ll_empty2
2961 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 : lp->ll_n1 != lp->ll_n2)
2963 EMSG(_("E711: List value has not enough items"));
2964 }
2965 else
2966 {
2967 /*
2968 * Assign to a List or Dictionary item.
2969 */
2970 if (lp->ll_newkey != NULL)
2971 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002972 if (op != NULL && *op != '=')
2973 {
2974 EMSG2(_(e_letwrong), op);
2975 return;
2976 }
2977
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002979 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 if (di == NULL)
2981 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002982 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2983 {
2984 vim_free(di);
2985 return;
2986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 else if (op != NULL && *op != '=')
2990 {
2991 tv_op(lp->ll_tv, rettv, op);
2992 return;
2993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 /*
2998 * Assign the value to the variable or list item.
2999 */
3000 if (copy)
3001 copy_tv(rettv, lp->ll_tv);
3002 else
3003 {
3004 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003005 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003007 }
3008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003009}
3010
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3013 * Returns OK or FAIL.
3014 */
3015 static int
3016tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 typval_T *tv1;
3018 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 char_u *op;
3020{
3021 long n;
3022 char_u numbuf[NUMBUFLEN];
3023 char_u *s;
3024
3025 /* Can't do anything with a Funcref or a Dict on the right. */
3026 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3027 {
3028 switch (tv1->v_type)
3029 {
3030 case VAR_DICT:
3031 case VAR_FUNC:
3032 break;
3033
3034 case VAR_LIST:
3035 if (*op != '+' || tv2->v_type != VAR_LIST)
3036 break;
3037 /* List += List */
3038 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3039 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3040 return OK;
3041
3042 case VAR_NUMBER:
3043 case VAR_STRING:
3044 if (tv2->v_type == VAR_LIST)
3045 break;
3046 if (*op == '+' || *op == '-')
3047 {
3048 /* nr += nr or nr -= nr*/
3049 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050#ifdef FEAT_FLOAT
3051 if (tv2->v_type == VAR_FLOAT)
3052 {
3053 float_T f = n;
3054
3055 if (*op == '+')
3056 f += tv2->vval.v_float;
3057 else
3058 f -= tv2->vval.v_float;
3059 clear_tv(tv1);
3060 tv1->v_type = VAR_FLOAT;
3061 tv1->vval.v_float = f;
3062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064#endif
3065 {
3066 if (*op == '+')
3067 n += get_tv_number(tv2);
3068 else
3069 n -= get_tv_number(tv2);
3070 clear_tv(tv1);
3071 tv1->v_type = VAR_NUMBER;
3072 tv1->vval.v_number = n;
3073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 }
3075 else
3076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077 if (tv2->v_type == VAR_FLOAT)
3078 break;
3079
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 /* str .= str */
3081 s = get_tv_string(tv1);
3082 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3083 clear_tv(tv1);
3084 tv1->v_type = VAR_STRING;
3085 tv1->vval.v_string = s;
3086 }
3087 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088
3089#ifdef FEAT_FLOAT
3090 case VAR_FLOAT:
3091 {
3092 float_T f;
3093
3094 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3095 && tv2->v_type != VAR_NUMBER
3096 && tv2->v_type != VAR_STRING))
3097 break;
3098 if (tv2->v_type == VAR_FLOAT)
3099 f = tv2->vval.v_float;
3100 else
3101 f = get_tv_number(tv2);
3102 if (*op == '+')
3103 tv1->vval.v_float += f;
3104 else
3105 tv1->vval.v_float -= f;
3106 }
3107 return OK;
3108#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 }
3110 }
3111
3112 EMSG2(_(e_letwrong), op);
3113 return FAIL;
3114}
3115
3116/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 * Add a watcher to a list.
3118 */
3119 static void
3120list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 list_T *l;
3122 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123{
3124 lw->lw_next = l->lv_watch;
3125 l->lv_watch = lw;
3126}
3127
3128/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003129 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130 * No warning when it isn't found...
3131 */
3132 static void
3133list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003134 list_T *l;
3135 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003136{
Bram Moolenaar33570922005-01-25 22:26:29 +00003137 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138
3139 lwp = &l->lv_watch;
3140 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3141 {
3142 if (lw == lwrem)
3143 {
3144 *lwp = lw->lw_next;
3145 break;
3146 }
3147 lwp = &lw->lw_next;
3148 }
3149}
3150
3151/*
3152 * Just before removing an item from a list: advance watchers to the next
3153 * item.
3154 */
3155 static void
3156list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 list_T *l;
3158 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159{
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003161
3162 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3163 if (lw->lw_item == item)
3164 lw->lw_item = item->li_next;
3165}
3166
3167/*
3168 * Evaluate the expression used in a ":for var in expr" command.
3169 * "arg" points to "var".
3170 * Set "*errp" to TRUE for an error, FALSE otherwise;
3171 * Return a pointer that holds the info. Null when there is an error.
3172 */
3173 void *
3174eval_for_line(arg, errp, nextcmdp, skip)
3175 char_u *arg;
3176 int *errp;
3177 char_u **nextcmdp;
3178 int skip;
3179{
Bram Moolenaar33570922005-01-25 22:26:29 +00003180 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 typval_T tv;
3183 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184
3185 *errp = TRUE; /* default: there is an error */
3186
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 if (fi == NULL)
3189 return NULL;
3190
3191 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3192 if (expr == NULL)
3193 return fi;
3194
3195 expr = skipwhite(expr);
3196 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3197 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003198 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 return fi;
3200 }
3201
3202 if (skip)
3203 ++emsg_skip;
3204 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3205 {
3206 *errp = FALSE;
3207 if (!skip)
3208 {
3209 l = tv.vval.v_list;
3210 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003211 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003213 clear_tv(&tv);
3214 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 else
3216 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003217 /* No need to increment the refcount, it's already set for the
3218 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 fi->fi_list = l;
3220 list_add_watch(l, &fi->fi_lw);
3221 fi->fi_lw.lw_item = l->lv_first;
3222 }
3223 }
3224 }
3225 if (skip)
3226 --emsg_skip;
3227
3228 return fi;
3229}
3230
3231/*
3232 * Use the first item in a ":for" list. Advance to the next.
3233 * Assign the values to the variable (list). "arg" points to the first one.
3234 * Return TRUE when a valid item was found, FALSE when at end of list or
3235 * something wrong.
3236 */
3237 int
3238next_for_item(fi_void, arg)
3239 void *fi_void;
3240 char_u *arg;
3241{
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
3246 item = fi->fi_lw.lw_item;
3247 if (item == NULL)
3248 result = FALSE;
3249 else
3250 {
3251 fi->fi_lw.lw_item = item->li_next;
3252 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3253 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3254 }
3255 return result;
3256}
3257
3258/*
3259 * Free the structure used to store info used by ":for".
3260 */
3261 void
3262free_for_info(fi_void)
3263 void *fi_void;
3264{
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003267 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 list_unref(fi->fi_list);
3271 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 vim_free(fi);
3273}
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3276
3277 void
3278set_context_for_expression(xp, arg, cmdidx)
3279 expand_T *xp;
3280 char_u *arg;
3281 cmdidx_T cmdidx;
3282{
3283 int got_eq = FALSE;
3284 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 if (cmdidx == CMD_let)
3288 {
3289 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003290 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 {
3292 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003293 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 {
3295 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (vim_iswhite(*p))
3298 break;
3299 }
3300 return;
3301 }
3302 }
3303 else
3304 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3305 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 while ((xp->xp_pattern = vim_strpbrk(arg,
3307 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3308 {
3309 c = *xp->xp_pattern;
3310 if (c == '&')
3311 {
3312 c = xp->xp_pattern[1];
3313 if (c == '&')
3314 {
3315 ++xp->xp_pattern;
3316 xp->xp_context = cmdidx != CMD_let || got_eq
3317 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3318 }
3319 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003322 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3323 xp->xp_pattern += 2;
3324
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327 else if (c == '$')
3328 {
3329 /* environment variable */
3330 xp->xp_context = EXPAND_ENV_VARS;
3331 }
3332 else if (c == '=')
3333 {
3334 got_eq = TRUE;
3335 xp->xp_context = EXPAND_EXPRESSION;
3336 }
3337 else if (c == '<'
3338 && xp->xp_context == EXPAND_FUNCTIONS
3339 && vim_strchr(xp->xp_pattern, '(') == NULL)
3340 {
3341 /* Function name can start with "<SNR>" */
3342 break;
3343 }
3344 else if (cmdidx != CMD_let || got_eq)
3345 {
3346 if (c == '"') /* string */
3347 {
3348 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3349 if (c == '\\' && xp->xp_pattern[1] != NUL)
3350 ++xp->xp_pattern;
3351 xp->xp_context = EXPAND_NOTHING;
3352 }
3353 else if (c == '\'') /* literal string */
3354 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3357 /* skip */ ;
3358 xp->xp_context = EXPAND_NOTHING;
3359 }
3360 else if (c == '|')
3361 {
3362 if (xp->xp_pattern[1] == '|')
3363 {
3364 ++xp->xp_pattern;
3365 xp->xp_context = EXPAND_EXPRESSION;
3366 }
3367 else
3368 xp->xp_context = EXPAND_COMMANDS;
3369 }
3370 else
3371 xp->xp_context = EXPAND_EXPRESSION;
3372 }
3373 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003374 /* Doesn't look like something valid, expand as an expression
3375 * anyway. */
3376 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 arg = xp->xp_pattern;
3378 if (*arg != NUL)
3379 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3380 /* skip */ ;
3381 }
3382 xp->xp_pattern = arg;
3383}
3384
3385#endif /* FEAT_CMDL_COMPL */
3386
3387/*
3388 * ":1,25call func(arg1, arg2)" function call.
3389 */
3390 void
3391ex_call(eap)
3392 exarg_T *eap;
3393{
3394 char_u *arg = eap->arg;
3395 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003397 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 linenr_T lnum;
3401 int doesrange;
3402 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003403 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003405 if (eap->skip)
3406 {
3407 /* trans_function_name() doesn't work well when skipping, use eval0()
3408 * instead to skip to any following command, e.g. for:
3409 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003410 ++emsg_skip;
3411 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3412 clear_tv(&rettv);
3413 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003414 return;
3415 }
3416
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003417 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003418 if (fudi.fd_newkey != NULL)
3419 {
3420 /* Still need to give an error message for missing key. */
3421 EMSG2(_(e_dictkey), fudi.fd_newkey);
3422 vim_free(fudi.fd_newkey);
3423 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 if (tofree == NULL)
3425 return;
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 /* Increase refcount on dictionary, it could get deleted when evaluating
3428 * the arguments. */
3429 if (fudi.fd_dict != NULL)
3430 ++fudi.fd_dict->dv_refcount;
3431
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003432 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003433 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar532c7802005-01-27 14:44:31 +00003436 /* Skip white space to allow ":call func ()". Not good, but required for
3437 * backward compatibility. */
3438 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 if (*startarg != '(')
3442 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003443 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 goto end;
3445 }
3446
3447 /*
3448 * When skipping, evaluate the function once, to find the end of the
3449 * arguments.
3450 * When the function takes a range, this is discovered after the first
3451 * call, and the loop is broken.
3452 */
3453 if (eap->skip)
3454 {
3455 ++emsg_skip;
3456 lnum = eap->line2; /* do it once, also with an invalid range */
3457 }
3458 else
3459 lnum = eap->line1;
3460 for ( ; lnum <= eap->line2; ++lnum)
3461 {
3462 if (!eap->skip && eap->addr_count > 0)
3463 {
3464 curwin->w_cursor.lnum = lnum;
3465 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003466#ifdef FEAT_VIRTUALEDIT
3467 curwin->w_cursor.coladd = 0;
3468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 eap->line1, eap->line2, &doesrange,
3473 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
3475 failed = TRUE;
3476 break;
3477 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003478
3479 /* Handle a function returning a Funcref, Dictionary or List. */
3480 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3481 {
3482 failed = TRUE;
3483 break;
3484 }
3485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (doesrange || eap->skip)
3488 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003491 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003493 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (aborting())
3495 break;
3496 }
3497 if (eap->skip)
3498 --emsg_skip;
3499
3500 if (!failed)
3501 {
3502 /* Check for trailing illegal characters and a following command. */
3503 if (!ends_excmd(*arg))
3504 {
3505 emsg_severe = TRUE;
3506 EMSG(_(e_trailing));
3507 }
3508 else
3509 eap->nextcmd = check_nextcmd(arg);
3510 }
3511
3512end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003513 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003514 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515}
3516
3517/*
3518 * ":unlet[!] var1 ... " command.
3519 */
3520 void
3521ex_unlet(eap)
3522 exarg_T *eap;
3523{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 ex_unletlock(eap, eap->arg, 0);
3525}
3526
3527/*
3528 * ":lockvar" and ":unlockvar" commands
3529 */
3530 void
3531ex_lockvar(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 int deep = 2;
3536
3537 if (eap->forceit)
3538 deep = -1;
3539 else if (vim_isdigit(*arg))
3540 {
3541 deep = getdigits(&arg);
3542 arg = skipwhite(arg);
3543 }
3544
3545 ex_unletlock(eap, arg, deep);
3546}
3547
3548/*
3549 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3550 */
3551 static void
3552ex_unletlock(eap, argstart, deep)
3553 exarg_T *eap;
3554 char_u *argstart;
3555 int deep;
3556{
3557 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003560 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 do
3563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003565 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3566 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003567 if (lv.ll_name == NULL)
3568 error = TRUE; /* error but continue parsing */
3569 if (name_end == NULL || (!vim_iswhite(*name_end)
3570 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 if (name_end != NULL)
3573 {
3574 emsg_severe = TRUE;
3575 EMSG(_(e_trailing));
3576 }
3577 if (!(eap->skip || error))
3578 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 break;
3580 }
3581
3582 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 {
3584 if (eap->cmdidx == CMD_unlet)
3585 {
3586 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3587 error = TRUE;
3588 }
3589 else
3590 {
3591 if (do_lock_var(&lv, name_end, deep,
3592 eap->cmdidx == CMD_lockvar) == FAIL)
3593 error = TRUE;
3594 }
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 if (!eap->skip)
3598 clear_lval(&lv);
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 arg = skipwhite(name_end);
3601 } while (!ends_excmd(*arg));
3602
3603 eap->nextcmd = check_nextcmd(arg);
3604}
3605
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 int forceit;
3611{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 int ret = OK;
3613 int cc;
3614
3615 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 cc = *name_end;
3618 *name_end = NUL;
3619
3620 /* Normal name or expanded name. */
3621 if (check_changedtick(lp->ll_name))
3622 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003627 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3628 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 else if (lp->ll_range)
3630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003631 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632
3633 /* Delete a range of List items. */
3634 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3635 {
3636 li = lp->ll_li->li_next;
3637 listitem_remove(lp->ll_list, lp->ll_li);
3638 lp->ll_li = li;
3639 ++lp->ll_n1;
3640 }
3641 }
3642 else
3643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 /* unlet a List item. */
3646 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003647 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003649 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 }
3651
3652 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653}
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655/*
3656 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
3659 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 hashtab_T *ht;
3665 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003666 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003667 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar33570922005-01-25 22:26:29 +00003669 ht = find_var_ht(name, &varname);
3670 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003672 hi = hash_find(ht, varname);
3673 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003675 di = HI2DI(hi);
3676 if (var_check_fixed(di->di_flags, name)
3677 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
3679 delete_var(ht, hi);
3680 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 if (forceit)
3684 return OK;
3685 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FAIL;
3687}
3688
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003689/*
3690 * Lock or unlock variable indicated by "lp".
3691 * "deep" is the levels to go (-1 for unlimited);
3692 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3693 */
3694 static int
3695do_lock_var(lp, name_end, deep, lock)
3696 lval_T *lp;
3697 char_u *name_end;
3698 int deep;
3699 int lock;
3700{
3701 int ret = OK;
3702 int cc;
3703 dictitem_T *di;
3704
3705 if (deep == 0) /* nothing to do */
3706 return OK;
3707
3708 if (lp->ll_tv == NULL)
3709 {
3710 cc = *name_end;
3711 *name_end = NUL;
3712
3713 /* Normal name or expanded name. */
3714 if (check_changedtick(lp->ll_name))
3715 ret = FAIL;
3716 else
3717 {
3718 di = find_var(lp->ll_name, NULL);
3719 if (di == NULL)
3720 ret = FAIL;
3721 else
3722 {
3723 if (lock)
3724 di->di_flags |= DI_FLAGS_LOCK;
3725 else
3726 di->di_flags &= ~DI_FLAGS_LOCK;
3727 item_lock(&di->di_tv, deep, lock);
3728 }
3729 }
3730 *name_end = cc;
3731 }
3732 else if (lp->ll_range)
3733 {
3734 listitem_T *li = lp->ll_li;
3735
3736 /* (un)lock a range of List items. */
3737 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3738 {
3739 item_lock(&li->li_tv, deep, lock);
3740 li = li->li_next;
3741 ++lp->ll_n1;
3742 }
3743 }
3744 else if (lp->ll_list != NULL)
3745 /* (un)lock a List item. */
3746 item_lock(&lp->ll_li->li_tv, deep, lock);
3747 else
3748 /* un(lock) a Dictionary item. */
3749 item_lock(&lp->ll_di->di_tv, deep, lock);
3750
3751 return ret;
3752}
3753
3754/*
3755 * Lock or unlock an item. "deep" is nr of levels to go.
3756 */
3757 static void
3758item_lock(tv, deep, lock)
3759 typval_T *tv;
3760 int deep;
3761 int lock;
3762{
3763 static int recurse = 0;
3764 list_T *l;
3765 listitem_T *li;
3766 dict_T *d;
3767 hashitem_T *hi;
3768 int todo;
3769
3770 if (recurse >= DICT_MAXNEST)
3771 {
3772 EMSG(_("E743: variable nested too deep for (un)lock"));
3773 return;
3774 }
3775 if (deep == 0)
3776 return;
3777 ++recurse;
3778
3779 /* lock/unlock the item itself */
3780 if (lock)
3781 tv->v_lock |= VAR_LOCKED;
3782 else
3783 tv->v_lock &= ~VAR_LOCKED;
3784
3785 switch (tv->v_type)
3786 {
3787 case VAR_LIST:
3788 if ((l = tv->vval.v_list) != NULL)
3789 {
3790 if (lock)
3791 l->lv_lock |= VAR_LOCKED;
3792 else
3793 l->lv_lock &= ~VAR_LOCKED;
3794 if (deep < 0 || deep > 1)
3795 /* recursive: lock/unlock the items the List contains */
3796 for (li = l->lv_first; li != NULL; li = li->li_next)
3797 item_lock(&li->li_tv, deep - 1, lock);
3798 }
3799 break;
3800 case VAR_DICT:
3801 if ((d = tv->vval.v_dict) != NULL)
3802 {
3803 if (lock)
3804 d->dv_lock |= VAR_LOCKED;
3805 else
3806 d->dv_lock &= ~VAR_LOCKED;
3807 if (deep < 0 || deep > 1)
3808 {
3809 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003810 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3812 {
3813 if (!HASHITEM_EMPTY(hi))
3814 {
3815 --todo;
3816 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3817 }
3818 }
3819 }
3820 }
3821 }
3822 --recurse;
3823}
3824
Bram Moolenaara40058a2005-07-11 22:42:07 +00003825/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003826 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3827 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003828 */
3829 static int
3830tv_islocked(tv)
3831 typval_T *tv;
3832{
3833 return (tv->v_lock & VAR_LOCKED)
3834 || (tv->v_type == VAR_LIST
3835 && tv->vval.v_list != NULL
3836 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3837 || (tv->v_type == VAR_DICT
3838 && tv->vval.v_dict != NULL
3839 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3840}
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3843/*
3844 * Delete all "menutrans_" variables.
3845 */
3846 void
3847del_menutrans_vars()
3848{
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003853 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003854 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003855 {
3856 if (!HASHITEM_EMPTY(hi))
3857 {
3858 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3860 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003861 }
3862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003863 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864}
3865#endif
3866
3867#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3868
3869/*
3870 * Local string buffer for the next two functions to store a variable name
3871 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3872 * get_user_var_name().
3873 */
3874
3875static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3876
3877static char_u *varnamebuf = NULL;
3878static int varnamebuflen = 0;
3879
3880/*
3881 * Function to concatenate a prefix and a variable name.
3882 */
3883 static char_u *
3884cat_prefix_varname(prefix, name)
3885 int prefix;
3886 char_u *name;
3887{
3888 int len;
3889
3890 len = (int)STRLEN(name) + 3;
3891 if (len > varnamebuflen)
3892 {
3893 vim_free(varnamebuf);
3894 len += 10; /* some additional space */
3895 varnamebuf = alloc(len);
3896 if (varnamebuf == NULL)
3897 {
3898 varnamebuflen = 0;
3899 return NULL;
3900 }
3901 varnamebuflen = len;
3902 }
3903 *varnamebuf = prefix;
3904 varnamebuf[1] = ':';
3905 STRCPY(varnamebuf + 2, name);
3906 return varnamebuf;
3907}
3908
3909/*
3910 * Function given to ExpandGeneric() to obtain the list of user defined
3911 * (global/buffer/window/built-in) variable names.
3912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 char_u *
3914get_user_var_name(xp, idx)
3915 expand_T *xp;
3916 int idx;
3917{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003918 static long_u gdone;
3919 static long_u bdone;
3920 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921#ifdef FEAT_WINDOWS
3922 static long_u tdone;
3923#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003924 static int vidx;
3925 static hashitem_T *hi;
3926 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003930 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 tdone = 0;
3933#endif
3934 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003935
3936 /* Global variables */
3937 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003941 else
3942 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 while (HASHITEM_EMPTY(hi))
3944 ++hi;
3945 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3946 return cat_prefix_varname('g', hi->hi_key);
3947 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003949
3950 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003951 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003956 else
3957 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 while (HASHITEM_EMPTY(hi))
3959 ++hi;
3960 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 return (char_u *)"b:changedtick";
3966 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003967
3968 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003969 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003972 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003974 else
3975 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003976 while (HASHITEM_EMPTY(hi))
3977 ++hi;
3978 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981#ifdef FEAT_WINDOWS
3982 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003983 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003984 if (tdone < ht->ht_used)
3985 {
3986 if (tdone++ == 0)
3987 hi = ht->ht_array;
3988 else
3989 ++hi;
3990 while (HASHITEM_EMPTY(hi))
3991 ++hi;
3992 return cat_prefix_varname('t', hi->hi_key);
3993 }
3994#endif
3995
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 /* v: variables */
3997 if (vidx < VV_LEN)
3998 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 vim_free(varnamebuf);
4001 varnamebuf = NULL;
4002 varnamebuflen = 0;
4003 return NULL;
4004}
4005
4006#endif /* FEAT_CMDL_COMPL */
4007
4008/*
4009 * types for expressions.
4010 */
4011typedef enum
4012{
4013 TYPE_UNKNOWN = 0
4014 , TYPE_EQUAL /* == */
4015 , TYPE_NEQUAL /* != */
4016 , TYPE_GREATER /* > */
4017 , TYPE_GEQUAL /* >= */
4018 , TYPE_SMALLER /* < */
4019 , TYPE_SEQUAL /* <= */
4020 , TYPE_MATCH /* =~ */
4021 , TYPE_NOMATCH /* !~ */
4022} exptype_T;
4023
4024/*
4025 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4028 */
4029
4030/*
4031 * Handle zero level expression.
4032 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004034 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 * Return OK or FAIL.
4036 */
4037 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 char_u **nextcmd;
4042 int evaluate;
4043{
4044 int ret;
4045 char_u *p;
4046
4047 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (ret == FAIL || !ends_excmd(*p))
4050 {
4051 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004052 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 /*
4054 * Report the invalid expression unless the expression evaluation has
4055 * been cancelled due to an aborting error, an interrupt, or an
4056 * exception.
4057 */
4058 if (!aborting())
4059 EMSG2(_(e_invexpr2), arg);
4060 ret = FAIL;
4061 }
4062 if (nextcmd != NULL)
4063 *nextcmd = check_nextcmd(p);
4064
4065 return ret;
4066}
4067
4068/*
4069 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004070 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 *
4072 * "arg" must point to the first non-white of the expression.
4073 * "arg" is advanced to the next non-white after the recognized expression.
4074 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004075 * Note: "rettv.v_lock" is not set.
4076 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 * Return OK or FAIL.
4078 */
4079 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 int evaluate;
4084{
4085 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 /*
4089 * Get the first variable.
4090 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return FAIL;
4093
4094 if ((*arg)[0] == '?')
4095 {
4096 result = FALSE;
4097 if (evaluate)
4098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 int error = FALSE;
4100
4101 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104 if (error)
4105 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107
4108 /*
4109 * Get the second variable.
4110 */
4111 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 return FAIL;
4114
4115 /*
4116 * Check for the ":".
4117 */
4118 if ((*arg)[0] != ':')
4119 {
4120 EMSG(_("E109: Missing ':' after '?'"));
4121 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
4125
4126 /*
4127 * Get the third variable.
4128 */
4129 *arg = skipwhite(*arg + 1);
4130 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4131 {
4132 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FAIL;
4135 }
4136 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139
4140 return OK;
4141}
4142
4143/*
4144 * Handle first level expression:
4145 * expr2 || expr2 || expr2 logical OR
4146 *
4147 * "arg" must point to the first non-white of the expression.
4148 * "arg" is advanced to the next non-white after the recognized expression.
4149 *
4150 * Return OK or FAIL.
4151 */
4152 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004153eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int evaluate;
4157{
Bram Moolenaar33570922005-01-25 22:26:29 +00004158 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 long result;
4160 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
4163 /*
4164 * Get the first variable.
4165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 return FAIL;
4168
4169 /*
4170 * Repeat until there is no following "||".
4171 */
4172 first = TRUE;
4173 result = FALSE;
4174 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4175 {
4176 if (evaluate && first)
4177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 if (error)
4182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 first = FALSE;
4184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 2);
4190 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4191 return FAIL;
4192
4193 /*
4194 * Compute the result.
4195 */
4196 if (evaluate && !result)
4197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 if (error)
4202 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 if (evaluate)
4205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 rettv->v_type = VAR_NUMBER;
4207 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209 }
4210
4211 return OK;
4212}
4213
4214/*
4215 * Handle second level expression:
4216 * expr3 && expr3 && expr3 logical AND
4217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
4221 * Return OK or FAIL.
4222 */
4223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int evaluate;
4228{
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 long result;
4231 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 /*
4241 * Repeat until there is no following "&&".
4242 */
4243 first = TRUE;
4244 result = TRUE;
4245 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4246 {
4247 if (evaluate && first)
4248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 if (error)
4253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 first = FALSE;
4255 }
4256
4257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(*arg + 2);
4261 if (eval4(arg, &var2, evaluate && result) == FAIL)
4262 return FAIL;
4263
4264 /*
4265 * Compute the result.
4266 */
4267 if (evaluate && result)
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (error)
4273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 if (evaluate)
4276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 rettv->v_type = VAR_NUMBER;
4278 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281
4282 return OK;
4283}
4284
4285/*
4286 * Handle third level expression:
4287 * var1 == var2
4288 * var1 =~ var2
4289 * var1 != var2
4290 * var1 !~ var2
4291 * var1 > var2
4292 * var1 >= var2
4293 * var1 < var2
4294 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004295 * var1 is var2
4296 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 int evaluate;
4308{
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 char_u *p;
4311 int i;
4312 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004313 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 int len = 2;
4315 long n1, n2;
4316 char_u *s1, *s2;
4317 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4318 regmatch_T regmatch;
4319 int ic;
4320 char_u *save_cpo;
4321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 p = *arg;
4329 switch (p[0])
4330 {
4331 case '=': if (p[1] == '=')
4332 type = TYPE_EQUAL;
4333 else if (p[1] == '~')
4334 type = TYPE_MATCH;
4335 break;
4336 case '!': if (p[1] == '=')
4337 type = TYPE_NEQUAL;
4338 else if (p[1] == '~')
4339 type = TYPE_NOMATCH;
4340 break;
4341 case '>': if (p[1] != '=')
4342 {
4343 type = TYPE_GREATER;
4344 len = 1;
4345 }
4346 else
4347 type = TYPE_GEQUAL;
4348 break;
4349 case '<': if (p[1] != '=')
4350 {
4351 type = TYPE_SMALLER;
4352 len = 1;
4353 }
4354 else
4355 type = TYPE_SEQUAL;
4356 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004357 case 'i': if (p[1] == 's')
4358 {
4359 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4360 len = 5;
4361 if (!vim_isIDc(p[len]))
4362 {
4363 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4364 type_is = TRUE;
4365 }
4366 }
4367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369
4370 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 if (type != TYPE_UNKNOWN)
4374 {
4375 /* extra question mark appended: ignore case */
4376 if (p[len] == '?')
4377 {
4378 ic = TRUE;
4379 ++len;
4380 }
4381 /* extra '#' appended: match case */
4382 else if (p[len] == '#')
4383 {
4384 ic = FALSE;
4385 ++len;
4386 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004387 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 else
4389 ic = p_ic;
4390
4391 /*
4392 * Get the second variable.
4393 */
4394 *arg = skipwhite(p + len);
4395 if (eval5(arg, &var2, evaluate) == FAIL)
4396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004397 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 return FAIL;
4399 }
4400
4401 if (evaluate)
4402 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 if (type_is && rettv->v_type != var2.v_type)
4404 {
4405 /* For "is" a different type always means FALSE, for "notis"
4406 * it means TRUE. */
4407 n1 = (type == TYPE_NEQUAL);
4408 }
4409 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4410 {
4411 if (type_is)
4412 {
4413 n1 = (rettv->v_type == var2.v_type
4414 && rettv->vval.v_list == var2.vval.v_list);
4415 if (type == TYPE_NEQUAL)
4416 n1 = !n1;
4417 }
4418 else if (rettv->v_type != var2.v_type
4419 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4420 {
4421 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004422 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004424 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004425 clear_tv(rettv);
4426 clear_tv(&var2);
4427 return FAIL;
4428 }
4429 else
4430 {
4431 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004432 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4433 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 }
4438
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004439 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4440 {
4441 if (type_is)
4442 {
4443 n1 = (rettv->v_type == var2.v_type
4444 && rettv->vval.v_dict == var2.vval.v_dict);
4445 if (type == TYPE_NEQUAL)
4446 n1 = !n1;
4447 }
4448 else if (rettv->v_type != var2.v_type
4449 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4450 {
4451 if (rettv->v_type != var2.v_type)
4452 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4453 else
4454 EMSG(_("E736: Invalid operation for Dictionary"));
4455 clear_tv(rettv);
4456 clear_tv(&var2);
4457 return FAIL;
4458 }
4459 else
4460 {
4461 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004462 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4463 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004464 if (type == TYPE_NEQUAL)
4465 n1 = !n1;
4466 }
4467 }
4468
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4470 {
4471 if (rettv->v_type != var2.v_type
4472 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4473 {
4474 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004475 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004477 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004478 clear_tv(rettv);
4479 clear_tv(&var2);
4480 return FAIL;
4481 }
4482 else
4483 {
4484 /* Compare two Funcrefs for being equal or unequal. */
4485 if (rettv->vval.v_string == NULL
4486 || var2.vval.v_string == NULL)
4487 n1 = FALSE;
4488 else
4489 n1 = STRCMP(rettv->vval.v_string,
4490 var2.vval.v_string) == 0;
4491 if (type == TYPE_NEQUAL)
4492 n1 = !n1;
4493 }
4494 }
4495
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496#ifdef FEAT_FLOAT
4497 /*
4498 * If one of the two variables is a float, compare as a float.
4499 * When using "=~" or "!~", always compare as string.
4500 */
4501 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4502 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4503 {
4504 float_T f1, f2;
4505
4506 if (rettv->v_type == VAR_FLOAT)
4507 f1 = rettv->vval.v_float;
4508 else
4509 f1 = get_tv_number(rettv);
4510 if (var2.v_type == VAR_FLOAT)
4511 f2 = var2.vval.v_float;
4512 else
4513 f2 = get_tv_number(&var2);
4514 n1 = FALSE;
4515 switch (type)
4516 {
4517 case TYPE_EQUAL: n1 = (f1 == f2); break;
4518 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4519 case TYPE_GREATER: n1 = (f1 > f2); break;
4520 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4521 case TYPE_SMALLER: n1 = (f1 < f2); break;
4522 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4523 case TYPE_UNKNOWN:
4524 case TYPE_MATCH:
4525 case TYPE_NOMATCH: break; /* avoid gcc warning */
4526 }
4527 }
4528#endif
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /*
4531 * If one of the two variables is a number, compare as a number.
4532 * When using "=~" or "!~", always compare as string.
4533 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004537 n1 = get_tv_number(rettv);
4538 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 switch (type)
4540 {
4541 case TYPE_EQUAL: n1 = (n1 == n2); break;
4542 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4543 case TYPE_GREATER: n1 = (n1 > n2); break;
4544 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4545 case TYPE_SMALLER: n1 = (n1 < n2); break;
4546 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4547 case TYPE_UNKNOWN:
4548 case TYPE_MATCH:
4549 case TYPE_NOMATCH: break; /* avoid gcc warning */
4550 }
4551 }
4552 else
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 s1 = get_tv_string_buf(rettv, buf1);
4555 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4557 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4558 else
4559 i = 0;
4560 n1 = FALSE;
4561 switch (type)
4562 {
4563 case TYPE_EQUAL: n1 = (i == 0); break;
4564 case TYPE_NEQUAL: n1 = (i != 0); break;
4565 case TYPE_GREATER: n1 = (i > 0); break;
4566 case TYPE_GEQUAL: n1 = (i >= 0); break;
4567 case TYPE_SMALLER: n1 = (i < 0); break;
4568 case TYPE_SEQUAL: n1 = (i <= 0); break;
4569
4570 case TYPE_MATCH:
4571 case TYPE_NOMATCH:
4572 /* avoid 'l' flag in 'cpoptions' */
4573 save_cpo = p_cpo;
4574 p_cpo = (char_u *)"";
4575 regmatch.regprog = vim_regcomp(s2,
4576 RE_MAGIC + RE_STRING);
4577 regmatch.rm_ic = ic;
4578 if (regmatch.regprog != NULL)
4579 {
4580 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4581 vim_free(regmatch.regprog);
4582 if (type == TYPE_NOMATCH)
4583 n1 = !n1;
4584 }
4585 p_cpo = save_cpo;
4586 break;
4587
4588 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4589 }
4590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 rettv->v_type = VAR_NUMBER;
4594 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 }
4597
4598 return OK;
4599}
4600
4601/*
4602 * Handle fourth level expression:
4603 * + number addition
4604 * - number subtraction
4605 * . string concatenation
4606 *
4607 * "arg" must point to the first non-white of the expression.
4608 * "arg" is advanced to the next non-white after the recognized expression.
4609 *
4610 * Return OK or FAIL.
4611 */
4612 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 int evaluate;
4617{
Bram Moolenaar33570922005-01-25 22:26:29 +00004618 typval_T var2;
4619 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 int op;
4621 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004622#ifdef FEAT_FLOAT
4623 float_T f1 = 0, f2 = 0;
4624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 char_u *s1, *s2;
4626 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4627 char_u *p;
4628
4629 /*
4630 * Get the first variable.
4631 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004632 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return FAIL;
4634
4635 /*
4636 * Repeat computing, until no '+', '-' or '.' is following.
4637 */
4638 for (;;)
4639 {
4640 op = **arg;
4641 if (op != '+' && op != '-' && op != '.')
4642 break;
4643
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004644 if ((op != '+' || rettv->v_type != VAR_LIST)
4645#ifdef FEAT_FLOAT
4646 && (op == '.' || rettv->v_type != VAR_FLOAT)
4647#endif
4648 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 {
4650 /* For "list + ...", an illegal use of the first operand as
4651 * a number cannot be determined before evaluating the 2nd
4652 * operand: if this is also a list, all is ok.
4653 * For "something . ...", "something - ..." or "non-list + ...",
4654 * we know that the first operand needs to be a string or number
4655 * without evaluating the 2nd operand. So check before to avoid
4656 * side effects after an error. */
4657 if (evaluate && get_tv_string_chk(rettv) == NULL)
4658 {
4659 clear_tv(rettv);
4660 return FAIL;
4661 }
4662 }
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 /*
4665 * Get the second variable.
4666 */
4667 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004668 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return FAIL;
4672 }
4673
4674 if (evaluate)
4675 {
4676 /*
4677 * Compute the result.
4678 */
4679 if (op == '.')
4680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4682 s2 = get_tv_string_buf_chk(&var2, buf2);
4683 if (s2 == NULL) /* type error ? */
4684 {
4685 clear_tv(rettv);
4686 clear_tv(&var2);
4687 return FAIL;
4688 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004689 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 clear_tv(rettv);
4691 rettv->v_type = VAR_STRING;
4692 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004694 else if (op == '+' && rettv->v_type == VAR_LIST
4695 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004696 {
4697 /* concatenate Lists */
4698 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4699 &var3) == FAIL)
4700 {
4701 clear_tv(rettv);
4702 clear_tv(&var2);
4703 return FAIL;
4704 }
4705 clear_tv(rettv);
4706 *rettv = var3;
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 else
4709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 int error = FALSE;
4711
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 f1 = rettv->vval.v_float;
4716 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 else
4719#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721 n1 = get_tv_number_chk(rettv, &error);
4722 if (error)
4723 {
4724 /* This can only happen for "list + non-list". For
4725 * "non-list + ..." or "something - ...", we returned
4726 * before evaluating the 2nd operand. */
4727 clear_tv(rettv);
4728 return FAIL;
4729 }
4730#ifdef FEAT_FLOAT
4731 if (var2.v_type == VAR_FLOAT)
4732 f1 = n1;
4733#endif
4734 }
4735#ifdef FEAT_FLOAT
4736 if (var2.v_type == VAR_FLOAT)
4737 {
4738 f2 = var2.vval.v_float;
4739 n2 = 0;
4740 }
4741 else
4742#endif
4743 {
4744 n2 = get_tv_number_chk(&var2, &error);
4745 if (error)
4746 {
4747 clear_tv(rettv);
4748 clear_tv(&var2);
4749 return FAIL;
4750 }
4751#ifdef FEAT_FLOAT
4752 if (rettv->v_type == VAR_FLOAT)
4753 f2 = n2;
4754#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757
4758#ifdef FEAT_FLOAT
4759 /* If there is a float on either side the result is a float. */
4760 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4761 {
4762 if (op == '+')
4763 f1 = f1 + f2;
4764 else
4765 f1 = f1 - f2;
4766 rettv->v_type = VAR_FLOAT;
4767 rettv->vval.v_float = f1;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#endif
4771 {
4772 if (op == '+')
4773 n1 = n1 + n2;
4774 else
4775 n1 = n1 - n2;
4776 rettv->v_type = VAR_NUMBER;
4777 rettv->vval.v_number = n1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 }
4783 return OK;
4784}
4785
4786/*
4787 * Handle fifth level expression:
4788 * * number multiplication
4789 * / number division
4790 * % number modulo
4791 *
4792 * "arg" must point to the first non-white of the expression.
4793 * "arg" is advanced to the next non-white after the recognized expression.
4794 *
4795 * Return OK or FAIL.
4796 */
4797 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004802 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
Bram Moolenaar33570922005-01-25 22:26:29 +00004804 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 int op;
4806 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807#ifdef FEAT_FLOAT
4808 int use_float = FALSE;
4809 float_T f1 = 0, f2;
4810#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 /*
4814 * Get the first variable.
4815 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004816 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 return FAIL;
4818
4819 /*
4820 * Repeat computing, until no '*', '/' or '%' is following.
4821 */
4822 for (;;)
4823 {
4824 op = **arg;
4825 if (op != '*' && op != '/' && op != '%')
4826 break;
4827
4828 if (evaluate)
4829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830#ifdef FEAT_FLOAT
4831 if (rettv->v_type == VAR_FLOAT)
4832 {
4833 f1 = rettv->vval.v_float;
4834 use_float = TRUE;
4835 n1 = 0;
4836 }
4837 else
4838#endif
4839 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 if (error)
4842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else
4845 n1 = 0;
4846
4847 /*
4848 * Get the second variable.
4849 */
4850 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004851 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return FAIL;
4853
4854 if (evaluate)
4855 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856#ifdef FEAT_FLOAT
4857 if (var2.v_type == VAR_FLOAT)
4858 {
4859 if (!use_float)
4860 {
4861 f1 = n1;
4862 use_float = TRUE;
4863 }
4864 f2 = var2.vval.v_float;
4865 n2 = 0;
4866 }
4867 else
4868#endif
4869 {
4870 n2 = get_tv_number_chk(&var2, &error);
4871 clear_tv(&var2);
4872 if (error)
4873 return FAIL;
4874#ifdef FEAT_FLOAT
4875 if (use_float)
4876 f2 = n2;
4877#endif
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884#ifdef FEAT_FLOAT
4885 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 if (op == '*')
4888 f1 = f1 * f2;
4889 else if (op == '/')
4890 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891# ifdef VMS
4892 /* VMS crashes on divide by zero, work around it */
4893 if (f2 == 0.0)
4894 {
4895 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004896 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004898 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004899 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004900 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901 }
4902 else
4903 f1 = f1 / f2;
4904# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 /* We rely on the floating point library to handle divide
4906 * by zero to result in "inf" and not a crash. */
4907 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004912 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 return FAIL;
4914 }
4915 rettv->v_type = VAR_FLOAT;
4916 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 if (op == '*')
4922 n1 = n1 * n2;
4923 else if (op == '/')
4924 {
4925 if (n2 == 0) /* give an error message? */
4926 {
4927 if (n1 == 0)
4928 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4929 else if (n1 < 0)
4930 n1 = -0x7fffffffL;
4931 else
4932 n1 = 0x7fffffffL;
4933 }
4934 else
4935 n1 = n1 / n2;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004938 {
4939 if (n2 == 0) /* give an error message? */
4940 n1 = 0;
4941 else
4942 n1 = n1 % n2;
4943 }
4944 rettv->v_type = VAR_NUMBER;
4945 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948 }
4949
4950 return OK;
4951}
4952
4953/*
4954 * Handle sixth level expression:
4955 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004956 * "string" string constant
4957 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 * &option-name option value
4959 * @r register contents
4960 * identifier variable value
4961 * function() function call
4962 * $VAR environment variable
4963 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004964 * [expr, expr] List
4965 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 *
4967 * Also handle:
4968 * ! in front logical NOT
4969 * - in front unary minus
4970 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 * trailing [] subscript in String or List
4972 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 *
4974 * "arg" must point to the first non-white of the expression.
4975 * "arg" is advanced to the next non-white after the recognized expression.
4976 *
4977 * Return OK or FAIL.
4978 */
4979 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004980eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004984 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 long n;
4987 int len;
4988 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 char_u *start_leader, *end_leader;
4990 int ret = OK;
4991 char_u *alias;
4992
4993 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
4999 /*
5000 * Skip '!' and '-' characters. They are handled later.
5001 */
5002 start_leader = *arg;
5003 while (**arg == '!' || **arg == '-' || **arg == '+')
5004 *arg = skipwhite(*arg + 1);
5005 end_leader = *arg;
5006
5007 switch (**arg)
5008 {
5009 /*
5010 * Number constant.
5011 */
5012 case '0':
5013 case '1':
5014 case '2':
5015 case '3':
5016 case '4':
5017 case '5':
5018 case '6':
5019 case '7':
5020 case '8':
5021 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 {
5023#ifdef FEAT_FLOAT
5024 char_u *p = skipdigits(*arg + 1);
5025 int get_float = FALSE;
5026
5027 /* We accept a float when the format matches
5028 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005029 * strict to avoid backwards compatibility problems.
5030 * Don't look for a float after the "." operator, so that
5031 * ":let vers = 1.2.3" doesn't fail. */
5032 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 get_float = TRUE;
5035 p = skipdigits(p + 2);
5036 if (*p == 'e' || *p == 'E')
5037 {
5038 ++p;
5039 if (*p == '-' || *p == '+')
5040 ++p;
5041 if (!vim_isdigit(*p))
5042 get_float = FALSE;
5043 else
5044 p = skipdigits(p + 1);
5045 }
5046 if (ASCII_ISALPHA(*p) || *p == '.')
5047 get_float = FALSE;
5048 }
5049 if (get_float)
5050 {
5051 float_T f;
5052
5053 *arg += string2float(*arg, &f);
5054 if (evaluate)
5055 {
5056 rettv->v_type = VAR_FLOAT;
5057 rettv->vval.v_float = f;
5058 }
5059 }
5060 else
5061#endif
5062 {
5063 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5064 *arg += len;
5065 if (evaluate)
5066 {
5067 rettv->v_type = VAR_NUMBER;
5068 rettv->vval.v_number = n;
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * String constant: "string".
5076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 break;
5079
5080 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 break;
5085
5086 /*
5087 * List: [expr, expr]
5088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091
5092 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 * Dictionary: {key: val, key: val}
5094 */
5095 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5096 break;
5097
5098 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005099 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005101 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103
5104 /*
5105 * Environment variable: $VAR.
5106 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109
5110 /*
5111 * Register contents: @r.
5112 */
5113 case '@': ++*arg;
5114 if (evaluate)
5115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005117 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 if (**arg != NUL)
5120 ++*arg;
5121 break;
5122
5123 /*
5124 * nested expression: (expression).
5125 */
5126 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (**arg == ')')
5129 ++*arg;
5130 else if (ret == OK)
5131 {
5132 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 ret = FAIL;
5135 }
5136 break;
5137
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141
5142 if (ret == NOTDONE)
5143 {
5144 /*
5145 * Must be a variable or function name.
5146 * Can also be a curly-braces kind of name: {expr}.
5147 */
5148 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 if (alias != NULL)
5151 s = alias;
5152
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 ret = FAIL;
5155 else
5156 {
5157 if (**arg == '(') /* recursive! */
5158 {
5159 /* If "s" is the name of a variable of type VAR_FUNC
5160 * use its contents. */
5161 s = deref_func_name(s, &len);
5162
5163 /* Invoke the function. */
5164 ret = get_func_tv(s, len, rettv, arg,
5165 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005167
5168 /* If evaluate is FALSE rettv->v_type was not set in
5169 * get_func_tv, but it's needed in handle_subscript() to parse
5170 * what follows. So set it here. */
5171 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5172 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005173 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005174 rettv->v_type = VAR_FUNC;
5175 }
5176
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 /* Stop the expression evaluation when immediately
5178 * aborting on error, or when an interrupt occurred or
5179 * an exception was thrown but not caught. */
5180 if (aborting())
5181 {
5182 if (ret == OK)
5183 clear_tv(rettv);
5184 ret = FAIL;
5185 }
5186 }
5187 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005189 else
5190 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005192 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 }
5194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 *arg = skipwhite(*arg);
5196
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005197 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5198 * expr(expr). */
5199 if (ret == OK)
5200 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202 /*
5203 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5204 */
5205 if (ret == OK && evaluate && end_leader > start_leader)
5206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005208 int val = 0;
5209#ifdef FEAT_FLOAT
5210 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 if (rettv->v_type == VAR_FLOAT)
5213 f = rettv->vval.v_float;
5214 else
5215#endif
5216 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 clear_tv(rettv);
5220 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 else
5223 {
5224 while (end_leader > start_leader)
5225 {
5226 --end_leader;
5227 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005228 {
5229#ifdef FEAT_FLOAT
5230 if (rettv->v_type == VAR_FLOAT)
5231 f = !f;
5232 else
5233#endif
5234 val = !val;
5235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005236 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005237 {
5238#ifdef FEAT_FLOAT
5239 if (rettv->v_type == VAR_FLOAT)
5240 f = -f;
5241 else
5242#endif
5243 val = -val;
5244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246#ifdef FEAT_FLOAT
5247 if (rettv->v_type == VAR_FLOAT)
5248 {
5249 clear_tv(rettv);
5250 rettv->vval.v_float = f;
5251 }
5252 else
5253#endif
5254 {
5255 clear_tv(rettv);
5256 rettv->v_type = VAR_NUMBER;
5257 rettv->vval.v_number = val;
5258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261
5262 return ret;
5263}
5264
5265/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005266 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5267 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5269 */
5270 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005273 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276{
5277 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005278 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 long len = -1;
5281 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285 if (rettv->v_type == VAR_FUNC
5286#ifdef FEAT_FLOAT
5287 || rettv->v_type == VAR_FLOAT
5288#endif
5289 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 if (verbose)
5292 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 return FAIL;
5294 }
5295
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 /*
5299 * dict.name
5300 */
5301 key = *arg + 1;
5302 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5303 ;
5304 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 }
5308 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 /*
5311 * something[idx]
5312 *
5313 * Get the (first) variable from inside the [].
5314 */
5315 *arg = skipwhite(*arg + 1);
5316 if (**arg == ':')
5317 empty1 = TRUE;
5318 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5319 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5321 {
5322 /* not a number or string */
5323 clear_tv(&var1);
5324 return FAIL;
5325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326
5327 /*
5328 * Get the second variable from inside the [:].
5329 */
5330 if (**arg == ':')
5331 {
5332 range = TRUE;
5333 *arg = skipwhite(*arg + 1);
5334 if (**arg == ']')
5335 empty2 = TRUE;
5336 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 if (!empty1)
5339 clear_tv(&var1);
5340 return FAIL;
5341 }
5342 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5343 {
5344 /* not a number or string */
5345 if (!empty1)
5346 clear_tv(&var1);
5347 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 return FAIL;
5349 }
5350 }
5351
5352 /* Check for the ']'. */
5353 if (**arg != ']')
5354 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355 if (verbose)
5356 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 clear_tv(&var1);
5358 if (range)
5359 clear_tv(&var2);
5360 return FAIL;
5361 }
5362 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 }
5364
5365 if (evaluate)
5366 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 n1 = 0;
5368 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 n1 = get_tv_number(&var1);
5371 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 }
5373 if (range)
5374 {
5375 if (empty2)
5376 n2 = -1;
5377 else
5378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005379 n2 = get_tv_number(&var2);
5380 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 }
5382 }
5383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
5386 case VAR_NUMBER:
5387 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005388 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 len = (long)STRLEN(s);
5390 if (range)
5391 {
5392 /* The resulting variable is a substring. If the indexes
5393 * are out of range the result is empty. */
5394 if (n1 < 0)
5395 {
5396 n1 = len + n1;
5397 if (n1 < 0)
5398 n1 = 0;
5399 }
5400 if (n2 < 0)
5401 n2 = len + n2;
5402 else if (n2 >= len)
5403 n2 = len;
5404 if (n1 >= len || n2 < 0 || n1 > n2)
5405 s = NULL;
5406 else
5407 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5408 }
5409 else
5410 {
5411 /* The resulting variable is a string of a single
5412 * character. If the index is too big or negative the
5413 * result is empty. */
5414 if (n1 >= len || n1 < 0)
5415 s = NULL;
5416 else
5417 s = vim_strnsave(s + n1, 1);
5418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 clear_tv(rettv);
5420 rettv->v_type = VAR_STRING;
5421 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 break;
5423
5424 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 if (n1 < 0)
5427 n1 = len + n1;
5428 if (!empty1 && (n1 < 0 || n1 >= len))
5429 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005430 /* For a range we allow invalid values and return an empty
5431 * list. A list index out of range is an error. */
5432 if (!range)
5433 {
5434 if (verbose)
5435 EMSGN(_(e_listidx), n1);
5436 return FAIL;
5437 }
5438 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440 if (range)
5441 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005442 list_T *l;
5443 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444
5445 if (n2 < 0)
5446 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005447 else if (n2 >= len)
5448 n2 = len - 1;
5449 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005450 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 l = list_alloc();
5452 if (l == NULL)
5453 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 n1 <= n2; ++n1)
5456 {
5457 if (list_append_tv(l, &item->li_tv) == FAIL)
5458 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005459 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 return FAIL;
5461 }
5462 item = item->li_next;
5463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(rettv);
5465 rettv->v_type = VAR_LIST;
5466 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005467 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 else
5470 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005471 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 clear_tv(rettv);
5473 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476
5477 case VAR_DICT:
5478 if (range)
5479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005480 if (verbose)
5481 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 if (len == -1)
5483 clear_tv(&var1);
5484 return FAIL;
5485 }
5486 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488
5489 if (len == -1)
5490 {
5491 key = get_tv_string(&var1);
5492 if (*key == NUL)
5493 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005494 if (verbose)
5495 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 clear_tv(&var1);
5497 return FAIL;
5498 }
5499 }
5500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005501 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005503 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005504 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005505 if (len == -1)
5506 clear_tv(&var1);
5507 if (item == NULL)
5508 return FAIL;
5509
5510 copy_tv(&item->di_tv, &var1);
5511 clear_tv(rettv);
5512 *rettv = var1;
5513 }
5514 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 }
5516 }
5517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 return OK;
5519}
5520
5521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 * Get an option value.
5523 * "arg" points to the '&' or '+' before the option name.
5524 * "arg" is advanced to character after the option name.
5525 * Return OK or FAIL.
5526 */
5527 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005530 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 int evaluate;
5532{
5533 char_u *option_end;
5534 long numval;
5535 char_u *stringval;
5536 int opt_type;
5537 int c;
5538 int working = (**arg == '+'); /* has("+option") */
5539 int ret = OK;
5540 int opt_flags;
5541
5542 /*
5543 * Isolate the option name and find its value.
5544 */
5545 option_end = find_option_end(arg, &opt_flags);
5546 if (option_end == NULL)
5547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 EMSG2(_("E112: Option name missing: %s"), *arg);
5550 return FAIL;
5551 }
5552
5553 if (!evaluate)
5554 {
5555 *arg = option_end;
5556 return OK;
5557 }
5558
5559 c = *option_end;
5560 *option_end = NUL;
5561 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
5564 if (opt_type == -3) /* invalid name */
5565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 EMSG2(_("E113: Unknown option: %s"), *arg);
5568 ret = FAIL;
5569 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
5572 if (opt_type == -2) /* hidden string option */
5573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 rettv->v_type = VAR_STRING;
5575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 else if (opt_type == -1) /* hidden number option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_NUMBER;
5580 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else if (opt_type == 1) /* number option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_NUMBER;
5585 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else /* string option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_STRING;
5590 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 }
5593 else if (working && (opt_type == -2 || opt_type == -1))
5594 ret = FAIL;
5595
5596 *option_end = c; /* put back for error messages */
5597 *arg = option_end;
5598
5599 return ret;
5600}
5601
5602/*
5603 * Allocate a variable for a string constant.
5604 * Return OK or FAIL.
5605 */
5606 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 int evaluate;
5611{
5612 char_u *p;
5613 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 int extra = 0;
5615
5616 /*
5617 * Find the end of the string, skipping backslashed characters.
5618 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005619 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 if (*p == '\\' && p[1] != NUL)
5622 {
5623 ++p;
5624 /* A "\<x>" form occupies at least 4 characters, and produces up
5625 * to 6 characters: reserve space for 2 extra */
5626 if (*p == '<')
5627 extra += 2;
5628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630
5631 if (*p != '"')
5632 {
5633 EMSG2(_("E114: Missing quote: %s"), *arg);
5634 return FAIL;
5635 }
5636
5637 /* If only parsing, set *arg and return here */
5638 if (!evaluate)
5639 {
5640 *arg = p + 1;
5641 return OK;
5642 }
5643
5644 /*
5645 * Copy the string into allocated memory, handling backslashed
5646 * characters.
5647 */
5648 name = alloc((unsigned)(p - *arg + extra));
5649 if (name == NULL)
5650 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 rettv->v_type = VAR_STRING;
5652 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 if (*p == '\\')
5657 {
5658 switch (*++p)
5659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 case 'b': *name++ = BS; ++p; break;
5661 case 'e': *name++ = ESC; ++p; break;
5662 case 'f': *name++ = FF; ++p; break;
5663 case 'n': *name++ = NL; ++p; break;
5664 case 'r': *name++ = CAR; ++p; break;
5665 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
5667 case 'X': /* hex: "\x1", "\x12" */
5668 case 'x':
5669 case 'u': /* Unicode: "\u0023" */
5670 case 'U':
5671 if (vim_isxdigit(p[1]))
5672 {
5673 int n, nr;
5674 int c = toupper(*p);
5675
5676 if (c == 'X')
5677 n = 2;
5678 else
5679 n = 4;
5680 nr = 0;
5681 while (--n >= 0 && vim_isxdigit(p[1]))
5682 {
5683 ++p;
5684 nr = (nr << 4) + hex2nr(*p);
5685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687#ifdef FEAT_MBYTE
5688 /* For "\u" store the number according to
5689 * 'encoding'. */
5690 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else
5693#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 break;
5697
5698 /* octal: "\1", "\12", "\123" */
5699 case '0':
5700 case '1':
5701 case '2':
5702 case '3':
5703 case '4':
5704 case '5':
5705 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 case '7': *name = *p++ - '0';
5707 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name = (*name << 3) + *p++ - '0';
5710 if (*p >= '0' && *p <= '7')
5711 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 break;
5715
5716 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 if (extra != 0)
5719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 break;
5722 }
5723 /* FALLTHROUGH */
5724
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 }
5728 }
5729 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 *arg = p + 1;
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 return OK;
5737}
5738
5739/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 * Return OK or FAIL.
5742 */
5743 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005744get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 int evaluate;
5748{
5749 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 int reduce = 0;
5752
5753 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 */
5756 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 break;
5762 ++reduce;
5763 ++p;
5764 }
5765 }
5766
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 return FAIL;
5771 }
5772
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 if (!evaluate)
5775 {
5776 *arg = p + 1;
5777 return OK;
5778 }
5779
5780 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 */
5783 str = alloc((unsigned)((p - *arg) - reduce));
5784 if (str == NULL)
5785 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 rettv->v_type = VAR_STRING;
5787 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 break;
5795 ++p;
5796 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 *arg = p + 1;
5801
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 return OK;
5803}
5804
5805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 * Allocate a variable for a List and fill it from "*arg".
5807 * Return OK or FAIL.
5808 */
5809 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005810get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 int evaluate;
5814{
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 list_T *l = NULL;
5816 typval_T tv;
5817 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818
5819 if (evaluate)
5820 {
5821 l = list_alloc();
5822 if (l == NULL)
5823 return FAIL;
5824 }
5825
5826 *arg = skipwhite(*arg + 1);
5827 while (**arg != ']' && **arg != NUL)
5828 {
5829 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5830 goto failret;
5831 if (evaluate)
5832 {
5833 item = listitem_alloc();
5834 if (item != NULL)
5835 {
5836 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005837 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 list_append(l, item);
5839 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005840 else
5841 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 }
5843
5844 if (**arg == ']')
5845 break;
5846 if (**arg != ',')
5847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 goto failret;
5850 }
5851 *arg = skipwhite(*arg + 1);
5852 }
5853
5854 if (**arg != ']')
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857failret:
5858 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005859 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 return FAIL;
5861 }
5862
5863 *arg = skipwhite(*arg + 1);
5864 if (evaluate)
5865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005866 rettv->v_type = VAR_LIST;
5867 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868 ++l->lv_refcount;
5869 }
5870
5871 return OK;
5872}
5873
5874/*
5875 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005876 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005878 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879list_alloc()
5880{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005881 list_T *l;
5882
5883 l = (list_T *)alloc_clear(sizeof(list_T));
5884 if (l != NULL)
5885 {
5886 /* Prepend the list to the list of lists for garbage collection. */
5887 if (first_list != NULL)
5888 first_list->lv_used_prev = l;
5889 l->lv_used_prev = NULL;
5890 l->lv_used_next = first_list;
5891 first_list = l;
5892 }
5893 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894}
5895
5896/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005897 * Allocate an empty list for a return value.
5898 * Returns OK or FAIL.
5899 */
5900 static int
5901rettv_list_alloc(rettv)
5902 typval_T *rettv;
5903{
5904 list_T *l = list_alloc();
5905
5906 if (l == NULL)
5907 return FAIL;
5908
5909 rettv->vval.v_list = l;
5910 rettv->v_type = VAR_LIST;
5911 ++l->lv_refcount;
5912 return OK;
5913}
5914
5915/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 * Unreference a list: decrement the reference count and free it when it
5917 * becomes zero.
5918 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005919 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005923 if (l != NULL && --l->lv_refcount <= 0)
5924 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925}
5926
5927/*
5928 * Free a list, including all items it points to.
5929 * Ignores the reference count.
5930 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005931 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005932list_free(l, recurse)
5933 list_T *l;
5934 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935{
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005938 /* Remove the list from the list of lists for garbage collection. */
5939 if (l->lv_used_prev == NULL)
5940 first_list = l->lv_used_next;
5941 else
5942 l->lv_used_prev->lv_used_next = l->lv_used_next;
5943 if (l->lv_used_next != NULL)
5944 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5945
Bram Moolenaard9fba312005-06-26 22:34:35 +00005946 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005948 /* Remove the item before deleting it. */
5949 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005950 if (recurse || (item->li_tv.v_type != VAR_LIST
5951 && item->li_tv.v_type != VAR_DICT))
5952 clear_tv(&item->li_tv);
5953 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 }
5955 vim_free(l);
5956}
5957
5958/*
5959 * Allocate a list item.
5960 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005961 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962listitem_alloc()
5963{
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965}
5966
5967/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005968 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 */
5970 static void
5971listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005974 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 vim_free(item);
5976}
5977
5978/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005979 * Remove a list item from a List and free it. Also clears the value.
5980 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005981 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005982listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 list_T *l;
5984 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005985{
5986 list_remove(l, item, item);
5987 listitem_free(item);
5988}
5989
5990/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 * Get the number of items in a list.
5992 */
5993 static long
5994list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 if (l == NULL)
5998 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005999 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000}
6001
6002/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003 * Return TRUE when two lists have exactly the same values.
6004 */
6005 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006006list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 list_T *l1;
6008 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011{
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006014 if (l1 == NULL || l2 == NULL)
6015 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006016 if (l1 == l2)
6017 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006018 if (list_len(l1) != list_len(l2))
6019 return FALSE;
6020
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006021 for (item1 = l1->lv_first, item2 = l2->lv_first;
6022 item1 != NULL && item2 != NULL;
6023 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006024 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025 return FALSE;
6026 return item1 == NULL && item2 == NULL;
6027}
6028
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006029#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6030 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006031/*
6032 * Return the dictitem that an entry in a hashtable points to.
6033 */
6034 dictitem_T *
6035dict_lookup(hi)
6036 hashitem_T *hi;
6037{
6038 return HI2DI(hi);
6039}
6040#endif
6041
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006042/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006043 * Return TRUE when two dictionaries have exactly the same key/values.
6044 */
6045 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006046dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 dict_T *d1;
6048 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006050 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051{
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 hashitem_T *hi;
6053 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006054 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006055
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006056 if (d1 == NULL || d2 == NULL)
6057 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006058 if (d1 == d2)
6059 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060 if (dict_len(d1) != dict_len(d2))
6061 return FALSE;
6062
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006063 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006066 if (!HASHITEM_EMPTY(hi))
6067 {
6068 item2 = dict_find(d2, hi->hi_key, -1);
6069 if (item2 == NULL)
6070 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006072 return FALSE;
6073 --todo;
6074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 }
6076 return TRUE;
6077}
6078
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079static int tv_equal_recurse_limit;
6080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006081/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006083 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006084 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085 */
6086 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006087tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 typval_T *tv1;
6089 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090 int ic; /* ignore case */
6091 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092{
6093 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006094 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006096 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006098 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006099 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006101 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 * recursiveness to a limit. We guess they are equal then.
6103 * A fixed limit has the problem of still taking an awful long time.
6104 * Reduce the limit every time running into it. That should work fine for
6105 * deeply linked structures that are not recursively linked and catch
6106 * recursiveness quickly. */
6107 if (!recursive)
6108 tv_equal_recurse_limit = 1000;
6109 if (recursive_cnt >= tv_equal_recurse_limit)
6110 {
6111 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006112 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114
6115 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006117 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 ++recursive_cnt;
6119 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6120 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006121 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122
6123 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 ++recursive_cnt;
6125 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6126 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006127 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128
6129 case VAR_FUNC:
6130 return (tv1->vval.v_string != NULL
6131 && tv2->vval.v_string != NULL
6132 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6133
6134 case VAR_NUMBER:
6135 return tv1->vval.v_number == tv2->vval.v_number;
6136
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006137#ifdef FEAT_FLOAT
6138 case VAR_FLOAT:
6139 return tv1->vval.v_float == tv2->vval.v_float;
6140#endif
6141
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006142 case VAR_STRING:
6143 s1 = get_tv_string_buf(tv1, buf1);
6144 s2 = get_tv_string_buf(tv2, buf2);
6145 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006146 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147
6148 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006149 return TRUE;
6150}
6151
6152/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 * Locate item with index "n" in list "l" and return it.
6154 * A negative index is counted from the end; -1 is the last item.
6155 * Returns NULL when "n" is out of range.
6156 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006157 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 long n;
6161{
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163 long idx;
6164
6165 if (l == NULL)
6166 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006167
6168 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006170 n = l->lv_len + n;
6171
6172 /* Check for index out of range. */
6173 if (n < 0 || n >= l->lv_len)
6174 return NULL;
6175
6176 /* When there is a cached index may start search from there. */
6177 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006179 if (n < l->lv_idx / 2)
6180 {
6181 /* closest to the start of the list */
6182 item = l->lv_first;
6183 idx = 0;
6184 }
6185 else if (n > (l->lv_idx + l->lv_len) / 2)
6186 {
6187 /* closest to the end of the list */
6188 item = l->lv_last;
6189 idx = l->lv_len - 1;
6190 }
6191 else
6192 {
6193 /* closest to the cached index */
6194 item = l->lv_idx_item;
6195 idx = l->lv_idx;
6196 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 }
6198 else
6199 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 if (n < l->lv_len / 2)
6201 {
6202 /* closest to the start of the list */
6203 item = l->lv_first;
6204 idx = 0;
6205 }
6206 else
6207 {
6208 /* closest to the end of the list */
6209 item = l->lv_last;
6210 idx = l->lv_len - 1;
6211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006213
6214 while (n > idx)
6215 {
6216 /* search forward */
6217 item = item->li_next;
6218 ++idx;
6219 }
6220 while (n < idx)
6221 {
6222 /* search backward */
6223 item = item->li_prev;
6224 --idx;
6225 }
6226
6227 /* cache the used index */
6228 l->lv_idx = idx;
6229 l->lv_idx_item = item;
6230
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 return item;
6232}
6233
6234/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006235 * Get list item "l[idx]" as a number.
6236 */
6237 static long
6238list_find_nr(l, idx, errorp)
6239 list_T *l;
6240 long idx;
6241 int *errorp; /* set to TRUE when something wrong */
6242{
6243 listitem_T *li;
6244
6245 li = list_find(l, idx);
6246 if (li == NULL)
6247 {
6248 if (errorp != NULL)
6249 *errorp = TRUE;
6250 return -1L;
6251 }
6252 return get_tv_number_chk(&li->li_tv, errorp);
6253}
6254
6255/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006256 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6257 */
6258 char_u *
6259list_find_str(l, idx)
6260 list_T *l;
6261 long idx;
6262{
6263 listitem_T *li;
6264
6265 li = list_find(l, idx - 1);
6266 if (li == NULL)
6267 {
6268 EMSGN(_(e_listidx), idx);
6269 return NULL;
6270 }
6271 return get_tv_string(&li->li_tv);
6272}
6273
6274/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275 * Locate "item" list "l" and return its index.
6276 * Returns -1 when "item" is not in the list.
6277 */
6278 static long
6279list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 list_T *l;
6281 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006282{
6283 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006285
6286 if (l == NULL)
6287 return -1;
6288 idx = 0;
6289 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6290 ++idx;
6291 if (li == NULL)
6292 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006293 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006294}
6295
6296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 * Append item "item" to the end of list "l".
6298 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006299 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006301 list_T *l;
6302 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303{
6304 if (l->lv_last == NULL)
6305 {
6306 /* empty list */
6307 l->lv_first = item;
6308 l->lv_last = item;
6309 item->li_prev = NULL;
6310 }
6311 else
6312 {
6313 l->lv_last->li_next = item;
6314 item->li_prev = l->lv_last;
6315 l->lv_last = item;
6316 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006317 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 item->li_next = NULL;
6319}
6320
6321/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006323 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006325 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 list_T *l;
6328 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006330 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331
Bram Moolenaar05159a02005-02-26 23:04:13 +00006332 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006334 copy_tv(tv, &li->li_tv);
6335 list_append(l, li);
6336 return OK;
6337}
6338
6339/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006340 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006341 * Return FAIL when out of memory.
6342 */
6343 int
6344list_append_dict(list, dict)
6345 list_T *list;
6346 dict_T *dict;
6347{
6348 listitem_T *li = listitem_alloc();
6349
6350 if (li == NULL)
6351 return FAIL;
6352 li->li_tv.v_type = VAR_DICT;
6353 li->li_tv.v_lock = 0;
6354 li->li_tv.vval.v_dict = dict;
6355 list_append(list, li);
6356 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357 return OK;
6358}
6359
6360/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006361 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006362 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006363 * Returns FAIL when out of memory.
6364 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006365 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006366list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006367 list_T *l;
6368 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006369 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006370{
6371 listitem_T *li = listitem_alloc();
6372
6373 if (li == NULL)
6374 return FAIL;
6375 list_append(l, li);
6376 li->li_tv.v_type = VAR_STRING;
6377 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006378 if (str == NULL)
6379 li->li_tv.vval.v_string = NULL;
6380 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006381 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 return FAIL;
6383 return OK;
6384}
6385
6386/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006387 * Append "n" to list "l".
6388 * Returns FAIL when out of memory.
6389 */
6390 static int
6391list_append_number(l, n)
6392 list_T *l;
6393 varnumber_T n;
6394{
6395 listitem_T *li;
6396
6397 li = listitem_alloc();
6398 if (li == NULL)
6399 return FAIL;
6400 li->li_tv.v_type = VAR_NUMBER;
6401 li->li_tv.v_lock = 0;
6402 li->li_tv.vval.v_number = n;
6403 list_append(l, li);
6404 return OK;
6405}
6406
6407/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409 * If "item" is NULL append at the end.
6410 * Return FAIL when out of memory.
6411 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006412 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 list_T *l;
6415 typval_T *tv;
6416 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417{
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419
6420 if (ni == NULL)
6421 return FAIL;
6422 copy_tv(tv, &ni->li_tv);
6423 if (item == NULL)
6424 /* Append new item at end of list. */
6425 list_append(l, ni);
6426 else
6427 {
6428 /* Insert new item before existing item. */
6429 ni->li_prev = item->li_prev;
6430 ni->li_next = item;
6431 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006434 ++l->lv_idx;
6435 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006437 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006439 l->lv_idx_item = NULL;
6440 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006442 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 }
6444 return OK;
6445}
6446
6447/*
6448 * Extend "l1" with "l2".
6449 * If "bef" is NULL append at the end, otherwise insert before this item.
6450 * Returns FAIL when out of memory.
6451 */
6452 static int
6453list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 list_T *l1;
6455 list_T *l2;
6456 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006459 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006461 /* We also quit the loop when we have inserted the original item count of
6462 * the list, avoid a hang when we extend a list with itself. */
6463 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6465 return FAIL;
6466 return OK;
6467}
6468
6469/*
6470 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6471 * Return FAIL when out of memory.
6472 */
6473 static int
6474list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 list_T *l1;
6476 list_T *l2;
6477 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478{
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006481 if (l1 == NULL || l2 == NULL)
6482 return FAIL;
6483
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006485 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486 if (l == NULL)
6487 return FAIL;
6488 tv->v_type = VAR_LIST;
6489 tv->vval.v_list = l;
6490
6491 /* append all items from the second list */
6492 return list_extend(l, l2, NULL);
6493}
6494
6495/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499 * Returns NULL when out of memory.
6500 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006502list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006505 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506{
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 list_T *copy;
6508 listitem_T *item;
6509 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510
6511 if (orig == NULL)
6512 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513
6514 copy = list_alloc();
6515 if (copy != NULL)
6516 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 if (copyID != 0)
6518 {
6519 /* Do this before adding the items, because one of the items may
6520 * refer back to this list. */
6521 orig->lv_copyID = copyID;
6522 orig->lv_copylist = copy;
6523 }
6524 for (item = orig->lv_first; item != NULL && !got_int;
6525 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 {
6527 ni = listitem_alloc();
6528 if (ni == NULL)
6529 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006530 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 {
6532 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6533 {
6534 vim_free(ni);
6535 break;
6536 }
6537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006539 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 list_append(copy, ni);
6541 }
6542 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 if (item != NULL)
6544 {
6545 list_unref(copy);
6546 copy = NULL;
6547 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 }
6549
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 return copy;
6551}
6552
6553/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006555 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006557 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006558list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 list_T *l;
6560 listitem_T *item;
6561 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562{
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 /* notify watchers */
6566 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 list_fix_watch(l, ip);
6570 if (ip == item2)
6571 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573
6574 if (item2->li_next == NULL)
6575 l->lv_last = item->li_prev;
6576 else
6577 item2->li_next->li_prev = item->li_prev;
6578 if (item->li_prev == NULL)
6579 l->lv_first = item2->li_next;
6580 else
6581 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006582 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583}
6584
6585/*
6586 * Return an allocated string with the string representation of a list.
6587 * May return NULL.
6588 */
6589 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006592 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593{
6594 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
6596 if (tv->vval.v_list == NULL)
6597 return NULL;
6598 ga_init2(&ga, (int)sizeof(char), 80);
6599 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006600 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 {
6602 vim_free(ga.ga_data);
6603 return NULL;
6604 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 ga_append(&ga, ']');
6606 ga_append(&ga, NUL);
6607 return (char_u *)ga.ga_data;
6608}
6609
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006610typedef struct join_S {
6611 char_u *s;
6612 char_u *tofree;
6613} join_T;
6614
6615 static int
6616list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6617 garray_T *gap; /* to store the result in */
6618 list_T *l;
6619 char_u *sep;
6620 int echo_style;
6621 int copyID;
6622 garray_T *join_gap; /* to keep each list item string */
6623{
6624 int i;
6625 join_T *p;
6626 int len;
6627 int sumlen = 0;
6628 int first = TRUE;
6629 char_u *tofree;
6630 char_u numbuf[NUMBUFLEN];
6631 listitem_T *item;
6632 char_u *s;
6633
6634 /* Stringify each item in the list. */
6635 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6636 {
6637 if (echo_style)
6638 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6639 else
6640 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6641 if (s == NULL)
6642 return FAIL;
6643
6644 len = (int)STRLEN(s);
6645 sumlen += len;
6646
6647 ga_grow(join_gap, 1);
6648 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6649 if (tofree != NULL || s != numbuf)
6650 {
6651 p->s = s;
6652 p->tofree = tofree;
6653 }
6654 else
6655 {
6656 p->s = vim_strnsave(s, len);
6657 p->tofree = p->s;
6658 }
6659
6660 line_breakcheck();
6661 }
6662
6663 /* Allocate result buffer with its total size, avoid re-allocation and
6664 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6665 if (join_gap->ga_len >= 2)
6666 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6667 if (ga_grow(gap, sumlen + 2) == FAIL)
6668 return FAIL;
6669
6670 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6671 {
6672 if (first)
6673 first = FALSE;
6674 else
6675 ga_concat(gap, sep);
6676 p = ((join_T *)join_gap->ga_data) + i;
6677
6678 if (p->s != NULL)
6679 ga_concat(gap, p->s);
6680 line_breakcheck();
6681 }
6682
6683 return OK;
6684}
6685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006688 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006690 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006692list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006694 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006696 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006697 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699 garray_T join_ga;
6700 int retval;
6701 join_T *p;
6702 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6705 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6706
6707 /* Dispose each item in join_ga. */
6708 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710 p = (join_T *)join_ga.ga_data;
6711 for (i = 0; i < join_ga.ga_len; ++i)
6712 {
6713 vim_free(p->tofree);
6714 ++p;
6715 }
6716 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718
6719 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006720}
6721
6722/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006723 * Garbage collection for lists and dictionaries.
6724 *
6725 * We use reference counts to be able to free most items right away when they
6726 * are no longer used. But for composite items it's possible that it becomes
6727 * unused while the reference count is > 0: When there is a recursive
6728 * reference. Example:
6729 * :let l = [1, 2, 3]
6730 * :let d = {9: l}
6731 * :let l[1] = d
6732 *
6733 * Since this is quite unusual we handle this with garbage collection: every
6734 * once in a while find out which lists and dicts are not referenced from any
6735 * variable.
6736 *
6737 * Here is a good reference text about garbage collection (refers to Python
6738 * but it applies to all reference-counting mechanisms):
6739 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006740 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741
6742/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 * Do garbage collection for lists and dicts.
6744 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006745 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 int
6747garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006748{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006749 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006750 buf_T *buf;
6751 win_T *wp;
6752 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006753 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006754 int did_free;
6755 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006756#ifdef FEAT_WINDOWS
6757 tabpage_T *tp;
6758#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006759
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006760 /* Only do this once. */
6761 want_garbage_collect = FALSE;
6762 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006763 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006764
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006765 /* We advance by two because we add one for items referenced through
6766 * previous_funccal. */
6767 current_copyID += COPYID_INC;
6768 copyID = current_copyID;
6769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /*
6771 * 1. Go through all accessible variables and mark all lists and dicts
6772 * with copyID.
6773 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006774
6775 /* Don't free variables in the previous_funccal list unless they are only
6776 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006777 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006778 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6779 {
6780 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6781 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6782 }
6783
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 /* script-local variables */
6785 for (i = 1; i <= ga_scripts.ga_len; ++i)
6786 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6787
6788 /* buffer-local variables */
6789 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006790 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791
6792 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006793 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006794 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006795#ifdef FEAT_AUTOCMD
6796 if (aucmd_win != NULL)
6797 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6798#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006800#ifdef FEAT_WINDOWS
6801 /* tabpage-local variables */
6802 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006803 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006804#endif
6805
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006806 /* global variables */
6807 set_ref_in_ht(&globvarht, copyID);
6808
6809 /* function-local variables */
6810 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6811 {
6812 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6813 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6814 }
6815
Bram Moolenaard812df62008-11-09 12:46:09 +00006816 /* v: vars */
6817 set_ref_in_ht(&vimvarht, copyID);
6818
Bram Moolenaar1dced572012-04-05 16:54:08 +02006819#ifdef FEAT_LUA
6820 set_ref_in_lua(copyID);
6821#endif
6822
Bram Moolenaardb913952012-06-29 12:54:53 +02006823#ifdef FEAT_PYTHON
6824 set_ref_in_python(copyID);
6825#endif
6826
6827#ifdef FEAT_PYTHON3
6828 set_ref_in_python3(copyID);
6829#endif
6830
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006831 /*
6832 * 2. Free lists and dictionaries that are not referenced.
6833 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006834 did_free = free_unref_items(copyID);
6835
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006836 /*
6837 * 3. Check if any funccal can be freed now.
6838 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006839 for (pfc = &previous_funccal; *pfc != NULL; )
6840 {
6841 if (can_free_funccal(*pfc, copyID))
6842 {
6843 fc = *pfc;
6844 *pfc = fc->caller;
6845 free_funccal(fc, TRUE);
6846 did_free = TRUE;
6847 did_free_funccal = TRUE;
6848 }
6849 else
6850 pfc = &(*pfc)->caller;
6851 }
6852 if (did_free_funccal)
6853 /* When a funccal was freed some more items might be garbage
6854 * collected, so run again. */
6855 (void)garbage_collect();
6856
6857 return did_free;
6858}
6859
6860/*
6861 * Free lists and dictionaries that are no longer referenced.
6862 */
6863 static int
6864free_unref_items(copyID)
6865 int copyID;
6866{
6867 dict_T *dd;
6868 list_T *ll;
6869 int did_free = FALSE;
6870
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873 */
6874 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006877 /* Free the Dictionary and ordinary items it contains, but don't
6878 * recurse into Lists and Dictionaries, they will be in the list
6879 * of dicts or list of lists. */
6880 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 did_free = TRUE;
6882
6883 /* restart, next dict may also have been freed */
6884 dd = first_dict;
6885 }
6886 else
6887 dd = dd->dv_used_next;
6888
6889 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890 * Go through the list of lists and free items without the copyID.
6891 * But don't free a list that has a watcher (used in a for loop), these
6892 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 */
6894 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6896 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006898 /* Free the List and ordinary items it contains, but don't recurse
6899 * into Lists and Dictionaries, they will be in the list of dicts
6900 * or list of lists. */
6901 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 did_free = TRUE;
6903
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006904 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905 ll = first_list;
6906 }
6907 else
6908 ll = ll->lv_used_next;
6909
6910 return did_free;
6911}
6912
6913/*
6914 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6915 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006916 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917set_ref_in_ht(ht, copyID)
6918 hashtab_T *ht;
6919 int copyID;
6920{
6921 int todo;
6922 hashitem_T *hi;
6923
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006924 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 for (hi = ht->ht_array; todo > 0; ++hi)
6926 if (!HASHITEM_EMPTY(hi))
6927 {
6928 --todo;
6929 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6930 }
6931}
6932
6933/*
6934 * Mark all lists and dicts referenced through list "l" with "copyID".
6935 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006936 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937set_ref_in_list(l, copyID)
6938 list_T *l;
6939 int copyID;
6940{
6941 listitem_T *li;
6942
6943 for (li = l->lv_first; li != NULL; li = li->li_next)
6944 set_ref_in_item(&li->li_tv, copyID);
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6949 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006950 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951set_ref_in_item(tv, copyID)
6952 typval_T *tv;
6953 int copyID;
6954{
6955 dict_T *dd;
6956 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006957
6958 switch (tv->v_type)
6959 {
6960 case VAR_DICT:
6961 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006962 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 /* Didn't see this dict yet. */
6965 dd->dv_copyID = copyID;
6966 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006969
6970 case VAR_LIST:
6971 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006972 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006973 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 /* Didn't see this list yet. */
6975 ll->lv_copyID = copyID;
6976 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006979 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006981}
6982
6983/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006984 * Allocate an empty header for a dictionary.
6985 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006986 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006987dict_alloc()
6988{
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006990
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006992 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006993 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006994 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 if (first_dict != NULL)
6996 first_dict->dv_used_prev = d;
6997 d->dv_used_next = first_dict;
6998 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006999 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000
Bram Moolenaar33570922005-01-25 22:26:29 +00007001 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007002 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007003 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007004 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007005 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007006 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007007 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008}
7009
7010/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007011 * Allocate an empty dict for a return value.
7012 * Returns OK or FAIL.
7013 */
7014 static int
7015rettv_dict_alloc(rettv)
7016 typval_T *rettv;
7017{
7018 dict_T *d = dict_alloc();
7019
7020 if (d == NULL)
7021 return FAIL;
7022
7023 rettv->vval.v_dict = d;
7024 rettv->v_type = VAR_DICT;
7025 ++d->dv_refcount;
7026 return OK;
7027}
7028
7029
7030/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031 * Unreference a Dictionary: decrement the reference count and free it when it
7032 * becomes zero.
7033 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007034 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007037{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007038 if (d != NULL && --d->dv_refcount <= 0)
7039 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040}
7041
7042/*
7043 * Free a Dictionary, including all items it contains.
7044 * Ignores the reference count.
7045 */
7046 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007047dict_free(d, recurse)
7048 dict_T *d;
7049 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007051 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007052 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007053 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 /* Remove the dict from the list of dicts for garbage collection. */
7056 if (d->dv_used_prev == NULL)
7057 first_dict = d->dv_used_next;
7058 else
7059 d->dv_used_prev->dv_used_next = d->dv_used_next;
7060 if (d->dv_used_next != NULL)
7061 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7062
7063 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007064 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007065 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007067 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007068 if (!HASHITEM_EMPTY(hi))
7069 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007070 /* Remove the item before deleting it, just in case there is
7071 * something recursive causing trouble. */
7072 di = HI2DI(hi);
7073 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007074 if (recurse || (di->di_tv.v_type != VAR_LIST
7075 && di->di_tv.v_type != VAR_DICT))
7076 clear_tv(&di->di_tv);
7077 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078 --todo;
7079 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007081 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007082 vim_free(d);
7083}
7084
7085/*
7086 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007087 * The "key" is copied to the new item.
7088 * Note that the value of the item "di_tv" still needs to be initialized!
7089 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007091 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092dictitem_alloc(key)
7093 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094{
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007097 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007098 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007100 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007101 di->di_flags = 0;
7102 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007103 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104}
7105
7106/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 * Make a copy of a Dictionary item.
7108 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112{
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007115 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7116 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 if (di != NULL)
7118 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007120 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121 copy_tv(&org->di_tv, &di->di_tv);
7122 }
7123 return di;
7124}
7125
7126/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 * Remove item "item" from Dictionary "dict" and free it.
7128 */
7129 static void
7130dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 dict_T *dict;
7132 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133{
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007135
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007137 if (HASHITEM_EMPTY(hi))
7138 EMSG2(_(e_intern2), "dictitem_remove()");
7139 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007140 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 dictitem_free(item);
7142}
7143
7144/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007145 * Free a dict item. Also clears the value.
7146 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007147 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007150{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007151 clear_tv(&item->di_tv);
7152 vim_free(item);
7153}
7154
7155/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007156 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7157 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159 * Returns NULL when out of memory.
7160 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007162dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007165 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007166{
Bram Moolenaar33570922005-01-25 22:26:29 +00007167 dict_T *copy;
7168 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007169 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007170 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171
7172 if (orig == NULL)
7173 return NULL;
7174
7175 copy = dict_alloc();
7176 if (copy != NULL)
7177 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 if (copyID != 0)
7179 {
7180 orig->dv_copyID = copyID;
7181 orig->dv_copydict = copy;
7182 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007183 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007187 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 --todo;
7189
7190 di = dictitem_alloc(hi->hi_key);
7191 if (di == NULL)
7192 break;
7193 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007194 {
7195 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7196 copyID) == FAIL)
7197 {
7198 vim_free(di);
7199 break;
7200 }
7201 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 else
7203 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7204 if (dict_add(copy, di) == FAIL)
7205 {
7206 dictitem_free(di);
7207 break;
7208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007210 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007213 if (todo > 0)
7214 {
7215 dict_unref(copy);
7216 copy = NULL;
7217 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007218 }
7219
7220 return copy;
7221}
7222
7223/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007225 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007227 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007229 dict_T *d;
7230 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231{
Bram Moolenaar33570922005-01-25 22:26:29 +00007232 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233}
7234
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007236 * Add a number or string entry to dictionary "d".
7237 * When "str" is NULL use number "nr", otherwise use "str".
7238 * Returns FAIL when out of memory and when key already exists.
7239 */
7240 int
7241dict_add_nr_str(d, key, nr, str)
7242 dict_T *d;
7243 char *key;
7244 long nr;
7245 char_u *str;
7246{
7247 dictitem_T *item;
7248
7249 item = dictitem_alloc((char_u *)key);
7250 if (item == NULL)
7251 return FAIL;
7252 item->di_tv.v_lock = 0;
7253 if (str == NULL)
7254 {
7255 item->di_tv.v_type = VAR_NUMBER;
7256 item->di_tv.vval.v_number = nr;
7257 }
7258 else
7259 {
7260 item->di_tv.v_type = VAR_STRING;
7261 item->di_tv.vval.v_string = vim_strsave(str);
7262 }
7263 if (dict_add(d, item) == FAIL)
7264 {
7265 dictitem_free(item);
7266 return FAIL;
7267 }
7268 return OK;
7269}
7270
7271/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007272 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007273 * Returns FAIL when out of memory and when key already exists.
7274 */
7275 int
7276dict_add_list(d, key, list)
7277 dict_T *d;
7278 char *key;
7279 list_T *list;
7280{
7281 dictitem_T *item;
7282
7283 item = dictitem_alloc((char_u *)key);
7284 if (item == NULL)
7285 return FAIL;
7286 item->di_tv.v_lock = 0;
7287 item->di_tv.v_type = VAR_LIST;
7288 item->di_tv.vval.v_list = list;
7289 if (dict_add(d, item) == FAIL)
7290 {
7291 dictitem_free(item);
7292 return FAIL;
7293 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007294 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007295 return OK;
7296}
7297
7298/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 * Get the number of items in a Dictionary.
7300 */
7301 static long
7302dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (d == NULL)
7306 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007307 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308}
7309
7310/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 * Find item "key[len]" in Dictionary "d".
7312 * If "len" is negative use strlen(key).
7313 * Returns NULL when not found.
7314 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007315 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318 char_u *key;
7319 int len;
7320{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321#define AKEYLEN 200
7322 char_u buf[AKEYLEN];
7323 char_u *akey;
7324 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 if (len < 0)
7328 akey = key;
7329 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 tofree = akey = vim_strnsave(key, len);
7332 if (akey == NULL)
7333 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007334 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 else
7336 {
7337 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007338 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 akey = buf;
7340 }
7341
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 vim_free(tofree);
7344 if (HASHITEM_EMPTY(hi))
7345 return NULL;
7346 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347}
7348
7349/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007350 * Get a string item from a dictionary.
7351 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352 * Returns NULL if the entry doesn't exist or out of memory.
7353 */
7354 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007355get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007356 dict_T *d;
7357 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007358 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007359{
7360 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007361 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007362
7363 di = dict_find(d, key, -1);
7364 if (di == NULL)
7365 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007366 s = get_tv_string(&di->di_tv);
7367 if (save && s != NULL)
7368 s = vim_strsave(s);
7369 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007370}
7371
7372/*
7373 * Get a number item from a dictionary.
7374 * Returns 0 if the entry doesn't exist or out of memory.
7375 */
7376 long
7377get_dict_number(d, key)
7378 dict_T *d;
7379 char_u *key;
7380{
7381 dictitem_T *di;
7382
7383 di = dict_find(d, key, -1);
7384 if (di == NULL)
7385 return 0;
7386 return get_tv_number(&di->di_tv);
7387}
7388
7389/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 * Return an allocated string with the string representation of a Dictionary.
7391 * May return NULL.
7392 */
7393 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007394dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007396 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397{
7398 garray_T ga;
7399 int first = TRUE;
7400 char_u *tofree;
7401 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007404 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007407 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 return NULL;
7409 ga_init2(&ga, (int)sizeof(char), 80);
7410 ga_append(&ga, '{');
7411
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007412 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007413 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007415 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007417 --todo;
7418
7419 if (first)
7420 first = FALSE;
7421 else
7422 ga_concat(&ga, (char_u *)", ");
7423
7424 tofree = string_quote(hi->hi_key, FALSE);
7425 if (tofree != NULL)
7426 {
7427 ga_concat(&ga, tofree);
7428 vim_free(tofree);
7429 }
7430 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007431 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007432 if (s != NULL)
7433 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007435 if (s == NULL)
7436 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007439 if (todo > 0)
7440 {
7441 vim_free(ga.ga_data);
7442 return NULL;
7443 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444
7445 ga_append(&ga, '}');
7446 ga_append(&ga, NUL);
7447 return (char_u *)ga.ga_data;
7448}
7449
7450/*
7451 * Allocate a variable for a Dictionary and fill it from "*arg".
7452 * Return OK or FAIL. Returns NOTDONE for {expr}.
7453 */
7454 static int
7455get_dict_tv(arg, rettv, evaluate)
7456 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 int evaluate;
7459{
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 dict_T *d = NULL;
7461 typval_T tvkey;
7462 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007463 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007464 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007466 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467
7468 /*
7469 * First check if it's not a curly-braces thing: {expr}.
7470 * Must do this without evaluating, otherwise a function may be called
7471 * twice. Unfortunately this means we need to call eval1() twice for the
7472 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007473 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007474 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 if (*start != '}')
7476 {
7477 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7478 return FAIL;
7479 if (*start == '}')
7480 return NOTDONE;
7481 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482
7483 if (evaluate)
7484 {
7485 d = dict_alloc();
7486 if (d == NULL)
7487 return FAIL;
7488 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007489 tvkey.v_type = VAR_UNKNOWN;
7490 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491
7492 *arg = skipwhite(*arg + 1);
7493 while (**arg != '}' && **arg != NUL)
7494 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 goto failret;
7497 if (**arg != ':')
7498 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007499 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 goto failret;
7502 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007503 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007505 key = get_tv_string_buf_chk(&tvkey, buf);
7506 if (key == NULL || *key == NUL)
7507 {
7508 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7509 if (key != NULL)
7510 EMSG(_(e_emptykey));
7511 clear_tv(&tvkey);
7512 goto failret;
7513 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515
7516 *arg = skipwhite(*arg + 1);
7517 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7518 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007519 if (evaluate)
7520 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 goto failret;
7522 }
7523 if (evaluate)
7524 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 if (item != NULL)
7527 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007528 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 clear_tv(&tv);
7531 goto failret;
7532 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 item = dictitem_alloc(key);
7534 clear_tv(&tvkey);
7535 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007538 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 if (dict_add(d, item) == FAIL)
7540 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 }
7542 }
7543
7544 if (**arg == '}')
7545 break;
7546 if (**arg != ',')
7547 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007548 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549 goto failret;
7550 }
7551 *arg = skipwhite(*arg + 1);
7552 }
7553
7554 if (**arg != '}')
7555 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007556 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557failret:
7558 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007559 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 return FAIL;
7561 }
7562
7563 *arg = skipwhite(*arg + 1);
7564 if (evaluate)
7565 {
7566 rettv->v_type = VAR_DICT;
7567 rettv->vval.v_dict = d;
7568 ++d->dv_refcount;
7569 }
7570
7571 return OK;
7572}
7573
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 * Return a string with the string representation of a variable.
7576 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007577 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007578 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007579 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007580 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581 */
7582 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007583echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007585 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007586 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007587 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007588{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 static int recurse = 0;
7590 char_u *r = NULL;
7591
Bram Moolenaar33570922005-01-25 22:26:29 +00007592 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007593 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007594 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007595 *tofree = NULL;
7596 return NULL;
7597 }
7598 ++recurse;
7599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 switch (tv->v_type)
7601 {
7602 case VAR_FUNC:
7603 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007604 r = tv->vval.v_string;
7605 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007608 if (tv->vval.v_list == NULL)
7609 {
7610 *tofree = NULL;
7611 r = NULL;
7612 }
7613 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7614 {
7615 *tofree = NULL;
7616 r = (char_u *)"[...]";
7617 }
7618 else
7619 {
7620 tv->vval.v_list->lv_copyID = copyID;
7621 *tofree = list2string(tv, copyID);
7622 r = *tofree;
7623 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007624 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007625
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007627 if (tv->vval.v_dict == NULL)
7628 {
7629 *tofree = NULL;
7630 r = NULL;
7631 }
7632 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7633 {
7634 *tofree = NULL;
7635 r = (char_u *)"{...}";
7636 }
7637 else
7638 {
7639 tv->vval.v_dict->dv_copyID = copyID;
7640 *tofree = dict2string(tv, copyID);
7641 r = *tofree;
7642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007644
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 case VAR_STRING:
7646 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 *tofree = NULL;
7648 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007651#ifdef FEAT_FLOAT
7652 case VAR_FLOAT:
7653 *tofree = NULL;
7654 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7655 r = numbuf;
7656 break;
7657#endif
7658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007661 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007663
7664 --recurse;
7665 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007666}
7667
7668/*
7669 * Return a string with the string representation of a variable.
7670 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7671 * "numbuf" is used for a number.
7672 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007673 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 */
7675 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007676tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007677 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007678 char_u **tofree;
7679 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007680 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007681{
7682 switch (tv->v_type)
7683 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 case VAR_FUNC:
7685 *tofree = string_quote(tv->vval.v_string, TRUE);
7686 return *tofree;
7687 case VAR_STRING:
7688 *tofree = string_quote(tv->vval.v_string, FALSE);
7689 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007690#ifdef FEAT_FLOAT
7691 case VAR_FLOAT:
7692 *tofree = NULL;
7693 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7694 return numbuf;
7695#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007696 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007697 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007699 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007702 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007703 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007704}
7705
7706/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 * Return string "str" in ' quotes, doubling ' characters.
7708 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710 */
7711 static char_u *
7712string_quote(str, function)
7713 char_u *str;
7714 int function;
7715{
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 char_u *p, *r, *s;
7718
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 len = (function ? 13 : 3);
7720 if (str != NULL)
7721 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007722 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 for (p = str; *p != NUL; mb_ptr_adv(p))
7724 if (*p == '\'')
7725 ++len;
7726 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 s = r = alloc(len);
7728 if (r != NULL)
7729 {
7730 if (function)
7731 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007733 r += 10;
7734 }
7735 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 if (str != NULL)
7738 for (p = str; *p != NUL; )
7739 {
7740 if (*p == '\'')
7741 *r++ = '\'';
7742 MB_COPY_CHAR(p, r);
7743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007745 if (function)
7746 *r++ = ')';
7747 *r++ = NUL;
7748 }
7749 return s;
7750}
7751
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007752#ifdef FEAT_FLOAT
7753/*
7754 * Convert the string "text" to a floating point number.
7755 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7756 * this always uses a decimal point.
7757 * Returns the length of the text that was consumed.
7758 */
7759 static int
7760string2float(text, value)
7761 char_u *text;
7762 float_T *value; /* result stored here */
7763{
7764 char *s = (char *)text;
7765 float_T f;
7766
7767 f = strtod(s, &s);
7768 *value = f;
7769 return (int)((char_u *)s - text);
7770}
7771#endif
7772
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 * Get the value of an environment variable.
7775 * "arg" is pointing to the '$'. It is advanced to after the name.
7776 * If the environment variable was not set, silently assume it is empty.
7777 * Always return OK.
7778 */
7779 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 int evaluate;
7784{
7785 char_u *string = NULL;
7786 int len;
7787 int cc;
7788 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007789 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 ++*arg;
7792 name = *arg;
7793 len = get_env_len(arg);
7794 if (evaluate)
7795 {
7796 if (len != 0)
7797 {
7798 cc = name[len];
7799 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007800 /* first try vim_getenv(), fast for normal environment vars */
7801 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007803 {
7804 if (!mustfree)
7805 string = vim_strsave(string);
7806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 else
7808 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007809 if (mustfree)
7810 vim_free(string);
7811
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 /* next try expanding things like $VIM and ${HOME} */
7813 string = expand_env_save(name - 1);
7814 if (string != NULL && *string == '$')
7815 {
7816 vim_free(string);
7817 string = NULL;
7818 }
7819 }
7820 name[len] = cc;
7821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822 rettv->v_type = VAR_STRING;
7823 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 }
7825
7826 return OK;
7827}
7828
7829/*
7830 * Array with names and number of arguments of all internal functions
7831 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7832 */
7833static struct fst
7834{
7835 char *f_name; /* function name */
7836 char f_min_argc; /* minimal number of arguments */
7837 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007838 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007839 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840} functions[] =
7841{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842#ifdef FEAT_FLOAT
7843 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007844 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007846 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007847 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {"append", 2, 2, f_append},
7849 {"argc", 0, 0, f_argc},
7850 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007851 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007853 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007854 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007855 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007858 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {"bufexists", 1, 1, f_bufexists},
7860 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7861 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7862 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7863 {"buflisted", 1, 1, f_buflisted},
7864 {"bufloaded", 1, 1, f_bufloaded},
7865 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007866 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"bufwinnr", 1, 1, f_bufwinnr},
7868 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007869 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007870 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 {"ceil", 1, 1, f_ceil},
7873#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007874 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007875 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007877 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007879#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007880 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007881 {"complete_add", 1, 1, f_complete_add},
7882 {"complete_check", 0, 0, f_complete_check},
7883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007885 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007888 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007892 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007893 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"delete", 1, 1, f_delete},
7895 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007896 {"diff_filler", 1, 1, f_diff_filler},
7897 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007898 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"eventhandler", 0, 0, f_eventhandler},
7902 {"executable", 1, 1, f_executable},
7903 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007904#ifdef FEAT_FLOAT
7905 {"exp", 1, 1, f_exp},
7906#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007907 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007908 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007909 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7911 {"filereadable", 1, 1, f_filereadable},
7912 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007913 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007914 {"finddir", 1, 3, f_finddir},
7915 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007916#ifdef FEAT_FLOAT
7917 {"float2nr", 1, 1, f_float2nr},
7918 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007919 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007920#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007921 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"fnamemodify", 2, 2, f_fnamemodify},
7923 {"foldclosed", 1, 1, f_foldclosed},
7924 {"foldclosedend", 1, 1, f_foldclosedend},
7925 {"foldlevel", 1, 1, f_foldlevel},
7926 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007927 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007929 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007930 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007931 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007932 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007933 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"getchar", 0, 1, f_getchar},
7935 {"getcharmod", 0, 0, f_getcharmod},
7936 {"getcmdline", 0, 0, f_getcmdline},
7937 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007938 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007940 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007941 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"getfsize", 1, 1, f_getfsize},
7943 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007944 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007945 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007946 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007947 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007948 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007949 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007950 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007951 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007953 {"gettabvar", 2, 3, f_gettabvar},
7954 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {"getwinposx", 0, 0, f_getwinposx},
7956 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007957 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007958 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007959 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007961 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007962 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007963 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7965 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7966 {"histadd", 2, 2, f_histadd},
7967 {"histdel", 1, 2, f_histdel},
7968 {"histget", 1, 2, f_histget},
7969 {"histnr", 1, 1, f_histnr},
7970 {"hlID", 1, 1, f_hlID},
7971 {"hlexists", 1, 1, f_hlexists},
7972 {"hostname", 0, 0, f_hostname},
7973 {"iconv", 3, 3, f_iconv},
7974 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007975 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007976 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007978 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"inputrestore", 0, 0, f_inputrestore},
7980 {"inputsave", 0, 0, f_inputsave},
7981 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007982 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007983 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007985 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007986 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007987 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007990 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {"libcall", 3, 3, f_libcall},
7992 {"libcallnr", 3, 3, f_libcallnr},
7993 {"line", 1, 1, f_line},
7994 {"line2byte", 1, 1, f_line2byte},
7995 {"lispindent", 1, 1, f_lispindent},
7996 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007997#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007998 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007999 {"log10", 1, 1, f_log10},
8000#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008001#ifdef FEAT_LUA
8002 {"luaeval", 1, 2, f_luaeval},
8003#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008004 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008005 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008006 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008007 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008008 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008009 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008010 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008011 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008012 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008013 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008014 {"max", 1, 1, f_max},
8015 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008016#ifdef vim_mkdir
8017 {"mkdir", 1, 3, f_mkdir},
8018#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008019 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008020#ifdef FEAT_MZSCHEME
8021 {"mzeval", 1, 1, f_mzeval},
8022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008024 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008025 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008026 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008027#ifdef FEAT_FLOAT
8028 {"pow", 2, 2, f_pow},
8029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008031 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008032 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008033#ifdef FEAT_PYTHON3
8034 {"py3eval", 1, 1, f_py3eval},
8035#endif
8036#ifdef FEAT_PYTHON
8037 {"pyeval", 1, 1, f_pyeval},
8038#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008039 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008040 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008041 {"reltime", 0, 2, f_reltime},
8042 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {"remote_expr", 2, 3, f_remote_expr},
8044 {"remote_foreground", 1, 1, f_remote_foreground},
8045 {"remote_peek", 1, 2, f_remote_peek},
8046 {"remote_read", 1, 1, f_remote_read},
8047 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008048 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008050 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008052 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008053#ifdef FEAT_FLOAT
8054 {"round", 1, 1, f_round},
8055#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008056 {"screencol", 0, 0, f_screencol},
8057 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008058 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008059 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008060 {"searchpair", 3, 7, f_searchpair},
8061 {"searchpairpos", 3, 7, f_searchpairpos},
8062 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"server2client", 2, 2, f_server2client},
8064 {"serverlist", 0, 0, f_serverlist},
8065 {"setbufvar", 3, 3, f_setbufvar},
8066 {"setcmdpos", 1, 1, f_setcmdpos},
8067 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008068 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008069 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008070 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008071 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008073 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008074 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008076#ifdef FEAT_CRYPT
8077 {"sha256", 1, 1, f_sha256},
8078#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008079 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008080 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#ifdef FEAT_FLOAT
8083 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008084 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008085#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008086 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008087 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008088 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008089 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008090 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008091#ifdef FEAT_FLOAT
8092 {"sqrt", 1, 1, f_sqrt},
8093 {"str2float", 1, 1, f_str2float},
8094#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008095 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008096 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008097 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098#ifdef HAVE_STRFTIME
8099 {"strftime", 1, 2, f_strftime},
8100#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008101 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"strlen", 1, 1, f_strlen},
8104 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008105 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008107 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"submatch", 1, 1, f_submatch},
8109 {"substitute", 4, 4, f_substitute},
8110 {"synID", 3, 3, f_synID},
8111 {"synIDattr", 2, 3, f_synIDattr},
8112 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008113 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008114 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008115 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008116 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008117 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008118 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008119 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008120 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008121#ifdef FEAT_FLOAT
8122 {"tan", 1, 1, f_tan},
8123 {"tanh", 1, 1, f_tanh},
8124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008126 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"tolower", 1, 1, f_tolower},
8128 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008129 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008130#ifdef FEAT_FLOAT
8131 {"trunc", 1, 1, f_trunc},
8132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008134 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008135 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008136 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"virtcol", 1, 1, f_virtcol},
8138 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008139 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"winbufnr", 1, 1, f_winbufnr},
8141 {"wincol", 0, 0, f_wincol},
8142 {"winheight", 1, 1, f_winheight},
8143 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008144 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008146 {"winrestview", 1, 1, f_winrestview},
8147 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008149 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008150 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151};
8152
8153#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8154
8155/*
8156 * Function given to ExpandGeneric() to obtain the list of internal
8157 * or user defined function names.
8158 */
8159 char_u *
8160get_function_name(xp, idx)
8161 expand_T *xp;
8162 int idx;
8163{
8164 static int intidx = -1;
8165 char_u *name;
8166
8167 if (idx == 0)
8168 intidx = -1;
8169 if (intidx < 0)
8170 {
8171 name = get_user_func_name(xp, idx);
8172 if (name != NULL)
8173 return name;
8174 }
8175 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8176 {
8177 STRCPY(IObuff, functions[intidx].f_name);
8178 STRCAT(IObuff, "(");
8179 if (functions[intidx].f_max_argc == 0)
8180 STRCAT(IObuff, ")");
8181 return IObuff;
8182 }
8183
8184 return NULL;
8185}
8186
8187/*
8188 * Function given to ExpandGeneric() to obtain the list of internal or
8189 * user defined variable or function names.
8190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 char_u *
8192get_expr_name(xp, idx)
8193 expand_T *xp;
8194 int idx;
8195{
8196 static int intidx = -1;
8197 char_u *name;
8198
8199 if (idx == 0)
8200 intidx = -1;
8201 if (intidx < 0)
8202 {
8203 name = get_function_name(xp, idx);
8204 if (name != NULL)
8205 return name;
8206 }
8207 return get_user_var_name(xp, ++intidx);
8208}
8209
8210#endif /* FEAT_CMDL_COMPL */
8211
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008212#if defined(EBCDIC) || defined(PROTO)
8213/*
8214 * Compare struct fst by function name.
8215 */
8216 static int
8217compare_func_name(s1, s2)
8218 const void *s1;
8219 const void *s2;
8220{
8221 struct fst *p1 = (struct fst *)s1;
8222 struct fst *p2 = (struct fst *)s2;
8223
8224 return STRCMP(p1->f_name, p2->f_name);
8225}
8226
8227/*
8228 * Sort the function table by function name.
8229 * The sorting of the table above is ASCII dependant.
8230 * On machines using EBCDIC we have to sort it.
8231 */
8232 static void
8233sortFunctions()
8234{
8235 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8236
8237 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8238}
8239#endif
8240
8241
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242/*
8243 * Find internal function in table above.
8244 * Return index, or -1 if not found
8245 */
8246 static int
8247find_internal_func(name)
8248 char_u *name; /* name of the function */
8249{
8250 int first = 0;
8251 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8252 int cmp;
8253 int x;
8254
8255 /*
8256 * Find the function name in the table. Binary search.
8257 */
8258 while (first <= last)
8259 {
8260 x = first + ((unsigned)(last - first) >> 1);
8261 cmp = STRCMP(name, functions[x].f_name);
8262 if (cmp < 0)
8263 last = x - 1;
8264 else if (cmp > 0)
8265 first = x + 1;
8266 else
8267 return x;
8268 }
8269 return -1;
8270}
8271
8272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008273 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8274 * name it contains, otherwise return "name".
8275 */
8276 static char_u *
8277deref_func_name(name, lenp)
8278 char_u *name;
8279 int *lenp;
8280{
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 int cc;
8283
8284 cc = name[*lenp];
8285 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008286 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008288 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008289 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008290 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008291 {
8292 *lenp = 0;
8293 return (char_u *)""; /* just in case */
8294 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008295 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008297 }
8298
8299 return name;
8300}
8301
8302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 * Allocate a variable for the result of a function.
8304 * Return OK or FAIL.
8305 */
8306 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008307get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8308 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 char_u *name; /* name of the function */
8310 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 char_u **arg; /* argument, pointing to the '(' */
8313 linenr_T firstline; /* first line of range */
8314 linenr_T lastline; /* last line of range */
8315 int *doesrange; /* return: function handled range */
8316 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
8319 char_u *argp;
8320 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008321 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 int argcount = 0; /* number of arguments found */
8323
8324 /*
8325 * Get the arguments.
8326 */
8327 argp = *arg;
8328 while (argcount < MAX_FUNC_ARGS)
8329 {
8330 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8331 if (*argp == ')' || *argp == ',' || *argp == NUL)
8332 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8334 {
8335 ret = FAIL;
8336 break;
8337 }
8338 ++argcount;
8339 if (*argp != ',')
8340 break;
8341 }
8342 if (*argp == ')')
8343 ++argp;
8344 else
8345 ret = FAIL;
8346
8347 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008349 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 {
8352 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008353 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008355 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
8358 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
8361 *arg = skipwhite(argp);
8362 return ret;
8363}
8364
8365
8366/*
8367 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008368 * Return OK when the function can't be called, FAIL otherwise.
8369 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 */
8371 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008372call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008373 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008374 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008376 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008378 typval_T *argvars; /* vars for arguments, must have "argcount"
8379 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 linenr_T firstline; /* first line of range */
8381 linenr_T lastline; /* last line of range */
8382 int *doesrange; /* return: function handled range */
8383 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008384 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
8386 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387#define ERROR_UNKNOWN 0
8388#define ERROR_TOOMANY 1
8389#define ERROR_TOOFEW 2
8390#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008391#define ERROR_DICT 4
8392#define ERROR_NONE 5
8393#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 int error = ERROR_NONE;
8395 int i;
8396 int llen;
8397 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398#define FLEN_FIXED 40
8399 char_u fname_buf[FLEN_FIXED + 1];
8400 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008401 char_u *name;
8402
8403 /* Make a copy of the name, if it comes from a funcref variable it could
8404 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008405 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008406 if (name == NULL)
8407 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408
8409 /*
8410 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8411 * Change <SNR>123_name() to K_SNR 123_name().
8412 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 llen = eval_fname_script(name);
8415 if (llen > 0)
8416 {
8417 fname_buf[0] = K_SPECIAL;
8418 fname_buf[1] = KS_EXTRA;
8419 fname_buf[2] = (int)KE_SNR;
8420 i = 3;
8421 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8422 {
8423 if (current_SID <= 0)
8424 error = ERROR_SCRIPT;
8425 else
8426 {
8427 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8428 i = (int)STRLEN(fname_buf);
8429 }
8430 }
8431 if (i + STRLEN(name + llen) < FLEN_FIXED)
8432 {
8433 STRCPY(fname_buf + i, name + llen);
8434 fname = fname_buf;
8435 }
8436 else
8437 {
8438 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8439 if (fname == NULL)
8440 error = ERROR_OTHER;
8441 else
8442 {
8443 mch_memmove(fname, fname_buf, (size_t)i);
8444 STRCPY(fname + i, name + llen);
8445 }
8446 }
8447 }
8448 else
8449 fname = name;
8450
8451 *doesrange = FALSE;
8452
8453
8454 /* execute the function if no errors detected and executing */
8455 if (evaluate && error == ERROR_NONE)
8456 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008457 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8458 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 error = ERROR_UNKNOWN;
8460
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008461 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
8463 /*
8464 * User defined function.
8465 */
8466 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008467
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008469 /* Trigger FuncUndefined event, may load the function. */
8470 if (fp == NULL
8471 && apply_autocmds(EVENT_FUNCUNDEFINED,
8472 fname, fname, TRUE, NULL)
8473 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008475 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 fp = find_func(fname);
8477 }
8478#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008479 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008480 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008481 {
8482 /* loaded a package, search for the function again */
8483 fp = find_func(fname);
8484 }
8485
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if (fp != NULL)
8487 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008488 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008490 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008492 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008494 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 else
8497 {
8498 /*
8499 * Call the user function.
8500 * Save and restore search patterns, script variables and
8501 * redo buffer.
8502 */
8503 save_search_patterns();
8504 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008505 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008506 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008507 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008508 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8509 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8510 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008511 /* Function was unreferenced while being used, free it
8512 * now. */
8513 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 restoreRedobuff();
8515 restore_search_patterns();
8516 error = ERROR_NONE;
8517 }
8518 }
8519 }
8520 else
8521 {
8522 /*
8523 * Find the function name in the table, call its implementation.
8524 */
8525 i = find_internal_func(fname);
8526 if (i >= 0)
8527 {
8528 if (argcount < functions[i].f_min_argc)
8529 error = ERROR_TOOFEW;
8530 else if (argcount > functions[i].f_max_argc)
8531 error = ERROR_TOOMANY;
8532 else
8533 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008534 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008535 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 error = ERROR_NONE;
8537 }
8538 }
8539 }
8540 /*
8541 * The function call (or "FuncUndefined" autocommand sequence) might
8542 * have been aborted by an error, an interrupt, or an explicitly thrown
8543 * exception that has not been caught so far. This situation can be
8544 * tested for by calling aborting(). For an error in an internal
8545 * function or for the "E132" error in call_user_func(), however, the
8546 * throw point at which the "force_abort" flag (temporarily reset by
8547 * emsg()) is normally updated has not been reached yet. We need to
8548 * update that flag first to make aborting() reliable.
8549 */
8550 update_force_abort();
8551 }
8552 if (error == ERROR_NONE)
8553 ret = OK;
8554
8555 /*
8556 * Report an error unless the argument evaluation or function call has been
8557 * cancelled due to an aborting error, an interrupt, or an exception.
8558 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 if (!aborting())
8560 {
8561 switch (error)
8562 {
8563 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008564 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008565 break;
8566 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008567 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008568 break;
8569 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008570 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008571 name);
8572 break;
8573 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008574 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008575 name);
8576 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008577 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008578 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008579 name);
8580 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008581 }
8582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 if (fname != name && fname != fname_buf)
8585 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008586 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
8588 return ret;
8589}
8590
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008591/*
8592 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008593 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008594 */
8595 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008596emsg_funcname(ermsg, name)
8597 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008598 char_u *name;
8599{
8600 char_u *p;
8601
8602 if (*name == K_SPECIAL)
8603 p = concat_str((char_u *)"<SNR>", name + 3);
8604 else
8605 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008606 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008607 if (p != name)
8608 vim_free(p);
8609}
8610
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008611/*
8612 * Return TRUE for a non-zero Number and a non-empty String.
8613 */
8614 static int
8615non_zero_arg(argvars)
8616 typval_T *argvars;
8617{
8618 return ((argvars[0].v_type == VAR_NUMBER
8619 && argvars[0].vval.v_number != 0)
8620 || (argvars[0].v_type == VAR_STRING
8621 && argvars[0].vval.v_string != NULL
8622 && *argvars[0].vval.v_string != NUL));
8623}
8624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625/*********************************************
8626 * Implementation of the built-in functions
8627 */
8628
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008629#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008630static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8631
8632/*
8633 * Get the float value of "argvars[0]" into "f".
8634 * Returns FAIL when the argument is not a Number or Float.
8635 */
8636 static int
8637get_float_arg(argvars, f)
8638 typval_T *argvars;
8639 float_T *f;
8640{
8641 if (argvars[0].v_type == VAR_FLOAT)
8642 {
8643 *f = argvars[0].vval.v_float;
8644 return OK;
8645 }
8646 if (argvars[0].v_type == VAR_NUMBER)
8647 {
8648 *f = (float_T)argvars[0].vval.v_number;
8649 return OK;
8650 }
8651 EMSG(_("E808: Number or Float required"));
8652 return FAIL;
8653}
8654
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008655/*
8656 * "abs(expr)" function
8657 */
8658 static void
8659f_abs(argvars, rettv)
8660 typval_T *argvars;
8661 typval_T *rettv;
8662{
8663 if (argvars[0].v_type == VAR_FLOAT)
8664 {
8665 rettv->v_type = VAR_FLOAT;
8666 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8667 }
8668 else
8669 {
8670 varnumber_T n;
8671 int error = FALSE;
8672
8673 n = get_tv_number_chk(&argvars[0], &error);
8674 if (error)
8675 rettv->vval.v_number = -1;
8676 else if (n > 0)
8677 rettv->vval.v_number = n;
8678 else
8679 rettv->vval.v_number = -n;
8680 }
8681}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008682
8683/*
8684 * "acos()" function
8685 */
8686 static void
8687f_acos(argvars, rettv)
8688 typval_T *argvars;
8689 typval_T *rettv;
8690{
8691 float_T f;
8692
8693 rettv->v_type = VAR_FLOAT;
8694 if (get_float_arg(argvars, &f) == OK)
8695 rettv->vval.v_float = acos(f);
8696 else
8697 rettv->vval.v_float = 0.0;
8698}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008699#endif
8700
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008702 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 */
8704 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008705f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 typval_T *argvars;
8707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708{
Bram Moolenaar33570922005-01-25 22:26:29 +00008709 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008712 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008714 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008715 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008716 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008717 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008718 }
8719 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 EMSG(_(e_listreq));
8721}
8722
8723/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008724 * "and(expr, expr)" function
8725 */
8726 static void
8727f_and(argvars, rettv)
8728 typval_T *argvars;
8729 typval_T *rettv;
8730{
8731 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8732 & get_tv_number_chk(&argvars[1], NULL);
8733}
8734
8735/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736 * "append(lnum, string/list)" function
8737 */
8738 static void
8739f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 typval_T *argvars;
8741 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008742{
8743 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008744 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008745 list_T *l = NULL;
8746 listitem_T *li = NULL;
8747 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008748 long added = 0;
8749
Bram Moolenaar0d660222005-01-07 21:51:51 +00008750 lnum = get_tv_lnum(argvars);
8751 if (lnum >= 0
8752 && lnum <= curbuf->b_ml.ml_line_count
8753 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008754 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008755 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008756 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757 l = argvars[1].vval.v_list;
8758 if (l == NULL)
8759 return;
8760 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008761 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008762 for (;;)
8763 {
8764 if (l == NULL)
8765 tv = &argvars[1]; /* append a string */
8766 else if (li == NULL)
8767 break; /* end of list */
8768 else
8769 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008770 line = get_tv_string_chk(tv);
8771 if (line == NULL) /* type error */
8772 {
8773 rettv->vval.v_number = 1; /* Failed */
8774 break;
8775 }
8776 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008777 ++added;
8778 if (l == NULL)
8779 break;
8780 li = li->li_next;
8781 }
8782
8783 appended_lines_mark(lnum, added);
8784 if (curwin->w_cursor.lnum > lnum)
8785 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787 else
8788 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789}
8790
8791/*
8792 * "argc()" function
8793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008796 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800}
8801
8802/*
8803 * "argidx()" function
8804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008807 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811}
8812
8813/*
8814 * "argv(nr)" function
8815 */
8816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008818 typval_T *argvars;
8819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 int idx;
8822
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008823 if (argvars[0].v_type != VAR_UNKNOWN)
8824 {
8825 idx = get_tv_number_chk(&argvars[0], NULL);
8826 if (idx >= 0 && idx < ARGCOUNT)
8827 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8828 else
8829 rettv->vval.v_string = NULL;
8830 rettv->v_type = VAR_STRING;
8831 }
8832 else if (rettv_list_alloc(rettv) == OK)
8833 for (idx = 0; idx < ARGCOUNT; ++idx)
8834 list_append_string(rettv->vval.v_list,
8835 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836}
8837
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008838#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008839/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008840 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008841 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008842 static void
8843f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008844 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008845 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008846{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008847 float_T f;
8848
8849 rettv->v_type = VAR_FLOAT;
8850 if (get_float_arg(argvars, &f) == OK)
8851 rettv->vval.v_float = asin(f);
8852 else
8853 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008854}
8855
8856/*
8857 * "atan()" function
8858 */
8859 static void
8860f_atan(argvars, rettv)
8861 typval_T *argvars;
8862 typval_T *rettv;
8863{
8864 float_T f;
8865
8866 rettv->v_type = VAR_FLOAT;
8867 if (get_float_arg(argvars, &f) == OK)
8868 rettv->vval.v_float = atan(f);
8869 else
8870 rettv->vval.v_float = 0.0;
8871}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008872
8873/*
8874 * "atan2()" function
8875 */
8876 static void
8877f_atan2(argvars, rettv)
8878 typval_T *argvars;
8879 typval_T *rettv;
8880{
8881 float_T fx, fy;
8882
8883 rettv->v_type = VAR_FLOAT;
8884 if (get_float_arg(argvars, &fx) == OK
8885 && get_float_arg(&argvars[1], &fy) == OK)
8886 rettv->vval.v_float = atan2(fx, fy);
8887 else
8888 rettv->vval.v_float = 0.0;
8889}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008890#endif
8891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892/*
8893 * "browse(save, title, initdir, default)" function
8894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008897 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900#ifdef FEAT_BROWSE
8901 int save;
8902 char_u *title;
8903 char_u *initdir;
8904 char_u *defname;
8905 char_u buf[NUMBUFLEN];
8906 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 save = get_tv_number_chk(&argvars[0], &error);
8910 title = get_tv_string_chk(&argvars[1]);
8911 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8912 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008914 if (error || title == NULL || initdir == NULL || defname == NULL)
8915 rettv->vval.v_string = NULL;
8916 else
8917 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008918 do_browse(save ? BROWSE_SAVE : 0,
8919 title, defname, NULL, initdir, NULL, curbuf);
8920#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008922#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008924}
8925
8926/*
8927 * "browsedir(title, initdir)" function
8928 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008933{
8934#ifdef FEAT_BROWSE
8935 char_u *title;
8936 char_u *initdir;
8937 char_u buf[NUMBUFLEN];
8938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 title = get_tv_string_chk(&argvars[0]);
8940 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 if (title == NULL || initdir == NULL)
8943 rettv->vval.v_string = NULL;
8944 else
8945 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008946 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951}
8952
Bram Moolenaar33570922005-01-25 22:26:29 +00008953static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008954
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955/*
8956 * Find a buffer by number or exact name.
8957 */
8958 static buf_T *
8959find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 if (avar->v_type == VAR_NUMBER)
8965 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008966 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008968 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008969 if (buf == NULL)
8970 {
8971 /* No full path name match, try a match with a URL or a "nofile"
8972 * buffer, these don't use the full path. */
8973 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8974 if (buf->b_fname != NULL
8975 && (path_with_url(buf->b_fname)
8976#ifdef FEAT_QUICKFIX
8977 || bt_nofile(buf)
8978#endif
8979 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008980 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008981 break;
8982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 }
8984 return buf;
8985}
8986
8987/*
8988 * "bufexists(expr)" function
8989 */
8990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 typval_T *argvars;
8993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996}
8997
8998/*
8999 * "buflisted(expr)" function
9000 */
9001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006 buf_T *buf;
9007
9008 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010}
9011
9012/*
9013 * "bufloaded(expr)" function
9014 */
9015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 typval_T *argvars;
9018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
9020 buf_T *buf;
9021
9022 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024}
9025
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009026static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009027
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028/*
9029 * Get buffer by number or pattern.
9030 */
9031 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009032get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009033 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009034 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 int save_magic;
9038 char_u *save_cpo;
9039 buf_T *buf;
9040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 if (tv->v_type == VAR_NUMBER)
9042 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009043 if (tv->v_type != VAR_STRING)
9044 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 if (name == NULL || *name == NUL)
9046 return curbuf;
9047 if (name[0] == '$' && name[1] == NUL)
9048 return lastbuf;
9049
9050 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9051 save_magic = p_magic;
9052 p_magic = TRUE;
9053 save_cpo = p_cpo;
9054 p_cpo = (char_u *)"";
9055
9056 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009057 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
9059 p_magic = save_magic;
9060 p_cpo = save_cpo;
9061
9062 /* If not found, try expanding the name, like done for bufexists(). */
9063 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
9066 return buf;
9067}
9068
9069/*
9070 * "bufname(expr)" function
9071 */
9072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 typval_T *argvars;
9075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
9077 buf_T *buf;
9078
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 Moolenaar0c279bb2013-03-19 14:25:54 +01009081 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 --emsg_off;
9088}
9089
9090/*
9091 * "bufnr(expr)" function
9092 */
9093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009095 typval_T *argvars;
9096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
9098 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009099 int error = FALSE;
9100 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009102 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009104 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009105 --emsg_off;
9106
9107 /* If the buffer isn't found and the second argument is not zero create a
9108 * new buffer. */
9109 if (buf == NULL
9110 && argvars[1].v_type != VAR_UNKNOWN
9111 && get_tv_number_chk(&argvars[1], &error) != 0
9112 && !error
9113 && (name = get_tv_string_chk(&argvars[0])) != NULL
9114 && !error)
9115 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9116
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121}
9122
9123/*
9124 * "bufwinnr(nr)" function
9125 */
9126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009128 typval_T *argvars;
9129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130{
9131#ifdef FEAT_WINDOWS
9132 win_T *wp;
9133 int winnr = 0;
9134#endif
9135 buf_T *buf;
9136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009137 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009139 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140#ifdef FEAT_WINDOWS
9141 for (wp = firstwin; wp; wp = wp->w_next)
9142 {
9143 ++winnr;
9144 if (wp->w_buffer == buf)
9145 break;
9146 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#endif
9151 --emsg_off;
9152}
9153
9154/*
9155 * "byte2line(byte)" function
9156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009159 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
9162#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164#else
9165 long boff = 0;
9166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 (linenr_T)0, &boff);
9173#endif
9174}
9175
9176/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009177 * "byteidx()" function
9178 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009181 typval_T *argvars;
9182 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009183{
9184#ifdef FEAT_MBYTE
9185 char_u *t;
9186#endif
9187 char_u *str;
9188 long idx;
9189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009190 str = get_tv_string_chk(&argvars[0]);
9191 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009194 return;
9195
9196#ifdef FEAT_MBYTE
9197 t = str;
9198 for ( ; idx > 0; idx--)
9199 {
9200 if (*t == NUL) /* EOL reached */
9201 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009202 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009203 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009204 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009205#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009206 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009208#endif
9209}
9210
Bram Moolenaardb913952012-06-29 12:54:53 +02009211 int
9212func_call(name, args, selfdict, rettv)
9213 char_u *name;
9214 typval_T *args;
9215 dict_T *selfdict;
9216 typval_T *rettv;
9217{
9218 listitem_T *item;
9219 typval_T argv[MAX_FUNC_ARGS + 1];
9220 int argc = 0;
9221 int dummy;
9222 int r = 0;
9223
9224 for (item = args->vval.v_list->lv_first; item != NULL;
9225 item = item->li_next)
9226 {
9227 if (argc == MAX_FUNC_ARGS)
9228 {
9229 EMSG(_("E699: Too many arguments"));
9230 break;
9231 }
9232 /* Make a copy of each argument. This is needed to be able to set
9233 * v_lock to VAR_FIXED in the copy without changing the original list.
9234 */
9235 copy_tv(&item->li_tv, &argv[argc++]);
9236 }
9237
9238 if (item == NULL)
9239 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9241 &dummy, TRUE, selfdict);
9242
9243 /* Free the arguments. */
9244 while (argc > 0)
9245 clear_tv(&argv[--argc]);
9246
9247 return r;
9248}
9249
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009250/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009251 * "call(func, arglist)" function
9252 */
9253 static void
9254f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009255 typval_T *argvars;
9256 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009257{
9258 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009259 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009260
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009261 if (argvars[1].v_type != VAR_LIST)
9262 {
9263 EMSG(_(e_listreq));
9264 return;
9265 }
9266 if (argvars[1].vval.v_list == NULL)
9267 return;
9268
9269 if (argvars[0].v_type == VAR_FUNC)
9270 func = argvars[0].vval.v_string;
9271 else
9272 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 if (*func == NUL)
9274 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009275
Bram Moolenaare9a41262005-01-15 22:18:47 +00009276 if (argvars[2].v_type != VAR_UNKNOWN)
9277 {
9278 if (argvars[2].v_type != VAR_DICT)
9279 {
9280 EMSG(_(e_dictreq));
9281 return;
9282 }
9283 selfdict = argvars[2].vval.v_dict;
9284 }
9285
Bram Moolenaardb913952012-06-29 12:54:53 +02009286 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009287}
9288
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009289#ifdef FEAT_FLOAT
9290/*
9291 * "ceil({float})" function
9292 */
9293 static void
9294f_ceil(argvars, rettv)
9295 typval_T *argvars;
9296 typval_T *rettv;
9297{
9298 float_T f;
9299
9300 rettv->v_type = VAR_FLOAT;
9301 if (get_float_arg(argvars, &f) == OK)
9302 rettv->vval.v_float = ceil(f);
9303 else
9304 rettv->vval.v_float = 0.0;
9305}
9306#endif
9307
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009308/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009309 * "changenr()" function
9310 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009311 static void
9312f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009313 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009314 typval_T *rettv;
9315{
9316 rettv->vval.v_number = curbuf->b_u_seq_cur;
9317}
9318
9319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 * "char2nr(string)" function
9321 */
9322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009324 typval_T *argvars;
9325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326{
9327#ifdef FEAT_MBYTE
9328 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009329 {
9330 int utf8 = 0;
9331
9332 if (argvars[1].v_type != VAR_UNKNOWN)
9333 utf8 = get_tv_number_chk(&argvars[1], NULL);
9334
9335 if (utf8)
9336 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9337 else
9338 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 else
9341#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343}
9344
9345/*
9346 * "cindent(lnum)" function
9347 */
9348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009350 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353#ifdef FEAT_CINDENT
9354 pos_T pos;
9355 linenr_T lnum;
9356
9357 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9360 {
9361 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 curwin->w_cursor = pos;
9364 }
9365 else
9366#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009367 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368}
9369
9370/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009371 * "clearmatches()" function
9372 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009373 static void
9374f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009375 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009376 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009377{
9378#ifdef FEAT_SEARCH_EXTRA
9379 clear_matches(curwin);
9380#endif
9381}
9382
9383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 * "col(string)" function
9385 */
9386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 typval_T *argvars;
9389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 colnr_T col = 0;
9392 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009393 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009395 fp = var2fpos(&argvars[0], FALSE, &fnum);
9396 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 {
9398 if (fp->col == MAXCOL)
9399 {
9400 /* '> can be MAXCOL, get the length of the line then */
9401 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009402 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 else
9404 col = MAXCOL;
9405 }
9406 else
9407 {
9408 col = fp->col + 1;
9409#ifdef FEAT_VIRTUALEDIT
9410 /* col(".") when the cursor is on the NUL at the end of the line
9411 * because of "coladd" can be seen as an extra column. */
9412 if (virtual_active() && fp == &curwin->w_cursor)
9413 {
9414 char_u *p = ml_get_cursor();
9415
9416 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9417 curwin->w_virtcol - curwin->w_cursor.coladd))
9418 {
9419# ifdef FEAT_MBYTE
9420 int l;
9421
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009422 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 col += l;
9424# else
9425 if (*p != NUL && p[1] == NUL)
9426 ++col;
9427# endif
9428 }
9429 }
9430#endif
9431 }
9432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434}
9435
Bram Moolenaar572cb562005-08-05 21:35:02 +00009436#if defined(FEAT_INS_EXPAND)
9437/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009438 * "complete()" function
9439 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009440 static void
9441f_complete(argvars, rettv)
9442 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009443 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009444{
9445 int startcol;
9446
9447 if ((State & INSERT) == 0)
9448 {
9449 EMSG(_("E785: complete() can only be used in Insert mode"));
9450 return;
9451 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009452
9453 /* Check for undo allowed here, because if something was already inserted
9454 * the line was already saved for undo and this check isn't done. */
9455 if (!undo_allowed())
9456 return;
9457
Bram Moolenaarade00832006-03-10 21:46:58 +00009458 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9459 {
9460 EMSG(_(e_invarg));
9461 return;
9462 }
9463
9464 startcol = get_tv_number_chk(&argvars[0], NULL);
9465 if (startcol <= 0)
9466 return;
9467
9468 set_completion(startcol - 1, argvars[1].vval.v_list);
9469}
9470
9471/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009472 * "complete_add()" function
9473 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009474 static void
9475f_complete_add(argvars, rettv)
9476 typval_T *argvars;
9477 typval_T *rettv;
9478{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009479 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009480}
9481
9482/*
9483 * "complete_check()" function
9484 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009485 static void
9486f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009487 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009488 typval_T *rettv;
9489{
9490 int saved = RedrawingDisabled;
9491
9492 RedrawingDisabled = 0;
9493 ins_compl_check_keys(0);
9494 rettv->vval.v_number = compl_interrupted;
9495 RedrawingDisabled = saved;
9496}
9497#endif
9498
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499/*
9500 * "confirm(message, buttons[, default [, type]])" function
9501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009504 typval_T *argvars UNUSED;
9505 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506{
9507#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9508 char_u *message;
9509 char_u *buttons = NULL;
9510 char_u buf[NUMBUFLEN];
9511 char_u buf2[NUMBUFLEN];
9512 int def = 1;
9513 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 char_u *typestr;
9515 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 message = get_tv_string_chk(&argvars[0]);
9518 if (message == NULL)
9519 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009522 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9523 if (buttons == NULL)
9524 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009525 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009528 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9531 if (typestr == NULL)
9532 error = TRUE;
9533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 switch (TOUPPER_ASC(*typestr))
9536 {
9537 case 'E': type = VIM_ERROR; break;
9538 case 'Q': type = VIM_QUESTION; break;
9539 case 'I': type = VIM_INFO; break;
9540 case 'W': type = VIM_WARNING; break;
9541 case 'G': type = VIM_GENERIC; break;
9542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 }
9544 }
9545 }
9546 }
9547
9548 if (buttons == NULL || *buttons == NUL)
9549 buttons = (char_u *)_("&Ok");
9550
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009551 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009553 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554#endif
9555}
9556
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009557/*
9558 * "copy()" function
9559 */
9560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009562 typval_T *argvars;
9563 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009564{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009565 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009568#ifdef FEAT_FLOAT
9569/*
9570 * "cos()" function
9571 */
9572 static void
9573f_cos(argvars, rettv)
9574 typval_T *argvars;
9575 typval_T *rettv;
9576{
9577 float_T f;
9578
9579 rettv->v_type = VAR_FLOAT;
9580 if (get_float_arg(argvars, &f) == OK)
9581 rettv->vval.v_float = cos(f);
9582 else
9583 rettv->vval.v_float = 0.0;
9584}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009585
9586/*
9587 * "cosh()" function
9588 */
9589 static void
9590f_cosh(argvars, rettv)
9591 typval_T *argvars;
9592 typval_T *rettv;
9593{
9594 float_T f;
9595
9596 rettv->v_type = VAR_FLOAT;
9597 if (get_float_arg(argvars, &f) == OK)
9598 rettv->vval.v_float = cosh(f);
9599 else
9600 rettv->vval.v_float = 0.0;
9601}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009602#endif
9603
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009605 * "count()" function
9606 */
9607 static void
9608f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009609 typval_T *argvars;
9610 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009611{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009612 long n = 0;
9613 int ic = FALSE;
9614
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009616 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009617 listitem_T *li;
9618 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009619 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009620
Bram Moolenaare9a41262005-01-15 22:18:47 +00009621 if ((l = argvars[0].vval.v_list) != NULL)
9622 {
9623 li = l->lv_first;
9624 if (argvars[2].v_type != VAR_UNKNOWN)
9625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 int error = FALSE;
9627
9628 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 if (argvars[3].v_type != VAR_UNKNOWN)
9630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009631 idx = get_tv_number_chk(&argvars[3], &error);
9632 if (!error)
9633 {
9634 li = list_find(l, idx);
9635 if (li == NULL)
9636 EMSGN(_(e_listidx), idx);
9637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 if (error)
9640 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641 }
9642
9643 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009644 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 ++n;
9646 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648 else if (argvars[0].v_type == VAR_DICT)
9649 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 int todo;
9651 dict_T *d;
9652 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009653
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009654 if ((d = argvars[0].vval.v_dict) != NULL)
9655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 int error = FALSE;
9657
Bram Moolenaare9a41262005-01-15 22:18:47 +00009658 if (argvars[2].v_type != VAR_UNKNOWN)
9659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 if (argvars[3].v_type != VAR_UNKNOWN)
9662 EMSG(_(e_invarg));
9663 }
9664
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009665 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009666 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009667 {
9668 if (!HASHITEM_EMPTY(hi))
9669 {
9670 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009671 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009672 ++n;
9673 }
9674 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009675 }
9676 }
9677 else
9678 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009679 rettv->vval.v_number = n;
9680}
9681
9682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9684 *
9685 * Checks the existence of a cscope connection.
9686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009689 typval_T *argvars UNUSED;
9690 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691{
9692#ifdef FEAT_CSCOPE
9693 int num = 0;
9694 char_u *dbpath = NULL;
9695 char_u *prepend = NULL;
9696 char_u buf[NUMBUFLEN];
9697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009698 if (argvars[0].v_type != VAR_UNKNOWN
9699 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 num = (int)get_tv_number(&argvars[0]);
9702 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009703 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 }
9706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708#endif
9709}
9710
9711/*
9712 * "cursor(lnum, col)" function
9713 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009714 * Moves the cursor to the specified line and column.
9715 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009723#ifdef FEAT_VIRTUALEDIT
9724 long coladd = 0;
9725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009727 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009728 if (argvars[1].v_type == VAR_UNKNOWN)
9729 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009730 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009731
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009732 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009733 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009734 line = pos.lnum;
9735 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009736#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009737 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009738#endif
9739 }
9740 else
9741 {
9742 line = get_tv_lnum(argvars);
9743 col = get_tv_number_chk(&argvars[1], NULL);
9744#ifdef FEAT_VIRTUALEDIT
9745 if (argvars[2].v_type != VAR_UNKNOWN)
9746 coladd = get_tv_number_chk(&argvars[2], NULL);
9747#endif
9748 }
9749 if (line < 0 || col < 0
9750#ifdef FEAT_VIRTUALEDIT
9751 || coladd < 0
9752#endif
9753 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009754 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 if (line > 0)
9756 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (col > 0)
9758 curwin->w_cursor.col = col - 1;
9759#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009760 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761#endif
9762
9763 /* Make sure the cursor is in a valid position. */
9764 check_cursor();
9765#ifdef FEAT_MBYTE
9766 /* Correct cursor for multi-byte character. */
9767 if (has_mbyte)
9768 mb_adjust_cursor();
9769#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009770
9771 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009772 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773}
9774
9775/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009776 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 */
9778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009780 typval_T *argvars;
9781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009783 int noref = 0;
9784
9785 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009786 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009787 if (noref < 0 || noref > 1)
9788 EMSG(_(e_invarg));
9789 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009790 {
9791 current_copyID += COPYID_INC;
9792 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794}
9795
9796/*
9797 * "delete()" function
9798 */
9799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009801 typval_T *argvars;
9802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803{
9804 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808}
9809
9810/*
9811 * "did_filetype()" function
9812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009815 typval_T *argvars UNUSED;
9816 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817{
9818#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820#endif
9821}
9822
9823/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009824 * "diff_filler()" function
9825 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009828 typval_T *argvars UNUSED;
9829 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009830{
9831#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009833#endif
9834}
9835
9836/*
9837 * "diff_hlID()" function
9838 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009841 typval_T *argvars UNUSED;
9842 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009843{
9844#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009846 static linenr_T prev_lnum = 0;
9847 static int changedtick = 0;
9848 static int fnum = 0;
9849 static int change_start = 0;
9850 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009851 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009852 int filler_lines;
9853 int col;
9854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009855 if (lnum < 0) /* ignore type error in {lnum} arg */
9856 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009857 if (lnum != prev_lnum
9858 || changedtick != curbuf->b_changedtick
9859 || fnum != curbuf->b_fnum)
9860 {
9861 /* New line, buffer, change: need to get the values. */
9862 filler_lines = diff_check(curwin, lnum);
9863 if (filler_lines < 0)
9864 {
9865 if (filler_lines == -1)
9866 {
9867 change_start = MAXCOL;
9868 change_end = -1;
9869 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9870 hlID = HLF_ADD; /* added line */
9871 else
9872 hlID = HLF_CHD; /* changed line */
9873 }
9874 else
9875 hlID = HLF_ADD; /* added line */
9876 }
9877 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009878 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009879 prev_lnum = lnum;
9880 changedtick = curbuf->b_changedtick;
9881 fnum = curbuf->b_fnum;
9882 }
9883
9884 if (hlID == HLF_CHD || hlID == HLF_TXD)
9885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009886 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009887 if (col >= change_start && col <= change_end)
9888 hlID = HLF_TXD; /* changed text */
9889 else
9890 hlID = HLF_CHD; /* changed line */
9891 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009892 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009893#endif
9894}
9895
9896/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009897 * "empty({expr})" function
9898 */
9899 static void
9900f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 typval_T *argvars;
9902 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009903{
9904 int n;
9905
9906 switch (argvars[0].v_type)
9907 {
9908 case VAR_STRING:
9909 case VAR_FUNC:
9910 n = argvars[0].vval.v_string == NULL
9911 || *argvars[0].vval.v_string == NUL;
9912 break;
9913 case VAR_NUMBER:
9914 n = argvars[0].vval.v_number == 0;
9915 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009916#ifdef FEAT_FLOAT
9917 case VAR_FLOAT:
9918 n = argvars[0].vval.v_float == 0.0;
9919 break;
9920#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009921 case VAR_LIST:
9922 n = argvars[0].vval.v_list == NULL
9923 || argvars[0].vval.v_list->lv_first == NULL;
9924 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009925 case VAR_DICT:
9926 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009927 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009929 default:
9930 EMSG2(_(e_intern2), "f_empty()");
9931 n = 0;
9932 }
9933
9934 rettv->vval.v_number = n;
9935}
9936
9937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 * "escape({string}, {chars})" function
9939 */
9940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009941f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009942 typval_T *argvars;
9943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944{
9945 char_u buf[NUMBUFLEN];
9946
Bram Moolenaar758711c2005-02-02 23:11:38 +00009947 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9948 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950}
9951
9952/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009953 * "eval()" function
9954 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009955 static void
9956f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009959{
9960 char_u *s;
9961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009962 s = get_tv_string_chk(&argvars[0]);
9963 if (s != NULL)
9964 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9967 {
9968 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009969 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009970 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009971 else if (*s != NUL)
9972 EMSG(_(e_trailing));
9973}
9974
9975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 * "eventhandler()" function
9977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009980 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984}
9985
9986/*
9987 * "executable()" function
9988 */
9989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009991 typval_T *argvars;
9992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
9996
9997/*
9998 * "exists()" function
9999 */
10000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 typval_T *argvars;
10003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 char_u *p;
10006 char_u *name;
10007 int n = FALSE;
10008 int len = 0;
10009
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010010 no_autoload = TRUE;
10011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 if (*p == '$') /* environment variable */
10014 {
10015 /* first try "normal" environment variables (fast) */
10016 if (mch_getenv(p + 1) != NULL)
10017 n = TRUE;
10018 else
10019 {
10020 /* try expanding things like $VIM and ${HOME} */
10021 p = expand_env_save(p);
10022 if (p != NULL && *p != '$')
10023 n = TRUE;
10024 vim_free(p);
10025 }
10026 }
10027 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010030 if (*skipwhite(p) != NUL)
10031 n = FALSE; /* trailing garbage */
10032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 else if (*p == '*') /* internal or user defined function */
10034 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010035 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 }
10037 else if (*p == ':')
10038 {
10039 n = cmd_exists(p + 1);
10040 }
10041 else if (*p == '#')
10042 {
10043#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010044 if (p[1] == '#')
10045 n = autocmd_supported(p + 2);
10046 else
10047 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048#endif
10049 }
10050 else /* internal variable */
10051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010052 char_u *tofree;
10053 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010055 /* get_name_len() takes care of expanding curly braces */
10056 name = p;
10057 len = get_name_len(&p, &tofree, TRUE, FALSE);
10058 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010060 if (tofree != NULL)
10061 name = tofree;
10062 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10063 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010065 /* handle d.key, l[idx], f(expr) */
10066 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10067 if (n)
10068 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 }
10070 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010071 if (*p != NUL)
10072 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010074 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 }
10076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010078
10079 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080}
10081
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010082#ifdef FEAT_FLOAT
10083/*
10084 * "exp()" function
10085 */
10086 static void
10087f_exp(argvars, rettv)
10088 typval_T *argvars;
10089 typval_T *rettv;
10090{
10091 float_T f;
10092
10093 rettv->v_type = VAR_FLOAT;
10094 if (get_float_arg(argvars, &f) == OK)
10095 rettv->vval.v_float = exp(f);
10096 else
10097 rettv->vval.v_float = 0.0;
10098}
10099#endif
10100
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101/*
10102 * "expand()" function
10103 */
10104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010106 typval_T *argvars;
10107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108{
10109 char_u *s;
10110 int len;
10111 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010112 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010115 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010118 if (argvars[1].v_type != VAR_UNKNOWN
10119 && argvars[2].v_type != VAR_UNKNOWN
10120 && get_tv_number_chk(&argvars[2], &error)
10121 && !error)
10122 {
10123 rettv->v_type = VAR_LIST;
10124 rettv->vval.v_list = NULL;
10125 }
10126
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 if (*s == '%' || *s == '#' || *s == '<')
10129 {
10130 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010131 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010133 if (rettv->v_type == VAR_LIST)
10134 {
10135 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10136 list_append_string(rettv->vval.v_list, result, -1);
10137 else
10138 vim_free(result);
10139 }
10140 else
10141 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 }
10143 else
10144 {
10145 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010146 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010147 if (argvars[1].v_type != VAR_UNKNOWN
10148 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010149 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 if (!error)
10151 {
10152 ExpandInit(&xpc);
10153 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010154 if (p_wic)
10155 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010156 if (rettv->v_type == VAR_STRING)
10157 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10158 options, WILD_ALL);
10159 else if (rettv_list_alloc(rettv) != FAIL)
10160 {
10161 int i;
10162
10163 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10164 for (i = 0; i < xpc.xp_numfiles; i++)
10165 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10166 ExpandCleanup(&xpc);
10167 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 }
10169 else
10170 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 }
10172}
10173
10174/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010175 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010176 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010177 */
10178 static void
10179f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010180 typval_T *argvars;
10181 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010182{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010183 char *arg_errmsg = N_("extend() argument");
10184
Bram Moolenaare9a41262005-01-15 22:18:47 +000010185 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010186 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010187 list_T *l1, *l2;
10188 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010191
Bram Moolenaare9a41262005-01-15 22:18:47 +000010192 l1 = argvars[0].vval.v_list;
10193 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010194 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010195 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 {
10197 if (argvars[2].v_type != VAR_UNKNOWN)
10198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010199 before = get_tv_number_chk(&argvars[2], &error);
10200 if (error)
10201 return; /* type error; errmsg already given */
10202
Bram Moolenaar758711c2005-02-02 23:11:38 +000010203 if (before == l1->lv_len)
10204 item = NULL;
10205 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010206 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010207 item = list_find(l1, before);
10208 if (item == NULL)
10209 {
10210 EMSGN(_(e_listidx), before);
10211 return;
10212 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010213 }
10214 }
10215 else
10216 item = NULL;
10217 list_extend(l1, l2, item);
10218
Bram Moolenaare9a41262005-01-15 22:18:47 +000010219 copy_tv(&argvars[0], rettv);
10220 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010221 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10223 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 dict_T *d1, *d2;
10225 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010226 char_u *action;
10227 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010228 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010229 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230
10231 d1 = argvars[0].vval.v_dict;
10232 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010233 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010234 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 {
10236 /* Check the third argument. */
10237 if (argvars[2].v_type != VAR_UNKNOWN)
10238 {
10239 static char *(av[]) = {"keep", "force", "error"};
10240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 action = get_tv_string_chk(&argvars[2]);
10242 if (action == NULL)
10243 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010244 for (i = 0; i < 3; ++i)
10245 if (STRCMP(action, av[i]) == 0)
10246 break;
10247 if (i == 3)
10248 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010249 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010250 return;
10251 }
10252 }
10253 else
10254 action = (char_u *)"force";
10255
10256 /* Go over all entries in the second dict and add them to the
10257 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010258 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010259 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010261 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010263 --todo;
10264 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010265 if (d1->dv_scope != 0)
10266 {
10267 /* Disallow replacing a builtin function in l: and g:.
10268 * Check the key to be valid when adding to any
10269 * scope. */
10270 if (d1->dv_scope == VAR_DEF_SCOPE
10271 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10272 && var_check_func_name(hi2->hi_key,
10273 di1 == NULL))
10274 break;
10275 if (!valid_varname(hi2->hi_key))
10276 break;
10277 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010278 if (di1 == NULL)
10279 {
10280 di1 = dictitem_copy(HI2DI(hi2));
10281 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10282 dictitem_free(di1);
10283 }
10284 else if (*action == 'e')
10285 {
10286 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10287 break;
10288 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010289 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010290 {
10291 clear_tv(&di1->di_tv);
10292 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10293 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294 }
10295 }
10296
Bram Moolenaare9a41262005-01-15 22:18:47 +000010297 copy_tv(&argvars[0], rettv);
10298 }
10299 }
10300 else
10301 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010302}
10303
10304/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010305 * "feedkeys()" function
10306 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010307 static void
10308f_feedkeys(argvars, rettv)
10309 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010310 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010311{
10312 int remap = TRUE;
10313 char_u *keys, *flags;
10314 char_u nbuf[NUMBUFLEN];
10315 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010316 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010317
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010318 /* This is not allowed in the sandbox. If the commands would still be
10319 * executed in the sandbox it would be OK, but it probably happens later,
10320 * when "sandbox" is no longer set. */
10321 if (check_secure())
10322 return;
10323
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010324 keys = get_tv_string(&argvars[0]);
10325 if (*keys != NUL)
10326 {
10327 if (argvars[1].v_type != VAR_UNKNOWN)
10328 {
10329 flags = get_tv_string_buf(&argvars[1], nbuf);
10330 for ( ; *flags != NUL; ++flags)
10331 {
10332 switch (*flags)
10333 {
10334 case 'n': remap = FALSE; break;
10335 case 'm': remap = TRUE; break;
10336 case 't': typed = TRUE; break;
10337 }
10338 }
10339 }
10340
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010341 /* Need to escape K_SPECIAL and CSI before putting the string in the
10342 * typeahead buffer. */
10343 keys_esc = vim_strsave_escape_csi(keys);
10344 if (keys_esc != NULL)
10345 {
10346 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010347 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010348 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010349 if (vgetc_busy)
10350 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010351 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010352 }
10353}
10354
10355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 * "filereadable()" function
10357 */
10358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010359f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010360 typval_T *argvars;
10361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010363 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 char_u *p;
10365 int n;
10366
Bram Moolenaarc236c162008-07-13 17:41:49 +000010367#ifndef O_NONBLOCK
10368# define O_NONBLOCK 0
10369#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010370 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010371 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10372 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 {
10374 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010375 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 }
10377 else
10378 n = FALSE;
10379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010380 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381}
10382
10383/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010384 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 * rights to write into.
10386 */
10387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010388f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010389 typval_T *argvars;
10390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010392 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393}
10394
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010395static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010396
10397 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010398findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010400 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010401 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010402{
10403#ifdef FEAT_SEARCHPATH
10404 char_u *fname;
10405 char_u *fresult = NULL;
10406 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10407 char_u *p;
10408 char_u pathbuf[NUMBUFLEN];
10409 int count = 1;
10410 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010411 int error = FALSE;
10412#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010413
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010414 rettv->vval.v_string = NULL;
10415 rettv->v_type = VAR_STRING;
10416
10417#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010419
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010420 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10423 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010424 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 else
10426 {
10427 if (*p != NUL)
10428 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010431 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010433 }
10434
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010435 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10436 error = TRUE;
10437
10438 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 do
10441 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010442 if (rettv->v_type == VAR_STRING)
10443 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 fresult = find_file_in_path_option(first ? fname : NULL,
10445 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010446 0, first, path,
10447 find_what,
10448 curbuf->b_ffname,
10449 find_what == FINDFILE_DIR
10450 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010452
10453 if (fresult != NULL && rettv->v_type == VAR_LIST)
10454 list_append_string(rettv->vval.v_list, fresult, -1);
10455
10456 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010458
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010459 if (rettv->v_type == VAR_STRING)
10460 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010461#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010462}
10463
Bram Moolenaar33570922005-01-25 22:26:29 +000010464static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10465static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010466
10467/*
10468 * Implementation of map() and filter().
10469 */
10470 static void
10471filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010472 typval_T *argvars;
10473 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010474 int map;
10475{
10476 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010477 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 listitem_T *li, *nli;
10479 list_T *l = NULL;
10480 dictitem_T *di;
10481 hashtab_T *ht;
10482 hashitem_T *hi;
10483 dict_T *d = NULL;
10484 typval_T save_val;
10485 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010487 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010488 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10489 char *arg_errmsg = (map ? N_("map() argument")
10490 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010491 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010492 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010493
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010495 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010496 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010497 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010498 return;
10499 }
10500 else if (argvars[0].v_type == VAR_DICT)
10501 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010502 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010503 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010504 return;
10505 }
10506 else
10507 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010508 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010509 return;
10510 }
10511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 expr = get_tv_string_buf_chk(&argvars[1], buf);
10513 /* On type errors, the preceding call has already displayed an error
10514 * message. Avoid a misleading error message for an empty string that
10515 * was not passed as argument. */
10516 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010518 prepare_vimvar(VV_VAL, &save_val);
10519 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010520
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010521 /* We reset "did_emsg" to be able to detect whether an error
10522 * occurred during evaluation of the expression. */
10523 save_did_emsg = did_emsg;
10524 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010525
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010526 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 vimvars[VV_KEY].vv_type = VAR_STRING;
10530
10531 ht = &d->dv_hashtab;
10532 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010533 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 if (!HASHITEM_EMPTY(hi))
10537 {
10538 --todo;
10539 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010540 if (tv_check_lock(di->di_tv.v_lock,
10541 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 break;
10543 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010544 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010545 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546 break;
10547 if (!map && rem)
10548 dictitem_remove(d, di);
10549 clear_tv(&vimvars[VV_KEY].vv_tv);
10550 }
10551 }
10552 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 }
10554 else
10555 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010556 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010558 for (li = l->lv_first; li != NULL; li = nli)
10559 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010560 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010561 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010563 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010564 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010565 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010566 break;
10567 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010569 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010570 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010571 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010572
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010573 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010575
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010576 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578
10579 copy_tv(&argvars[0], rettv);
10580}
10581
10582 static int
10583filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010584 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 char_u *expr;
10586 int map;
10587 int *remp;
10588{
Bram Moolenaar33570922005-01-25 22:26:29 +000010589 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010591 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592
Bram Moolenaar33570922005-01-25 22:26:29 +000010593 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 s = expr;
10595 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010596 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 if (*s != NUL) /* check for trailing chars after expr */
10598 {
10599 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010600 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601 }
10602 if (map)
10603 {
10604 /* map(): replace the list item value */
10605 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010606 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 *tv = rettv;
10608 }
10609 else
10610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 int error = FALSE;
10612
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 /* On type error, nothing has been removed; return FAIL to stop the
10617 * loop. The error message was given by get_tv_number_chk(). */
10618 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010619 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010620 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010621 retval = OK;
10622theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010623 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010624 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010625}
10626
10627/*
10628 * "filter()" function
10629 */
10630 static void
10631f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *argvars;
10633 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010634{
10635 filter_map(argvars, rettv, FALSE);
10636}
10637
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010638/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010639 * "finddir({fname}[, {path}[, {count}]])" function
10640 */
10641 static void
10642f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010643 typval_T *argvars;
10644 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010645{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010646 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010647}
10648
10649/*
10650 * "findfile({fname}[, {path}[, {count}]])" function
10651 */
10652 static void
10653f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010654 typval_T *argvars;
10655 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010656{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010657 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010658}
10659
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010660#ifdef FEAT_FLOAT
10661/*
10662 * "float2nr({float})" function
10663 */
10664 static void
10665f_float2nr(argvars, rettv)
10666 typval_T *argvars;
10667 typval_T *rettv;
10668{
10669 float_T f;
10670
10671 if (get_float_arg(argvars, &f) == OK)
10672 {
10673 if (f < -0x7fffffff)
10674 rettv->vval.v_number = -0x7fffffff;
10675 else if (f > 0x7fffffff)
10676 rettv->vval.v_number = 0x7fffffff;
10677 else
10678 rettv->vval.v_number = (varnumber_T)f;
10679 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010680}
10681
10682/*
10683 * "floor({float})" function
10684 */
10685 static void
10686f_floor(argvars, rettv)
10687 typval_T *argvars;
10688 typval_T *rettv;
10689{
10690 float_T f;
10691
10692 rettv->v_type = VAR_FLOAT;
10693 if (get_float_arg(argvars, &f) == OK)
10694 rettv->vval.v_float = floor(f);
10695 else
10696 rettv->vval.v_float = 0.0;
10697}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010698
10699/*
10700 * "fmod()" function
10701 */
10702 static void
10703f_fmod(argvars, rettv)
10704 typval_T *argvars;
10705 typval_T *rettv;
10706{
10707 float_T fx, fy;
10708
10709 rettv->v_type = VAR_FLOAT;
10710 if (get_float_arg(argvars, &fx) == OK
10711 && get_float_arg(&argvars[1], &fy) == OK)
10712 rettv->vval.v_float = fmod(fx, fy);
10713 else
10714 rettv->vval.v_float = 0.0;
10715}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010716#endif
10717
Bram Moolenaar0d660222005-01-07 21:51:51 +000010718/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010719 * "fnameescape({string})" function
10720 */
10721 static void
10722f_fnameescape(argvars, rettv)
10723 typval_T *argvars;
10724 typval_T *rettv;
10725{
10726 rettv->vval.v_string = vim_strsave_fnameescape(
10727 get_tv_string(&argvars[0]), FALSE);
10728 rettv->v_type = VAR_STRING;
10729}
10730
10731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 * "fnamemodify({fname}, {mods})" function
10733 */
10734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010736 typval_T *argvars;
10737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738{
10739 char_u *fname;
10740 char_u *mods;
10741 int usedlen = 0;
10742 int len;
10743 char_u *fbuf = NULL;
10744 char_u buf[NUMBUFLEN];
10745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 fname = get_tv_string_chk(&argvars[0]);
10747 mods = get_tv_string_buf_chk(&argvars[1], buf);
10748 if (fname == NULL || mods == NULL)
10749 fname = NULL;
10750 else
10751 {
10752 len = (int)STRLEN(fname);
10753 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 vim_free(fbuf);
10762}
10763
Bram Moolenaar33570922005-01-25 22:26:29 +000010764static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765
10766/*
10767 * "foldclosed()" function
10768 */
10769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010771 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010772 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010773 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774{
10775#ifdef FEAT_FOLDING
10776 linenr_T lnum;
10777 linenr_T first, last;
10778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10781 {
10782 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10783 {
10784 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010785 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 return;
10789 }
10790 }
10791#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793}
10794
10795/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010796 * "foldclosed()" function
10797 */
10798 static void
10799f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010800 typval_T *argvars;
10801 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802{
10803 foldclosed_both(argvars, rettv, FALSE);
10804}
10805
10806/*
10807 * "foldclosedend()" function
10808 */
10809 static void
10810f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010811 typval_T *argvars;
10812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813{
10814 foldclosed_both(argvars, rettv, TRUE);
10815}
10816
10817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 * "foldlevel()" function
10819 */
10820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010822 typval_T *argvars UNUSED;
10823 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824{
10825#ifdef FEAT_FOLDING
10826 linenr_T lnum;
10827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832}
10833
10834/*
10835 * "foldtext()" function
10836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
10842#ifdef FEAT_FOLDING
10843 linenr_T lnum;
10844 char_u *s;
10845 char_u *r;
10846 int len;
10847 char *txt;
10848#endif
10849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->v_type = VAR_STRING;
10851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010853 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10854 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10855 <= curbuf->b_ml.ml_line_count
10856 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 {
10858 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010859 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10860 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 {
10862 if (!linewhite(lnum))
10863 break;
10864 ++lnum;
10865 }
10866
10867 /* Find interesting text in this line. */
10868 s = skipwhite(ml_get(lnum));
10869 /* skip C comment-start */
10870 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010873 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010875 {
10876 s = skipwhite(ml_get(lnum + 1));
10877 if (*s == '*')
10878 s = skipwhite(s + 1);
10879 }
10880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 txt = _("+-%s%3ld lines: ");
10882 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010883 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 + 20 /* for %3ld */
10885 + STRLEN(s))); /* concatenated */
10886 if (r != NULL)
10887 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10889 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10890 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 len = (int)STRLEN(r);
10892 STRCAT(r, s);
10893 /* remove 'foldmarker' and 'commentstring' */
10894 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 }
10897 }
10898#endif
10899}
10900
10901/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010902 * "foldtextresult(lnum)" function
10903 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010906 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010907 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010908{
10909#ifdef FEAT_FOLDING
10910 linenr_T lnum;
10911 char_u *text;
10912 char_u buf[51];
10913 foldinfo_T foldinfo;
10914 int fold_count;
10915#endif
10916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->v_type = VAR_STRING;
10918 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010919#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921 /* treat illegal types and illegal string values for {lnum} the same */
10922 if (lnum < 0)
10923 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010924 fold_count = foldedCount(curwin, lnum, &foldinfo);
10925 if (fold_count > 0)
10926 {
10927 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10928 &foldinfo, buf);
10929 if (text == buf)
10930 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010932 }
10933#endif
10934}
10935
10936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 * "foreground()" function
10938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010941 typval_T *argvars UNUSED;
10942 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944#ifdef FEAT_GUI
10945 if (gui.in_use)
10946 gui_mch_set_foreground();
10947#else
10948# ifdef WIN32
10949 win32_set_foreground();
10950# endif
10951#endif
10952}
10953
10954/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955 * "function()" function
10956 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010959 typval_T *argvars;
10960 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010961{
10962 char_u *s;
10963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010965 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010966 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010967 /* Don't check an autoload name for existence here. */
10968 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010969 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010970 else
10971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972 rettv->vval.v_string = vim_strsave(s);
10973 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010974 }
10975}
10976
10977/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010978 * "garbagecollect()" function
10979 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010980 static void
10981f_garbagecollect(argvars, rettv)
10982 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010983 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010984{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010985 /* This is postponed until we are back at the toplevel, because we may be
10986 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10987 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010988
10989 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10990 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010991}
10992
10993/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994 * "get()" function
10995 */
10996 static void
10997f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010998 typval_T *argvars;
10999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011000{
Bram Moolenaar33570922005-01-25 22:26:29 +000011001 listitem_T *li;
11002 list_T *l;
11003 dictitem_T *di;
11004 dict_T *d;
11005 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011006
Bram Moolenaare9a41262005-01-15 22:18:47 +000011007 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011008 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 int error = FALSE;
11012
11013 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11014 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011015 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011016 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011017 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011018 else if (argvars[0].v_type == VAR_DICT)
11019 {
11020 if ((d = argvars[0].vval.v_dict) != NULL)
11021 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011022 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011023 if (di != NULL)
11024 tv = &di->di_tv;
11025 }
11026 }
11027 else
11028 EMSG2(_(e_listdictarg), "get()");
11029
11030 if (tv == NULL)
11031 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011032 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011033 copy_tv(&argvars[2], rettv);
11034 }
11035 else
11036 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011037}
11038
Bram Moolenaar342337a2005-07-21 21:11:17 +000011039static 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 +000011040
11041/*
11042 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011043 * Return a range (from start to end) of lines in rettv from the specified
11044 * buffer.
11045 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011046 */
11047 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011048get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011049 buf_T *buf;
11050 linenr_T start;
11051 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011052 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011053 typval_T *rettv;
11054{
11055 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011056
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011057 if (retlist && rettv_list_alloc(rettv) == FAIL)
11058 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011059
11060 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11061 return;
11062
11063 if (!retlist)
11064 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011065 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11066 p = ml_get_buf(buf, start, FALSE);
11067 else
11068 p = (char_u *)"";
11069
11070 rettv->v_type = VAR_STRING;
11071 rettv->vval.v_string = vim_strsave(p);
11072 }
11073 else
11074 {
11075 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011076 return;
11077
11078 if (start < 1)
11079 start = 1;
11080 if (end > buf->b_ml.ml_line_count)
11081 end = buf->b_ml.ml_line_count;
11082 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011083 if (list_append_string(rettv->vval.v_list,
11084 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011085 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011086 }
11087}
11088
11089/*
11090 * "getbufline()" function
11091 */
11092 static void
11093f_getbufline(argvars, rettv)
11094 typval_T *argvars;
11095 typval_T *rettv;
11096{
11097 linenr_T lnum;
11098 linenr_T end;
11099 buf_T *buf;
11100
11101 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11102 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011103 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011104 --emsg_off;
11105
Bram Moolenaar661b1822005-07-28 22:36:45 +000011106 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011107 if (argvars[2].v_type == VAR_UNKNOWN)
11108 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011109 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011110 end = get_tv_lnum_buf(&argvars[2], buf);
11111
Bram Moolenaar342337a2005-07-21 21:11:17 +000011112 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011113}
11114
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115/*
11116 * "getbufvar()" function
11117 */
11118 static void
11119f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011120 typval_T *argvars;
11121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122{
11123 buf_T *buf;
11124 buf_T *save_curbuf;
11125 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011126 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011127 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011129 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11130 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011132 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011133
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011134 rettv->v_type = VAR_STRING;
11135 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011136
11137 if (buf != NULL && varname != NULL)
11138 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011139 /* set curbuf to be our buf, temporarily */
11140 save_curbuf = curbuf;
11141 curbuf = buf;
11142
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011144 {
11145 if (get_option_tv(&varname, rettv, TRUE) == OK)
11146 done = TRUE;
11147 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011148 else if (STRCMP(varname, "changedtick") == 0)
11149 {
11150 rettv->v_type = VAR_NUMBER;
11151 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011152 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011153 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011154 else
11155 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011156 /* Look up the variable. */
11157 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11158 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11159 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011160 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011161 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011162 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011163 done = TRUE;
11164 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011166
11167 /* restore previous notion of curbuf */
11168 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011169 }
11170
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011171 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11172 /* use the default value */
11173 copy_tv(&argvars[2], rettv);
11174
Bram Moolenaar0d660222005-01-07 21:51:51 +000011175 --emsg_off;
11176}
11177
11178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 * "getchar()" function
11180 */
11181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *argvars;
11184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185{
11186 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011189 /* Position the cursor. Needed after a message that ends in a space. */
11190 windgoto(msg_row, msg_col);
11191
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 ++no_mapping;
11193 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011194 for (;;)
11195 {
11196 if (argvars[0].v_type == VAR_UNKNOWN)
11197 /* getchar(): blocking wait. */
11198 n = safe_vgetc();
11199 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11200 /* getchar(1): only check if char avail */
11201 n = vpeekc();
11202 else if (error || vpeekc() == NUL)
11203 /* illegal argument or getchar(0) and no char avail: return zero */
11204 n = 0;
11205 else
11206 /* getchar(0) and char avail: return char */
11207 n = safe_vgetc();
11208 if (n == K_IGNORE)
11209 continue;
11210 break;
11211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 --no_mapping;
11213 --allow_keys;
11214
Bram Moolenaar219b8702006-11-01 14:32:36 +000011215 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11216 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11217 vimvars[VV_MOUSE_COL].vv_nr = 0;
11218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 if (IS_SPECIAL(n) || mod_mask != 0)
11221 {
11222 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11223 int i = 0;
11224
11225 /* Turn a special key into three bytes, plus modifier. */
11226 if (mod_mask != 0)
11227 {
11228 temp[i++] = K_SPECIAL;
11229 temp[i++] = KS_MODIFIER;
11230 temp[i++] = mod_mask;
11231 }
11232 if (IS_SPECIAL(n))
11233 {
11234 temp[i++] = K_SPECIAL;
11235 temp[i++] = K_SECOND(n);
11236 temp[i++] = K_THIRD(n);
11237 }
11238#ifdef FEAT_MBYTE
11239 else if (has_mbyte)
11240 i += (*mb_char2bytes)(n, temp + i);
11241#endif
11242 else
11243 temp[i++] = n;
11244 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->v_type = VAR_STRING;
11246 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011247
11248#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011249 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011250 {
11251 int row = mouse_row;
11252 int col = mouse_col;
11253 win_T *win;
11254 linenr_T lnum;
11255# ifdef FEAT_WINDOWS
11256 win_T *wp;
11257# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011258 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011259
11260 if (row >= 0 && col >= 0)
11261 {
11262 /* Find the window at the mouse coordinates and compute the
11263 * text position. */
11264 win = mouse_find_win(&row, &col);
11265 (void)mouse_comp_pos(win, &row, &col, &lnum);
11266# ifdef FEAT_WINDOWS
11267 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011268 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011269# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011270 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011271 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11272 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11273 }
11274 }
11275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 }
11277}
11278
11279/*
11280 * "getcharmod()" function
11281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011284 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288}
11289
11290/*
11291 * "getcmdline()" function
11292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294f_getcmdline(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->v_type = VAR_STRING;
11299 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300}
11301
11302/*
11303 * "getcmdpos()" function
11304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011307 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011310 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311}
11312
11313/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011314 * "getcmdtype()" function
11315 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011316 static void
11317f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011318 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011319 typval_T *rettv;
11320{
11321 rettv->v_type = VAR_STRING;
11322 rettv->vval.v_string = alloc(2);
11323 if (rettv->vval.v_string != NULL)
11324 {
11325 rettv->vval.v_string[0] = get_cmdline_type();
11326 rettv->vval.v_string[1] = NUL;
11327 }
11328}
11329
11330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 * "getcwd()" function
11332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011335 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011338 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011341 rettv->vval.v_string = NULL;
11342 cwd = alloc(MAXPATHL);
11343 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011345 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11346 {
11347 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011349 if (rettv->vval.v_string != NULL)
11350 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011352 }
11353 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 }
11355}
11356
11357/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011358 * "getfontname()" function
11359 */
11360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 rettv->v_type = VAR_STRING;
11366 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011367#ifdef FEAT_GUI
11368 if (gui.in_use)
11369 {
11370 GuiFont font;
11371 char_u *name = NULL;
11372
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011373 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011374 {
11375 /* Get the "Normal" font. Either the name saved by
11376 * hl_set_font_name() or from the font ID. */
11377 font = gui.norm_font;
11378 name = hl_get_font_name();
11379 }
11380 else
11381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011383 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11384 return;
11385 font = gui_mch_get_font(name, FALSE);
11386 if (font == NOFONT)
11387 return; /* Invalid font name, return empty string. */
11388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011390 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011391 gui_mch_free_font(font);
11392 }
11393#endif
11394}
11395
11396/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011397 * "getfperm({fname})" function
11398 */
11399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011401 typval_T *argvars;
11402 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011403{
11404 char_u *fname;
11405 struct stat st;
11406 char_u *perm = NULL;
11407 char_u flags[] = "rwx";
11408 int i;
11409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011413 if (mch_stat((char *)fname, &st) >= 0)
11414 {
11415 perm = vim_strsave((char_u *)"---------");
11416 if (perm != NULL)
11417 {
11418 for (i = 0; i < 9; i++)
11419 {
11420 if (st.st_mode & (1 << (8 - i)))
11421 perm[i] = flags[i % 3];
11422 }
11423 }
11424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011426}
11427
11428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 * "getfsize({fname})" function
11430 */
11431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011432f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011433 typval_T *argvars;
11434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435{
11436 char_u *fname;
11437 struct stat st;
11438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442
11443 if (mch_stat((char *)fname, &st) >= 0)
11444 {
11445 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011450
11451 /* non-perfect check for overflow */
11452 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11453 rettv->vval.v_number = -2;
11454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 }
11456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458}
11459
11460/*
11461 * "getftime({fname})" function
11462 */
11463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 typval_T *argvars;
11466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467{
11468 char_u *fname;
11469 struct stat st;
11470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472
11473 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477}
11478
11479/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011480 * "getftype({fname})" function
11481 */
11482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011484 typval_T *argvars;
11485 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011486{
11487 char_u *fname;
11488 struct stat st;
11489 char_u *type = NULL;
11490 char *t;
11491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011495 if (mch_lstat((char *)fname, &st) >= 0)
11496 {
11497#ifdef S_ISREG
11498 if (S_ISREG(st.st_mode))
11499 t = "file";
11500 else if (S_ISDIR(st.st_mode))
11501 t = "dir";
11502# ifdef S_ISLNK
11503 else if (S_ISLNK(st.st_mode))
11504 t = "link";
11505# endif
11506# ifdef S_ISBLK
11507 else if (S_ISBLK(st.st_mode))
11508 t = "bdev";
11509# endif
11510# ifdef S_ISCHR
11511 else if (S_ISCHR(st.st_mode))
11512 t = "cdev";
11513# endif
11514# ifdef S_ISFIFO
11515 else if (S_ISFIFO(st.st_mode))
11516 t = "fifo";
11517# endif
11518# ifdef S_ISSOCK
11519 else if (S_ISSOCK(st.st_mode))
11520 t = "fifo";
11521# endif
11522 else
11523 t = "other";
11524#else
11525# ifdef S_IFMT
11526 switch (st.st_mode & S_IFMT)
11527 {
11528 case S_IFREG: t = "file"; break;
11529 case S_IFDIR: t = "dir"; break;
11530# ifdef S_IFLNK
11531 case S_IFLNK: t = "link"; break;
11532# endif
11533# ifdef S_IFBLK
11534 case S_IFBLK: t = "bdev"; break;
11535# endif
11536# ifdef S_IFCHR
11537 case S_IFCHR: t = "cdev"; break;
11538# endif
11539# ifdef S_IFIFO
11540 case S_IFIFO: t = "fifo"; break;
11541# endif
11542# ifdef S_IFSOCK
11543 case S_IFSOCK: t = "socket"; break;
11544# endif
11545 default: t = "other";
11546 }
11547# else
11548 if (mch_isdir(fname))
11549 t = "dir";
11550 else
11551 t = "file";
11552# endif
11553#endif
11554 type = vim_strsave((char_u *)t);
11555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011557}
11558
11559/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011560 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011561 */
11562 static void
11563f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011564 typval_T *argvars;
11565 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011566{
11567 linenr_T lnum;
11568 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011569 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011570
11571 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011572 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011573 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011574 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011575 retlist = FALSE;
11576 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011578 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011579 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 retlist = TRUE;
11581 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011582
Bram Moolenaar342337a2005-07-21 21:11:17 +000011583 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011584}
11585
11586/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011587 * "getmatches()" function
11588 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011589 static void
11590f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011591 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011592 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011593{
11594#ifdef FEAT_SEARCH_EXTRA
11595 dict_T *dict;
11596 matchitem_T *cur = curwin->w_match_head;
11597
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011598 if (rettv_list_alloc(rettv) == OK)
11599 {
11600 while (cur != NULL)
11601 {
11602 dict = dict_alloc();
11603 if (dict == NULL)
11604 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011605 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11606 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11607 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11608 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11609 list_append_dict(rettv->vval.v_list, dict);
11610 cur = cur->next;
11611 }
11612 }
11613#endif
11614}
11615
11616/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011617 * "getpid()" function
11618 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011619 static void
11620f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011621 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011622 typval_T *rettv;
11623{
11624 rettv->vval.v_number = mch_get_pid();
11625}
11626
11627/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011628 * "getpos(string)" function
11629 */
11630 static void
11631f_getpos(argvars, rettv)
11632 typval_T *argvars;
11633 typval_T *rettv;
11634{
11635 pos_T *fp;
11636 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011637 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011638
11639 if (rettv_list_alloc(rettv) == OK)
11640 {
11641 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011642 fp = var2fpos(&argvars[0], TRUE, &fnum);
11643 if (fnum != -1)
11644 list_append_number(l, (varnumber_T)fnum);
11645 else
11646 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011647 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11648 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011649 list_append_number(l, (fp != NULL)
11650 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011651 : (varnumber_T)0);
11652 list_append_number(l,
11653#ifdef FEAT_VIRTUALEDIT
11654 (fp != NULL) ? (varnumber_T)fp->coladd :
11655#endif
11656 (varnumber_T)0);
11657 }
11658 else
11659 rettv->vval.v_number = FALSE;
11660}
11661
11662/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011663 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011664 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011665 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011666f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011667 typval_T *argvars UNUSED;
11668 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011669{
11670#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011671 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011672#endif
11673
Bram Moolenaar2641f772005-03-25 21:58:17 +000011674#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011675 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011676 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011677 wp = NULL;
11678 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11679 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011680 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011681 if (wp == NULL)
11682 return;
11683 }
11684
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011685 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011686 }
11687#endif
11688}
11689
11690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 * "getreg()" function
11692 */
11693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011695 typval_T *argvars;
11696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697{
11698 char_u *strregname;
11699 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011700 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011703 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 strregname = get_tv_string_chk(&argvars[0]);
11706 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011708 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011711 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 regname = (strregname == NULL ? '"' : *strregname);
11713 if (regname == 0)
11714 regname = '"';
11715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 rettv->vval.v_string = error ? NULL :
11718 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719}
11720
11721/*
11722 * "getregtype()" function
11723 */
11724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011726 typval_T *argvars;
11727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728{
11729 char_u *strregname;
11730 int regname;
11731 char_u buf[NUMBUFLEN + 2];
11732 long reglen = 0;
11733
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011734 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011735 {
11736 strregname = get_tv_string_chk(&argvars[0]);
11737 if (strregname == NULL) /* type error; errmsg already given */
11738 {
11739 rettv->v_type = VAR_STRING;
11740 rettv->vval.v_string = NULL;
11741 return;
11742 }
11743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 else
11745 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747
11748 regname = (strregname == NULL ? '"' : *strregname);
11749 if (regname == 0)
11750 regname = '"';
11751
11752 buf[0] = NUL;
11753 buf[1] = NUL;
11754 switch (get_reg_type(regname, &reglen))
11755 {
11756 case MLINE: buf[0] = 'V'; break;
11757 case MCHAR: buf[0] = 'v'; break;
11758#ifdef FEAT_VISUAL
11759 case MBLOCK:
11760 buf[0] = Ctrl_V;
11761 sprintf((char *)buf + 1, "%ld", reglen + 1);
11762 break;
11763#endif
11764 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->v_type = VAR_STRING;
11766 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767}
11768
11769/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011770 * "gettabvar()" function
11771 */
11772 static void
11773f_gettabvar(argvars, rettv)
11774 typval_T *argvars;
11775 typval_T *rettv;
11776{
11777 tabpage_T *tp;
11778 dictitem_T *v;
11779 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011780 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011781
11782 rettv->v_type = VAR_STRING;
11783 rettv->vval.v_string = NULL;
11784
11785 varname = get_tv_string_chk(&argvars[1]);
11786 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11787 if (tp != NULL && varname != NULL)
11788 {
11789 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011790 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011791 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011792 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011793 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011794 done = TRUE;
11795 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011796 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011797
11798 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011799 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011800}
11801
11802/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011803 * "gettabwinvar()" function
11804 */
11805 static void
11806f_gettabwinvar(argvars, rettv)
11807 typval_T *argvars;
11808 typval_T *rettv;
11809{
11810 getwinvar(argvars, rettv, 1);
11811}
11812
11813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 * "getwinposx()" function
11815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011818 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822#ifdef FEAT_GUI
11823 if (gui.in_use)
11824 {
11825 int x, y;
11826
11827 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 }
11830#endif
11831}
11832
11833/*
11834 * "getwinposy()" function
11835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011838 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842#ifdef FEAT_GUI
11843 if (gui.in_use)
11844 {
11845 int x, y;
11846
11847 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 }
11850#endif
11851}
11852
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011853/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011854 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011855 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011856 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011857find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011858 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011859 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011860{
11861#ifdef FEAT_WINDOWS
11862 win_T *wp;
11863#endif
11864 int nr;
11865
11866 nr = get_tv_number_chk(vp, NULL);
11867
11868#ifdef FEAT_WINDOWS
11869 if (nr < 0)
11870 return NULL;
11871 if (nr == 0)
11872 return curwin;
11873
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011874 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11875 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011876 if (--nr <= 0)
11877 break;
11878 return wp;
11879#else
11880 if (nr == 0 || nr == 1)
11881 return curwin;
11882 return NULL;
11883#endif
11884}
11885
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886/*
11887 * "getwinvar()" function
11888 */
11889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011891 typval_T *argvars;
11892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011894 getwinvar(argvars, rettv, 0);
11895}
11896
11897/*
11898 * getwinvar() and gettabwinvar()
11899 */
11900 static void
11901getwinvar(argvars, rettv, off)
11902 typval_T *argvars;
11903 typval_T *rettv;
11904 int off; /* 1 for gettabwinvar() */
11905{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 win_T *win, *oldcurwin;
11907 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011909 tabpage_T *tp;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011910 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011912#ifdef FEAT_WINDOWS
11913 if (off == 1)
11914 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11915 else
11916 tp = curtab;
11917#endif
11918 win = find_win_by_nr(&argvars[off], tp);
11919 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011920 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011922 rettv->v_type = VAR_STRING;
11923 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924
11925 if (win != NULL && varname != NULL)
11926 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011927 /* Set curwin to be our win, temporarily. Also set curbuf, so
11928 * that we can get buffer-local options. */
11929 oldcurwin = curwin;
11930 curwin = win;
11931 curbuf = win->w_buffer;
11932
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011934 {
11935 if (get_option_tv(&varname, rettv, 1) == OK)
11936 done = TRUE;
11937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 else
11939 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011940 /* Look up the variable. */
11941 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11942 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011944 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011945 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011946 done = TRUE;
11947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011949
11950 /* restore previous notion of curwin */
11951 curwin = oldcurwin;
11952 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 }
11954
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011955 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11956 /* use the default return value */
11957 copy_tv(&argvars[off + 2], rettv);
11958
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 --emsg_off;
11960}
11961
11962/*
11963 * "glob()" function
11964 */
11965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011967 typval_T *argvars;
11968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011970 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011972 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011974 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011975 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011977 if (argvars[1].v_type != VAR_UNKNOWN)
11978 {
11979 if (get_tv_number_chk(&argvars[1], &error))
11980 options |= WILD_KEEP_ALL;
11981 if (argvars[2].v_type != VAR_UNKNOWN
11982 && get_tv_number_chk(&argvars[2], &error))
11983 {
11984 rettv->v_type = VAR_LIST;
11985 rettv->vval.v_list = NULL;
11986 }
11987 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011988 if (!error)
11989 {
11990 ExpandInit(&xpc);
11991 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011992 if (p_wic)
11993 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011994 if (rettv->v_type == VAR_STRING)
11995 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011996 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011997 else if (rettv_list_alloc(rettv) != FAIL)
11998 {
11999 int i;
12000
12001 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12002 NULL, options, WILD_ALL_KEEP);
12003 for (i = 0; i < xpc.xp_numfiles; i++)
12004 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12005
12006 ExpandCleanup(&xpc);
12007 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012008 }
12009 else
12010 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011}
12012
12013/*
12014 * "globpath()" function
12015 */
12016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012017f_globpath(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{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012021 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012023 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012024 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012026 /* When the optional second argument is non-zero, don't remove matches
12027 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12028 if (argvars[2].v_type != VAR_UNKNOWN
12029 && get_tv_number_chk(&argvars[2], &error))
12030 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012032 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012033 rettv->vval.v_string = NULL;
12034 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012035 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12036 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037}
12038
12039/*
12040 * "has()" function
12041 */
12042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012044 typval_T *argvars;
12045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046{
12047 int i;
12048 char_u *name;
12049 int n = FALSE;
12050 static char *(has_list[]) =
12051 {
12052#ifdef AMIGA
12053 "amiga",
12054# ifdef FEAT_ARP
12055 "arp",
12056# endif
12057#endif
12058#ifdef __BEOS__
12059 "beos",
12060#endif
12061#ifdef MSDOS
12062# ifdef DJGPP
12063 "dos32",
12064# else
12065 "dos16",
12066# endif
12067#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012068#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 "mac",
12070#endif
12071#if defined(MACOS_X_UNIX)
12072 "macunix",
12073#endif
12074#ifdef OS2
12075 "os2",
12076#endif
12077#ifdef __QNX__
12078 "qnx",
12079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080#ifdef UNIX
12081 "unix",
12082#endif
12083#ifdef VMS
12084 "vms",
12085#endif
12086#ifdef WIN16
12087 "win16",
12088#endif
12089#ifdef WIN32
12090 "win32",
12091#endif
12092#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12093 "win32unix",
12094#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012095#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 "win64",
12097#endif
12098#ifdef EBCDIC
12099 "ebcdic",
12100#endif
12101#ifndef CASE_INSENSITIVE_FILENAME
12102 "fname_case",
12103#endif
12104#ifdef FEAT_ARABIC
12105 "arabic",
12106#endif
12107#ifdef FEAT_AUTOCMD
12108 "autocmd",
12109#endif
12110#ifdef FEAT_BEVAL
12111 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012112# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12113 "balloon_multiline",
12114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#endif
12116#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12117 "builtin_terms",
12118# ifdef ALL_BUILTIN_TCAPS
12119 "all_builtin_terms",
12120# endif
12121#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012122#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12123 || defined(FEAT_GUI_W32) \
12124 || defined(FEAT_GUI_MOTIF))
12125 "browsefilter",
12126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127#ifdef FEAT_BYTEOFF
12128 "byte_offset",
12129#endif
12130#ifdef FEAT_CINDENT
12131 "cindent",
12132#endif
12133#ifdef FEAT_CLIENTSERVER
12134 "clientserver",
12135#endif
12136#ifdef FEAT_CLIPBOARD
12137 "clipboard",
12138#endif
12139#ifdef FEAT_CMDL_COMPL
12140 "cmdline_compl",
12141#endif
12142#ifdef FEAT_CMDHIST
12143 "cmdline_hist",
12144#endif
12145#ifdef FEAT_COMMENTS
12146 "comments",
12147#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012148#ifdef FEAT_CONCEAL
12149 "conceal",
12150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151#ifdef FEAT_CRYPT
12152 "cryptv",
12153#endif
12154#ifdef FEAT_CSCOPE
12155 "cscope",
12156#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012157#ifdef FEAT_CURSORBIND
12158 "cursorbind",
12159#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012160#ifdef CURSOR_SHAPE
12161 "cursorshape",
12162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163#ifdef DEBUG
12164 "debug",
12165#endif
12166#ifdef FEAT_CON_DIALOG
12167 "dialog_con",
12168#endif
12169#ifdef FEAT_GUI_DIALOG
12170 "dialog_gui",
12171#endif
12172#ifdef FEAT_DIFF
12173 "diff",
12174#endif
12175#ifdef FEAT_DIGRAPHS
12176 "digraphs",
12177#endif
12178#ifdef FEAT_DND
12179 "dnd",
12180#endif
12181#ifdef FEAT_EMACS_TAGS
12182 "emacs_tags",
12183#endif
12184 "eval", /* always present, of course! */
12185#ifdef FEAT_EX_EXTRA
12186 "ex_extra",
12187#endif
12188#ifdef FEAT_SEARCH_EXTRA
12189 "extra_search",
12190#endif
12191#ifdef FEAT_FKMAP
12192 "farsi",
12193#endif
12194#ifdef FEAT_SEARCHPATH
12195 "file_in_path",
12196#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012197#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012198 "filterpipe",
12199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200#ifdef FEAT_FIND_ID
12201 "find_in_path",
12202#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012203#ifdef FEAT_FLOAT
12204 "float",
12205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206#ifdef FEAT_FOLDING
12207 "folding",
12208#endif
12209#ifdef FEAT_FOOTER
12210 "footer",
12211#endif
12212#if !defined(USE_SYSTEM) && defined(UNIX)
12213 "fork",
12214#endif
12215#ifdef FEAT_GETTEXT
12216 "gettext",
12217#endif
12218#ifdef FEAT_GUI
12219 "gui",
12220#endif
12221#ifdef FEAT_GUI_ATHENA
12222# ifdef FEAT_GUI_NEXTAW
12223 "gui_neXtaw",
12224# else
12225 "gui_athena",
12226# endif
12227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228#ifdef FEAT_GUI_GTK
12229 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012232#ifdef FEAT_GUI_GNOME
12233 "gui_gnome",
12234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235#ifdef FEAT_GUI_MAC
12236 "gui_mac",
12237#endif
12238#ifdef FEAT_GUI_MOTIF
12239 "gui_motif",
12240#endif
12241#ifdef FEAT_GUI_PHOTON
12242 "gui_photon",
12243#endif
12244#ifdef FEAT_GUI_W16
12245 "gui_win16",
12246#endif
12247#ifdef FEAT_GUI_W32
12248 "gui_win32",
12249#endif
12250#ifdef FEAT_HANGULIN
12251 "hangul_input",
12252#endif
12253#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12254 "iconv",
12255#endif
12256#ifdef FEAT_INS_EXPAND
12257 "insert_expand",
12258#endif
12259#ifdef FEAT_JUMPLIST
12260 "jumplist",
12261#endif
12262#ifdef FEAT_KEYMAP
12263 "keymap",
12264#endif
12265#ifdef FEAT_LANGMAP
12266 "langmap",
12267#endif
12268#ifdef FEAT_LIBCALL
12269 "libcall",
12270#endif
12271#ifdef FEAT_LINEBREAK
12272 "linebreak",
12273#endif
12274#ifdef FEAT_LISP
12275 "lispindent",
12276#endif
12277#ifdef FEAT_LISTCMDS
12278 "listcmds",
12279#endif
12280#ifdef FEAT_LOCALMAP
12281 "localmap",
12282#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012283#ifdef FEAT_LUA
12284# ifndef DYNAMIC_LUA
12285 "lua",
12286# endif
12287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288#ifdef FEAT_MENU
12289 "menu",
12290#endif
12291#ifdef FEAT_SESSION
12292 "mksession",
12293#endif
12294#ifdef FEAT_MODIFY_FNAME
12295 "modify_fname",
12296#endif
12297#ifdef FEAT_MOUSE
12298 "mouse",
12299#endif
12300#ifdef FEAT_MOUSESHAPE
12301 "mouseshape",
12302#endif
12303#if defined(UNIX) || defined(VMS)
12304# ifdef FEAT_MOUSE_DEC
12305 "mouse_dec",
12306# endif
12307# ifdef FEAT_MOUSE_GPM
12308 "mouse_gpm",
12309# endif
12310# ifdef FEAT_MOUSE_JSB
12311 "mouse_jsbterm",
12312# endif
12313# ifdef FEAT_MOUSE_NET
12314 "mouse_netterm",
12315# endif
12316# ifdef FEAT_MOUSE_PTERM
12317 "mouse_pterm",
12318# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012319# ifdef FEAT_MOUSE_SGR
12320 "mouse_sgr",
12321# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012322# ifdef FEAT_SYSMOUSE
12323 "mouse_sysmouse",
12324# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012325# ifdef FEAT_MOUSE_URXVT
12326 "mouse_urxvt",
12327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328# ifdef FEAT_MOUSE_XTERM
12329 "mouse_xterm",
12330# endif
12331#endif
12332#ifdef FEAT_MBYTE
12333 "multi_byte",
12334#endif
12335#ifdef FEAT_MBYTE_IME
12336 "multi_byte_ime",
12337#endif
12338#ifdef FEAT_MULTI_LANG
12339 "multi_lang",
12340#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012341#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012342#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012343 "mzscheme",
12344#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346#ifdef FEAT_OLE
12347 "ole",
12348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349#ifdef FEAT_PATH_EXTRA
12350 "path_extra",
12351#endif
12352#ifdef FEAT_PERL
12353#ifndef DYNAMIC_PERL
12354 "perl",
12355#endif
12356#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012357#ifdef FEAT_PERSISTENT_UNDO
12358 "persistent_undo",
12359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360#ifdef FEAT_PYTHON
12361#ifndef DYNAMIC_PYTHON
12362 "python",
12363#endif
12364#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012365#ifdef FEAT_PYTHON3
12366#ifndef DYNAMIC_PYTHON3
12367 "python3",
12368#endif
12369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370#ifdef FEAT_POSTSCRIPT
12371 "postscript",
12372#endif
12373#ifdef FEAT_PRINTER
12374 "printer",
12375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012376#ifdef FEAT_PROFILE
12377 "profile",
12378#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012379#ifdef FEAT_RELTIME
12380 "reltime",
12381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382#ifdef FEAT_QUICKFIX
12383 "quickfix",
12384#endif
12385#ifdef FEAT_RIGHTLEFT
12386 "rightleft",
12387#endif
12388#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12389 "ruby",
12390#endif
12391#ifdef FEAT_SCROLLBIND
12392 "scrollbind",
12393#endif
12394#ifdef FEAT_CMDL_INFO
12395 "showcmd",
12396 "cmdline_info",
12397#endif
12398#ifdef FEAT_SIGNS
12399 "signs",
12400#endif
12401#ifdef FEAT_SMARTINDENT
12402 "smartindent",
12403#endif
12404#ifdef FEAT_SNIFF
12405 "sniff",
12406#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012407#ifdef STARTUPTIME
12408 "startuptime",
12409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410#ifdef FEAT_STL_OPT
12411 "statusline",
12412#endif
12413#ifdef FEAT_SUN_WORKSHOP
12414 "sun_workshop",
12415#endif
12416#ifdef FEAT_NETBEANS_INTG
12417 "netbeans_intg",
12418#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012419#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012420 "spell",
12421#endif
12422#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 "syntax",
12424#endif
12425#if defined(USE_SYSTEM) || !defined(UNIX)
12426 "system",
12427#endif
12428#ifdef FEAT_TAG_BINS
12429 "tag_binary",
12430#endif
12431#ifdef FEAT_TAG_OLDSTATIC
12432 "tag_old_static",
12433#endif
12434#ifdef FEAT_TAG_ANYWHITE
12435 "tag_any_white",
12436#endif
12437#ifdef FEAT_TCL
12438# ifndef DYNAMIC_TCL
12439 "tcl",
12440# endif
12441#endif
12442#ifdef TERMINFO
12443 "terminfo",
12444#endif
12445#ifdef FEAT_TERMRESPONSE
12446 "termresponse",
12447#endif
12448#ifdef FEAT_TEXTOBJ
12449 "textobjects",
12450#endif
12451#ifdef HAVE_TGETENT
12452 "tgetent",
12453#endif
12454#ifdef FEAT_TITLE
12455 "title",
12456#endif
12457#ifdef FEAT_TOOLBAR
12458 "toolbar",
12459#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012460#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12461 "unnamedplus",
12462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463#ifdef FEAT_USR_CMDS
12464 "user-commands", /* was accidentally included in 5.4 */
12465 "user_commands",
12466#endif
12467#ifdef FEAT_VIMINFO
12468 "viminfo",
12469#endif
12470#ifdef FEAT_VERTSPLIT
12471 "vertsplit",
12472#endif
12473#ifdef FEAT_VIRTUALEDIT
12474 "virtualedit",
12475#endif
12476#ifdef FEAT_VISUAL
12477 "visual",
12478#endif
12479#ifdef FEAT_VISUALEXTRA
12480 "visualextra",
12481#endif
12482#ifdef FEAT_VREPLACE
12483 "vreplace",
12484#endif
12485#ifdef FEAT_WILDIGN
12486 "wildignore",
12487#endif
12488#ifdef FEAT_WILDMENU
12489 "wildmenu",
12490#endif
12491#ifdef FEAT_WINDOWS
12492 "windows",
12493#endif
12494#ifdef FEAT_WAK
12495 "winaltkeys",
12496#endif
12497#ifdef FEAT_WRITEBACKUP
12498 "writebackup",
12499#endif
12500#ifdef FEAT_XIM
12501 "xim",
12502#endif
12503#ifdef FEAT_XFONTSET
12504 "xfontset",
12505#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012506#ifdef FEAT_XPM_W32
12507 "xpm_w32",
12508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509#ifdef USE_XSMP
12510 "xsmp",
12511#endif
12512#ifdef USE_XSMP_INTERACT
12513 "xsmp_interact",
12514#endif
12515#ifdef FEAT_XCLIPBOARD
12516 "xterm_clipboard",
12517#endif
12518#ifdef FEAT_XTERM_SAVE
12519 "xterm_save",
12520#endif
12521#if defined(UNIX) && defined(FEAT_X11)
12522 "X11",
12523#endif
12524 NULL
12525 };
12526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012527 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 for (i = 0; has_list[i] != NULL; ++i)
12529 if (STRICMP(name, has_list[i]) == 0)
12530 {
12531 n = TRUE;
12532 break;
12533 }
12534
12535 if (n == FALSE)
12536 {
12537 if (STRNICMP(name, "patch", 5) == 0)
12538 n = has_patch(atoi((char *)name + 5));
12539 else if (STRICMP(name, "vim_starting") == 0)
12540 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012541#ifdef FEAT_MBYTE
12542 else if (STRICMP(name, "multi_byte_encoding") == 0)
12543 n = has_mbyte;
12544#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012545#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12546 else if (STRICMP(name, "balloon_multiline") == 0)
12547 n = multiline_balloon_available();
12548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549#ifdef DYNAMIC_TCL
12550 else if (STRICMP(name, "tcl") == 0)
12551 n = tcl_enabled(FALSE);
12552#endif
12553#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12554 else if (STRICMP(name, "iconv") == 0)
12555 n = iconv_enabled(FALSE);
12556#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012557#ifdef DYNAMIC_LUA
12558 else if (STRICMP(name, "lua") == 0)
12559 n = lua_enabled(FALSE);
12560#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012561#ifdef DYNAMIC_MZSCHEME
12562 else if (STRICMP(name, "mzscheme") == 0)
12563 n = mzscheme_enabled(FALSE);
12564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565#ifdef DYNAMIC_RUBY
12566 else if (STRICMP(name, "ruby") == 0)
12567 n = ruby_enabled(FALSE);
12568#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012569#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570#ifdef DYNAMIC_PYTHON
12571 else if (STRICMP(name, "python") == 0)
12572 n = python_enabled(FALSE);
12573#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012574#endif
12575#ifdef FEAT_PYTHON3
12576#ifdef DYNAMIC_PYTHON3
12577 else if (STRICMP(name, "python3") == 0)
12578 n = python3_enabled(FALSE);
12579#endif
12580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581#ifdef DYNAMIC_PERL
12582 else if (STRICMP(name, "perl") == 0)
12583 n = perl_enabled(FALSE);
12584#endif
12585#ifdef FEAT_GUI
12586 else if (STRICMP(name, "gui_running") == 0)
12587 n = (gui.in_use || gui.starting);
12588# ifdef FEAT_GUI_W32
12589 else if (STRICMP(name, "gui_win32s") == 0)
12590 n = gui_is_win32s();
12591# endif
12592# ifdef FEAT_BROWSE
12593 else if (STRICMP(name, "browse") == 0)
12594 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12595# endif
12596#endif
12597#ifdef FEAT_SYN_HL
12598 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012599 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600#endif
12601#if defined(WIN3264)
12602 else if (STRICMP(name, "win95") == 0)
12603 n = mch_windows95();
12604#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012605#ifdef FEAT_NETBEANS_INTG
12606 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012607 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609 }
12610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012611 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612}
12613
12614/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012615 * "has_key()" function
12616 */
12617 static void
12618f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012619 typval_T *argvars;
12620 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012621{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012622 if (argvars[0].v_type != VAR_DICT)
12623 {
12624 EMSG(_(e_dictreq));
12625 return;
12626 }
12627 if (argvars[0].vval.v_dict == NULL)
12628 return;
12629
12630 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012631 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012632}
12633
12634/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012635 * "haslocaldir()" function
12636 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012637 static void
12638f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012639 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012640 typval_T *rettv;
12641{
12642 rettv->vval.v_number = (curwin->w_localdir != NULL);
12643}
12644
12645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 * "hasmapto()" function
12647 */
12648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012650 typval_T *argvars;
12651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652{
12653 char_u *name;
12654 char_u *mode;
12655 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012656 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012659 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 mode = (char_u *)"nvo";
12661 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012663 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012664 if (argvars[2].v_type != VAR_UNKNOWN)
12665 abbr = get_tv_number(&argvars[2]);
12666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667
Bram Moolenaar2c932302006-03-18 21:42:09 +000012668 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012669 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672}
12673
12674/*
12675 * "histadd()" function
12676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012679 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681{
12682#ifdef FEAT_CMDHIST
12683 int histype;
12684 char_u *str;
12685 char_u buf[NUMBUFLEN];
12686#endif
12687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 if (check_restricted() || check_secure())
12690 return;
12691#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012692 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12693 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 if (histype >= 0)
12695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697 if (*str != NUL)
12698 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012699 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 return;
12703 }
12704 }
12705#endif
12706}
12707
12708/*
12709 * "histdel()" function
12710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012713 typval_T *argvars UNUSED;
12714 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715{
12716#ifdef FEAT_CMDHIST
12717 int n;
12718 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012721 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12722 if (str == NULL)
12723 n = 0;
12724 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012727 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012729 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 else
12732 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012733 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734 get_tv_string_buf(&argvars[1], buf));
12735 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736#endif
12737}
12738
12739/*
12740 * "histget()" function
12741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012744 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746{
12747#ifdef FEAT_CMDHIST
12748 int type;
12749 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012750 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12753 if (str == NULL)
12754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 {
12757 type = get_histtype(str);
12758 if (argvars[1].v_type == VAR_UNKNOWN)
12759 idx = get_history_idx(type);
12760 else
12761 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12762 /* -1 on type error */
12763 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012766 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769}
12770
12771/*
12772 * "histnr()" function
12773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012776 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778{
12779 int i;
12780
12781#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012782 char_u *history = get_tv_string_chk(&argvars[0]);
12783
12784 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 if (i >= HIST_CMD && i < HIST_COUNT)
12786 i = get_history_idx(i);
12787 else
12788#endif
12789 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791}
12792
12793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 * "highlightID(name)" function
12795 */
12796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012797f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012798 typval_T *argvars;
12799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802}
12803
12804/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012805 * "highlight_exists()" function
12806 */
12807 static void
12808f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012809 typval_T *argvars;
12810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012811{
12812 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12813}
12814
12815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 * "hostname()" function
12817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012820 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822{
12823 char_u hostname[256];
12824
12825 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826 rettv->v_type = VAR_STRING;
12827 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828}
12829
12830/*
12831 * iconv() function
12832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012835 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837{
12838#ifdef FEAT_MBYTE
12839 char_u buf1[NUMBUFLEN];
12840 char_u buf2[NUMBUFLEN];
12841 char_u *from, *to, *str;
12842 vimconv_T vimconv;
12843#endif
12844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 rettv->v_type = VAR_STRING;
12846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847
12848#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 str = get_tv_string(&argvars[0]);
12850 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12851 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 vimconv.vc_type = CONV_NONE;
12853 convert_setup(&vimconv, from, to);
12854
12855 /* If the encodings are equal, no conversion needed. */
12856 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860
12861 convert_setup(&vimconv, NULL, NULL);
12862 vim_free(from);
12863 vim_free(to);
12864#endif
12865}
12866
12867/*
12868 * "indent()" function
12869 */
12870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012872 typval_T *argvars;
12873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874{
12875 linenr_T lnum;
12876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882}
12883
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012884/*
12885 * "index()" function
12886 */
12887 static void
12888f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012889 typval_T *argvars;
12890 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012891{
Bram Moolenaar33570922005-01-25 22:26:29 +000012892 list_T *l;
12893 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012894 long idx = 0;
12895 int ic = FALSE;
12896
12897 rettv->vval.v_number = -1;
12898 if (argvars[0].v_type != VAR_LIST)
12899 {
12900 EMSG(_(e_listreq));
12901 return;
12902 }
12903 l = argvars[0].vval.v_list;
12904 if (l != NULL)
12905 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012906 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012907 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012909 int error = FALSE;
12910
Bram Moolenaar758711c2005-02-02 23:11:38 +000012911 /* Start at specified item. Use the cached index that list_find()
12912 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012913 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012914 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012915 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012916 ic = get_tv_number_chk(&argvars[3], &error);
12917 if (error)
12918 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012919 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012920
Bram Moolenaar758711c2005-02-02 23:11:38 +000012921 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012922 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012923 {
12924 rettv->vval.v_number = idx;
12925 break;
12926 }
12927 }
12928}
12929
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930static int inputsecret_flag = 0;
12931
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012932static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12933
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012935 * This function is used by f_input() and f_inputdialog() functions. The third
12936 * argument to f_input() specifies the type of completion to use at the
12937 * prompt. The third argument to f_inputdialog() specifies the value to return
12938 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939 */
12940 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012941get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012942 typval_T *argvars;
12943 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012944 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012946 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 char_u *p = NULL;
12948 int c;
12949 char_u buf[NUMBUFLEN];
12950 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012951 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012952 int xp_type = EXPAND_NOTHING;
12953 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957
12958#ifdef NO_CONSOLE_INPUT
12959 /* While starting up, there is no place to enter text. */
12960 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962#endif
12963
12964 cmd_silent = FALSE; /* Want to see the prompt. */
12965 if (prompt != NULL)
12966 {
12967 /* Only the part of the message after the last NL is considered as
12968 * prompt for the command line */
12969 p = vim_strrchr(prompt, '\n');
12970 if (p == NULL)
12971 p = prompt;
12972 else
12973 {
12974 ++p;
12975 c = *p;
12976 *p = NUL;
12977 msg_start();
12978 msg_clr_eos();
12979 msg_puts_attr(prompt, echo_attr);
12980 msg_didout = FALSE;
12981 msg_starthere();
12982 *p = c;
12983 }
12984 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012986 if (argvars[1].v_type != VAR_UNKNOWN)
12987 {
12988 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12989 if (defstr != NULL)
12990 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012992 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012993 {
12994 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012995 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012996 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012997
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012998 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012999 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013000
Bram Moolenaar4463f292005-09-25 22:20:24 +000013001 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13002 if (xp_name == NULL)
13003 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013004
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013005 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013006
Bram Moolenaar4463f292005-09-25 22:20:24 +000013007 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13008 &xp_arg) == FAIL)
13009 return;
13010 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013011 }
13012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013 if (defstr != NULL)
13014 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013015 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13016 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013017 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013018 && argvars[1].v_type != VAR_UNKNOWN
13019 && argvars[2].v_type != VAR_UNKNOWN)
13020 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13021 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013022
13023 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013025 /* since the user typed this, no need to wait for return */
13026 need_wait_return = FALSE;
13027 msg_didout = FALSE;
13028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 cmd_silent = cmd_silent_save;
13030}
13031
13032/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013033 * "input()" function
13034 * Also handles inputsecret() when inputsecret is set.
13035 */
13036 static void
13037f_input(argvars, rettv)
13038 typval_T *argvars;
13039 typval_T *rettv;
13040{
13041 get_user_input(argvars, rettv, FALSE);
13042}
13043
13044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 * "inputdialog()" function
13046 */
13047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013049 typval_T *argvars;
13050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051{
13052#if defined(FEAT_GUI_TEXTDIALOG)
13053 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13054 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13055 {
13056 char_u *message;
13057 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013058 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 message = get_tv_string_chk(&argvars[0]);
13061 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013062 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013063 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064 else
13065 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013066 if (message != NULL && defstr != NULL
13067 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013068 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 else
13071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013072 if (message != NULL && defstr != NULL
13073 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013074 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013075 rettv->vval.v_string = vim_strsave(
13076 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 }
13082 else
13083#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013084 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085}
13086
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013087/*
13088 * "inputlist()" function
13089 */
13090 static void
13091f_inputlist(argvars, rettv)
13092 typval_T *argvars;
13093 typval_T *rettv;
13094{
13095 listitem_T *li;
13096 int selected;
13097 int mouse_used;
13098
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013099#ifdef NO_CONSOLE_INPUT
13100 /* While starting up, there is no place to enter text. */
13101 if (no_console_input())
13102 return;
13103#endif
13104 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13105 {
13106 EMSG2(_(e_listarg), "inputlist()");
13107 return;
13108 }
13109
13110 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013111 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013112 lines_left = Rows; /* avoid more prompt */
13113 msg_scroll = TRUE;
13114 msg_clr_eos();
13115
13116 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13117 {
13118 msg_puts(get_tv_string(&li->li_tv));
13119 msg_putchar('\n');
13120 }
13121
13122 /* Ask for choice. */
13123 selected = prompt_for_number(&mouse_used);
13124 if (mouse_used)
13125 selected -= lines_left;
13126
13127 rettv->vval.v_number = selected;
13128}
13129
13130
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13132
13133/*
13134 * "inputrestore()" function
13135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013138 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140{
13141 if (ga_userinput.ga_len > 0)
13142 {
13143 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13145 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013146 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 }
13148 else if (p_verbose > 1)
13149 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013150 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013151 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 }
13153}
13154
13155/*
13156 * "inputsave()" function
13157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013159f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013163 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 if (ga_grow(&ga_userinput, 1) == OK)
13165 {
13166 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13167 + ga_userinput.ga_len);
13168 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013169 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 }
13171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173}
13174
13175/*
13176 * "inputsecret()" function
13177 */
13178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 typval_T *argvars;
13181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182{
13183 ++cmdline_star;
13184 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 --cmdline_star;
13187 --inputsecret_flag;
13188}
13189
13190/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013191 * "insert()" function
13192 */
13193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013195 typval_T *argvars;
13196 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013197{
13198 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013199 listitem_T *item;
13200 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013202
13203 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013204 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013205 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013206 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013207 {
13208 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013209 before = get_tv_number_chk(&argvars[2], &error);
13210 if (error)
13211 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013212
Bram Moolenaar758711c2005-02-02 23:11:38 +000013213 if (before == l->lv_len)
13214 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013215 else
13216 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013217 item = list_find(l, before);
13218 if (item == NULL)
13219 {
13220 EMSGN(_(e_listidx), before);
13221 l = NULL;
13222 }
13223 }
13224 if (l != NULL)
13225 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013226 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013227 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228 }
13229 }
13230}
13231
13232/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013233 * "invert(expr)" function
13234 */
13235 static void
13236f_invert(argvars, rettv)
13237 typval_T *argvars;
13238 typval_T *rettv;
13239{
13240 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13241}
13242
13243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 * "isdirectory()" function
13245 */
13246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013248 typval_T *argvars;
13249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252}
13253
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013254/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013255 * "islocked()" function
13256 */
13257 static void
13258f_islocked(argvars, rettv)
13259 typval_T *argvars;
13260 typval_T *rettv;
13261{
13262 lval_T lv;
13263 char_u *end;
13264 dictitem_T *di;
13265
13266 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013267 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13268 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013269 if (end != NULL && lv.ll_name != NULL)
13270 {
13271 if (*end != NUL)
13272 EMSG(_(e_trailing));
13273 else
13274 {
13275 if (lv.ll_tv == NULL)
13276 {
13277 if (check_changedtick(lv.ll_name))
13278 rettv->vval.v_number = 1; /* always locked */
13279 else
13280 {
13281 di = find_var(lv.ll_name, NULL);
13282 if (di != NULL)
13283 {
13284 /* Consider a variable locked when:
13285 * 1. the variable itself is locked
13286 * 2. the value of the variable is locked.
13287 * 3. the List or Dict value is locked.
13288 */
13289 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13290 || tv_islocked(&di->di_tv));
13291 }
13292 }
13293 }
13294 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013295 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013296 else if (lv.ll_newkey != NULL)
13297 EMSG2(_(e_dictkey), lv.ll_newkey);
13298 else if (lv.ll_list != NULL)
13299 /* List item. */
13300 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13301 else
13302 /* Dictionary item. */
13303 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13304 }
13305 }
13306
13307 clear_lval(&lv);
13308}
13309
Bram Moolenaar33570922005-01-25 22:26:29 +000013310static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013311
13312/*
13313 * Turn a dict into a list:
13314 * "what" == 0: list of keys
13315 * "what" == 1: list of values
13316 * "what" == 2: list of items
13317 */
13318 static void
13319dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013320 typval_T *argvars;
13321 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013322 int what;
13323{
Bram Moolenaar33570922005-01-25 22:26:29 +000013324 list_T *l2;
13325 dictitem_T *di;
13326 hashitem_T *hi;
13327 listitem_T *li;
13328 listitem_T *li2;
13329 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013330 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013331
Bram Moolenaar8c711452005-01-14 21:53:12 +000013332 if (argvars[0].v_type != VAR_DICT)
13333 {
13334 EMSG(_(e_dictreq));
13335 return;
13336 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013337 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013338 return;
13339
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013340 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013341 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013342
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013343 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013344 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013345 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013346 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013347 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013348 --todo;
13349 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013351 li = listitem_alloc();
13352 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013353 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013354 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013355
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013356 if (what == 0)
13357 {
13358 /* keys() */
13359 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013360 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013361 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13362 }
13363 else if (what == 1)
13364 {
13365 /* values() */
13366 copy_tv(&di->di_tv, &li->li_tv);
13367 }
13368 else
13369 {
13370 /* items() */
13371 l2 = list_alloc();
13372 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013373 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013374 li->li_tv.vval.v_list = l2;
13375 if (l2 == NULL)
13376 break;
13377 ++l2->lv_refcount;
13378
13379 li2 = listitem_alloc();
13380 if (li2 == NULL)
13381 break;
13382 list_append(l2, li2);
13383 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013384 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013385 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13386
13387 li2 = listitem_alloc();
13388 if (li2 == NULL)
13389 break;
13390 list_append(l2, li2);
13391 copy_tv(&di->di_tv, &li2->li_tv);
13392 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013393 }
13394 }
13395}
13396
13397/*
13398 * "items(dict)" function
13399 */
13400 static void
13401f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013402 typval_T *argvars;
13403 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013404{
13405 dict_list(argvars, rettv, 2);
13406}
13407
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013409 * "join()" function
13410 */
13411 static void
13412f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 typval_T *argvars;
13414 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013415{
13416 garray_T ga;
13417 char_u *sep;
13418
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013419 if (argvars[0].v_type != VAR_LIST)
13420 {
13421 EMSG(_(e_listreq));
13422 return;
13423 }
13424 if (argvars[0].vval.v_list == NULL)
13425 return;
13426 if (argvars[1].v_type == VAR_UNKNOWN)
13427 sep = (char_u *)" ";
13428 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013429 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013430
13431 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013432
13433 if (sep != NULL)
13434 {
13435 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013436 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013437 ga_append(&ga, NUL);
13438 rettv->vval.v_string = (char_u *)ga.ga_data;
13439 }
13440 else
13441 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013442}
13443
13444/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013445 * "keys()" function
13446 */
13447 static void
13448f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013449 typval_T *argvars;
13450 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013451{
13452 dict_list(argvars, rettv, 0);
13453}
13454
13455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 * "last_buffer_nr()" function.
13457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013460 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462{
13463 int n = 0;
13464 buf_T *buf;
13465
13466 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13467 if (n < buf->b_fnum)
13468 n = buf->b_fnum;
13469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013470 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013471}
13472
13473/*
13474 * "len()" function
13475 */
13476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 typval_T *argvars;
13479 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013480{
13481 switch (argvars[0].v_type)
13482 {
13483 case VAR_STRING:
13484 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485 rettv->vval.v_number = (varnumber_T)STRLEN(
13486 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013487 break;
13488 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013490 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013491 case VAR_DICT:
13492 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13493 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013494 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013495 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013496 break;
13497 }
13498}
13499
Bram Moolenaar33570922005-01-25 22:26:29 +000013500static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013501
13502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013503libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 typval_T *argvars;
13505 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013506 int type;
13507{
13508#ifdef FEAT_LIBCALL
13509 char_u *string_in;
13510 char_u **string_result;
13511 int nr_result;
13512#endif
13513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013515 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013516 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013517
13518 if (check_restricted() || check_secure())
13519 return;
13520
13521#ifdef FEAT_LIBCALL
13522 /* The first two args must be strings, otherwise its meaningless */
13523 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13524 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013525 string_in = NULL;
13526 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013527 string_in = argvars[2].vval.v_string;
13528 if (type == VAR_NUMBER)
13529 string_result = NULL;
13530 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013532 if (mch_libcall(argvars[0].vval.v_string,
13533 argvars[1].vval.v_string,
13534 string_in,
13535 argvars[2].vval.v_number,
13536 string_result,
13537 &nr_result) == OK
13538 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013540 }
13541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542}
13543
13544/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545 * "libcall()" function
13546 */
13547 static void
13548f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013549 typval_T *argvars;
13550 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013551{
13552 libcall_common(argvars, rettv, VAR_STRING);
13553}
13554
13555/*
13556 * "libcallnr()" function
13557 */
13558 static void
13559f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013560 typval_T *argvars;
13561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013562{
13563 libcall_common(argvars, rettv, VAR_NUMBER);
13564}
13565
13566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 * "line(string)" function
13568 */
13569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013571 typval_T *argvars;
13572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573{
13574 linenr_T lnum = 0;
13575 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013576 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013578 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 if (fp != NULL)
13580 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582}
13583
13584/*
13585 * "line2byte(lnum)" function
13586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013589 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591{
13592#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594#else
13595 linenr_T lnum;
13596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13602 if (rettv->vval.v_number >= 0)
13603 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604#endif
13605}
13606
13607/*
13608 * "lispindent(lnum)" function
13609 */
13610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013611f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013612 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614{
13615#ifdef FEAT_LISP
13616 pos_T pos;
13617 linenr_T lnum;
13618
13619 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13622 {
13623 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 curwin->w_cursor = pos;
13626 }
13627 else
13628#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630}
13631
13632/*
13633 * "localtime()" function
13634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013637 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641}
13642
Bram Moolenaar33570922005-01-25 22:26:29 +000013643static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644
13645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013647 typval_T *argvars;
13648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 int exact;
13650{
13651 char_u *keys;
13652 char_u *which;
13653 char_u buf[NUMBUFLEN];
13654 char_u *keys_buf = NULL;
13655 char_u *rhs;
13656 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013657 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013658 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013659 mapblock_T *mp;
13660 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
13662 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663 rettv->v_type = VAR_STRING;
13664 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 if (*keys == NUL)
13668 return;
13669
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013670 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013673 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013674 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013675 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013676 if (argvars[3].v_type != VAR_UNKNOWN)
13677 get_dict = get_tv_number(&argvars[3]);
13678 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 else
13681 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013682 if (which == NULL)
13683 return;
13684
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 mode = get_map_mode(&which, 0);
13686
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013687 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013688 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013690
13691 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013693 /* Return a string. */
13694 if (rhs != NULL)
13695 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696
Bram Moolenaarbd743252010-10-20 21:23:33 +020013697 }
13698 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13699 {
13700 /* Return a dictionary. */
13701 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13702 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13703 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704
Bram Moolenaarbd743252010-10-20 21:23:33 +020013705 dict_add_nr_str(dict, "lhs", 0L, lhs);
13706 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13707 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13708 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13709 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13710 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13711 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13712 dict_add_nr_str(dict, "mode", 0L, mapmode);
13713
13714 vim_free(lhs);
13715 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 }
13717}
13718
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013719#ifdef FEAT_FLOAT
13720/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013721 * "log()" function
13722 */
13723 static void
13724f_log(argvars, rettv)
13725 typval_T *argvars;
13726 typval_T *rettv;
13727{
13728 float_T f;
13729
13730 rettv->v_type = VAR_FLOAT;
13731 if (get_float_arg(argvars, &f) == OK)
13732 rettv->vval.v_float = log(f);
13733 else
13734 rettv->vval.v_float = 0.0;
13735}
13736
13737/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013738 * "log10()" function
13739 */
13740 static void
13741f_log10(argvars, rettv)
13742 typval_T *argvars;
13743 typval_T *rettv;
13744{
13745 float_T f;
13746
13747 rettv->v_type = VAR_FLOAT;
13748 if (get_float_arg(argvars, &f) == OK)
13749 rettv->vval.v_float = log10(f);
13750 else
13751 rettv->vval.v_float = 0.0;
13752}
13753#endif
13754
Bram Moolenaar1dced572012-04-05 16:54:08 +020013755#ifdef FEAT_LUA
13756/*
13757 * "luaeval()" function
13758 */
13759 static void
13760f_luaeval(argvars, rettv)
13761 typval_T *argvars;
13762 typval_T *rettv;
13763{
13764 char_u *str;
13765 char_u buf[NUMBUFLEN];
13766
13767 str = get_tv_string_buf(&argvars[0], buf);
13768 do_luaeval(str, argvars + 1, rettv);
13769}
13770#endif
13771
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013773 * "map()" function
13774 */
13775 static void
13776f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013777 typval_T *argvars;
13778 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013779{
13780 filter_map(argvars, rettv, TRUE);
13781}
13782
13783/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013784 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 */
13786 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013788 typval_T *argvars;
13789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013791 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792}
13793
13794/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013795 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 */
13797 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013798f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 typval_T *argvars;
13800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013802 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803}
13804
Bram Moolenaar33570922005-01-25 22:26:29 +000013805static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806
13807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013808find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013809 typval_T *argvars;
13810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 int type;
13812{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013813 char_u *str = NULL;
13814 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 char_u *pat;
13816 regmatch_T regmatch;
13817 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013818 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 char_u *save_cpo;
13820 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013821 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013822 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013823 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013824 list_T *l = NULL;
13825 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013826 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013827 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828
13829 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13830 save_cpo = p_cpo;
13831 p_cpo = (char_u *)"";
13832
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013833 rettv->vval.v_number = -1;
13834 if (type == 3)
13835 {
13836 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013837 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013838 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013839 }
13840 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013842 rettv->v_type = VAR_STRING;
13843 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013846 if (argvars[0].v_type == VAR_LIST)
13847 {
13848 if ((l = argvars[0].vval.v_list) == NULL)
13849 goto theend;
13850 li = l->lv_first;
13851 }
13852 else
13853 expr = str = get_tv_string(&argvars[0]);
13854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013855 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13856 if (pat == NULL)
13857 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013858
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013859 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 int error = FALSE;
13862
13863 start = get_tv_number_chk(&argvars[2], &error);
13864 if (error)
13865 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013866 if (l != NULL)
13867 {
13868 li = list_find(l, start);
13869 if (li == NULL)
13870 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013871 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013872 }
13873 else
13874 {
13875 if (start < 0)
13876 start = 0;
13877 if (start > (long)STRLEN(str))
13878 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013879 /* When "count" argument is there ignore matches before "start",
13880 * otherwise skip part of the string. Differs when pattern is "^"
13881 * or "\<". */
13882 if (argvars[3].v_type != VAR_UNKNOWN)
13883 startcol = start;
13884 else
13885 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013886 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013887
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013888 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 nth = get_tv_number_chk(&argvars[3], &error);
13890 if (error)
13891 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 }
13893
13894 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13895 if (regmatch.regprog != NULL)
13896 {
13897 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013898
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013899 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013900 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901 if (l != NULL)
13902 {
13903 if (li == NULL)
13904 {
13905 match = FALSE;
13906 break;
13907 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013908 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013909 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013910 if (str == NULL)
13911 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013912 }
13913
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013914 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013915
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013916 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013917 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013918 if (l == NULL && !match)
13919 break;
13920
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013921 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013922 if (l != NULL)
13923 {
13924 li = li->li_next;
13925 ++idx;
13926 }
13927 else
13928 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013929#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013930 startcol = (colnr_T)(regmatch.startp[0]
13931 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013932#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013933 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013934#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013935 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013936 }
13937
13938 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013940 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013942 int i;
13943
13944 /* return list with matched string and submatches */
13945 for (i = 0; i < NSUBEXP; ++i)
13946 {
13947 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013948 {
13949 if (list_append_string(rettv->vval.v_list,
13950 (char_u *)"", 0) == FAIL)
13951 break;
13952 }
13953 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013954 regmatch.startp[i],
13955 (int)(regmatch.endp[i] - regmatch.startp[i]))
13956 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013957 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013958 }
13959 }
13960 else if (type == 2)
13961 {
13962 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013963 if (l != NULL)
13964 copy_tv(&li->li_tv, rettv);
13965 else
13966 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968 }
13969 else if (l != NULL)
13970 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 else
13972 {
13973 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 (varnumber_T)(regmatch.startp[0] - str);
13976 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013977 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013979 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 }
13981 }
13982 vim_free(regmatch.regprog);
13983 }
13984
13985theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013986 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 p_cpo = save_cpo;
13988}
13989
13990/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991 * "match()" function
13992 */
13993 static void
13994f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013995 typval_T *argvars;
13996 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013997{
13998 find_some_match(argvars, rettv, 1);
13999}
14000
14001/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014002 * "matchadd()" function
14003 */
14004 static void
14005f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014006 typval_T *argvars UNUSED;
14007 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014008{
14009#ifdef FEAT_SEARCH_EXTRA
14010 char_u buf[NUMBUFLEN];
14011 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14012 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14013 int prio = 10; /* default priority */
14014 int id = -1;
14015 int error = FALSE;
14016
14017 rettv->vval.v_number = -1;
14018
14019 if (grp == NULL || pat == NULL)
14020 return;
14021 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014022 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014023 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014024 if (argvars[3].v_type != VAR_UNKNOWN)
14025 id = get_tv_number_chk(&argvars[3], &error);
14026 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014027 if (error == TRUE)
14028 return;
14029 if (id >= 1 && id <= 3)
14030 {
14031 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14032 return;
14033 }
14034
14035 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14036#endif
14037}
14038
14039/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014040 * "matcharg()" function
14041 */
14042 static void
14043f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014044 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014045 typval_T *rettv;
14046{
14047 if (rettv_list_alloc(rettv) == OK)
14048 {
14049#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014050 int id = get_tv_number(&argvars[0]);
14051 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014052
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014053 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014054 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014055 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14056 {
14057 list_append_string(rettv->vval.v_list,
14058 syn_id2name(m->hlg_id), -1);
14059 list_append_string(rettv->vval.v_list, m->pattern, -1);
14060 }
14061 else
14062 {
14063 list_append_string(rettv->vval.v_list, NUL, -1);
14064 list_append_string(rettv->vval.v_list, NUL, -1);
14065 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014066 }
14067#endif
14068 }
14069}
14070
14071/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014072 * "matchdelete()" function
14073 */
14074 static void
14075f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014076 typval_T *argvars UNUSED;
14077 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014078{
14079#ifdef FEAT_SEARCH_EXTRA
14080 rettv->vval.v_number = match_delete(curwin,
14081 (int)get_tv_number(&argvars[0]), TRUE);
14082#endif
14083}
14084
14085/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014086 * "matchend()" function
14087 */
14088 static void
14089f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014090 typval_T *argvars;
14091 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014092{
14093 find_some_match(argvars, rettv, 0);
14094}
14095
14096/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014097 * "matchlist()" function
14098 */
14099 static void
14100f_matchlist(argvars, rettv)
14101 typval_T *argvars;
14102 typval_T *rettv;
14103{
14104 find_some_match(argvars, rettv, 3);
14105}
14106
14107/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014108 * "matchstr()" function
14109 */
14110 static void
14111f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 typval_T *argvars;
14113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014114{
14115 find_some_match(argvars, rettv, 2);
14116}
14117
Bram Moolenaar33570922005-01-25 22:26:29 +000014118static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014119
14120 static void
14121max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 typval_T *argvars;
14123 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014124 int domax;
14125{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014126 long n = 0;
14127 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014129
14130 if (argvars[0].v_type == VAR_LIST)
14131 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014132 list_T *l;
14133 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014134
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014135 l = argvars[0].vval.v_list;
14136 if (l != NULL)
14137 {
14138 li = l->lv_first;
14139 if (li != NULL)
14140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014142 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014143 {
14144 li = li->li_next;
14145 if (li == NULL)
14146 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014148 if (domax ? i > n : i < n)
14149 n = i;
14150 }
14151 }
14152 }
14153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014154 else if (argvars[0].v_type == VAR_DICT)
14155 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014157 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014159 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014160
14161 d = argvars[0].vval.v_dict;
14162 if (d != NULL)
14163 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014164 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014166 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014167 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014168 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014169 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014171 if (first)
14172 {
14173 n = i;
14174 first = FALSE;
14175 }
14176 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014177 n = i;
14178 }
14179 }
14180 }
14181 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014182 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014183 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014185}
14186
14187/*
14188 * "max()" function
14189 */
14190 static void
14191f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 typval_T *argvars;
14193 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014194{
14195 max_min(argvars, rettv, TRUE);
14196}
14197
14198/*
14199 * "min()" function
14200 */
14201 static void
14202f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014203 typval_T *argvars;
14204 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014205{
14206 max_min(argvars, rettv, FALSE);
14207}
14208
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014209static int mkdir_recurse __ARGS((char_u *dir, int prot));
14210
14211/*
14212 * Create the directory in which "dir" is located, and higher levels when
14213 * needed.
14214 */
14215 static int
14216mkdir_recurse(dir, prot)
14217 char_u *dir;
14218 int prot;
14219{
14220 char_u *p;
14221 char_u *updir;
14222 int r = FAIL;
14223
14224 /* Get end of directory name in "dir".
14225 * We're done when it's "/" or "c:/". */
14226 p = gettail_sep(dir);
14227 if (p <= get_past_head(dir))
14228 return OK;
14229
14230 /* If the directory exists we're done. Otherwise: create it.*/
14231 updir = vim_strnsave(dir, (int)(p - dir));
14232 if (updir == NULL)
14233 return FAIL;
14234 if (mch_isdir(updir))
14235 r = OK;
14236 else if (mkdir_recurse(updir, prot) == OK)
14237 r = vim_mkdir_emsg(updir, prot);
14238 vim_free(updir);
14239 return r;
14240}
14241
14242#ifdef vim_mkdir
14243/*
14244 * "mkdir()" function
14245 */
14246 static void
14247f_mkdir(argvars, rettv)
14248 typval_T *argvars;
14249 typval_T *rettv;
14250{
14251 char_u *dir;
14252 char_u buf[NUMBUFLEN];
14253 int prot = 0755;
14254
14255 rettv->vval.v_number = FAIL;
14256 if (check_restricted() || check_secure())
14257 return;
14258
14259 dir = get_tv_string_buf(&argvars[0], buf);
14260 if (argvars[1].v_type != VAR_UNKNOWN)
14261 {
14262 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 prot = get_tv_number_chk(&argvars[2], NULL);
14264 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014265 mkdir_recurse(dir, prot);
14266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014268}
14269#endif
14270
Bram Moolenaar0d660222005-01-07 21:51:51 +000014271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 * "mode()" function
14273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 typval_T *argvars;
14277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014279 char_u buf[3];
14280
14281 buf[1] = NUL;
14282 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283
14284#ifdef FEAT_VISUAL
14285 if (VIsual_active)
14286 {
14287 if (VIsual_select)
14288 buf[0] = VIsual_mode + 's' - 'v';
14289 else
14290 buf[0] = VIsual_mode;
14291 }
14292 else
14293#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014294 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14295 || State == CONFIRM)
14296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014298 if (State == ASKMORE)
14299 buf[1] = 'm';
14300 else if (State == CONFIRM)
14301 buf[1] = '?';
14302 }
14303 else if (State == EXTERNCMD)
14304 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 else if (State & INSERT)
14306 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014307#ifdef FEAT_VREPLACE
14308 if (State & VREPLACE_FLAG)
14309 {
14310 buf[0] = 'R';
14311 buf[1] = 'v';
14312 }
14313 else
14314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 if (State & REPLACE_FLAG)
14316 buf[0] = 'R';
14317 else
14318 buf[0] = 'i';
14319 }
14320 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014323 if (exmode_active)
14324 buf[1] = 'v';
14325 }
14326 else if (exmode_active)
14327 {
14328 buf[0] = 'c';
14329 buf[1] = 'e';
14330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014334 if (finish_op)
14335 buf[1] = 'o';
14336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014338 /* Clear out the minor mode when the argument is not a non-zero number or
14339 * non-empty string. */
14340 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014341 buf[1] = NUL;
14342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343 rettv->vval.v_string = vim_strsave(buf);
14344 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345}
14346
Bram Moolenaar429fa852013-04-15 12:27:36 +020014347#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014348/*
14349 * "mzeval()" function
14350 */
14351 static void
14352f_mzeval(argvars, rettv)
14353 typval_T *argvars;
14354 typval_T *rettv;
14355{
14356 char_u *str;
14357 char_u buf[NUMBUFLEN];
14358
14359 str = get_tv_string_buf(&argvars[0], buf);
14360 do_mzeval(str, rettv);
14361}
Bram Moolenaar75676462013-01-30 14:55:42 +010014362
14363 void
14364mzscheme_call_vim(name, args, rettv)
14365 char_u *name;
14366 typval_T *args;
14367 typval_T *rettv;
14368{
14369 typval_T argvars[3];
14370
14371 argvars[0].v_type = VAR_STRING;
14372 argvars[0].vval.v_string = name;
14373 copy_tv(args, &argvars[1]);
14374 argvars[2].v_type = VAR_UNKNOWN;
14375 f_call(argvars, rettv);
14376 clear_tv(&argvars[1]);
14377}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014378#endif
14379
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014381 * "nextnonblank()" function
14382 */
14383 static void
14384f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014385 typval_T *argvars;
14386 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014387{
14388 linenr_T lnum;
14389
14390 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014392 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014393 {
14394 lnum = 0;
14395 break;
14396 }
14397 if (*skipwhite(ml_get(lnum)) != NUL)
14398 break;
14399 }
14400 rettv->vval.v_number = lnum;
14401}
14402
14403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 * "nr2char()" function
14405 */
14406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014408 typval_T *argvars;
14409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
14411 char_u buf[NUMBUFLEN];
14412
14413#ifdef FEAT_MBYTE
14414 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014415 {
14416 int utf8 = 0;
14417
14418 if (argvars[1].v_type != VAR_UNKNOWN)
14419 utf8 = get_tv_number_chk(&argvars[1], NULL);
14420 if (utf8)
14421 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14422 else
14423 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 else
14426#endif
14427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 buf[1] = NUL;
14430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 rettv->v_type = VAR_STRING;
14432 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014433}
14434
14435/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014436 * "or(expr, expr)" function
14437 */
14438 static void
14439f_or(argvars, rettv)
14440 typval_T *argvars;
14441 typval_T *rettv;
14442{
14443 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14444 | get_tv_number_chk(&argvars[1], NULL);
14445}
14446
14447/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014448 * "pathshorten()" function
14449 */
14450 static void
14451f_pathshorten(argvars, rettv)
14452 typval_T *argvars;
14453 typval_T *rettv;
14454{
14455 char_u *p;
14456
14457 rettv->v_type = VAR_STRING;
14458 p = get_tv_string_chk(&argvars[0]);
14459 if (p == NULL)
14460 rettv->vval.v_string = NULL;
14461 else
14462 {
14463 p = vim_strsave(p);
14464 rettv->vval.v_string = p;
14465 if (p != NULL)
14466 shorten_dir(p);
14467 }
14468}
14469
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014470#ifdef FEAT_FLOAT
14471/*
14472 * "pow()" function
14473 */
14474 static void
14475f_pow(argvars, rettv)
14476 typval_T *argvars;
14477 typval_T *rettv;
14478{
14479 float_T fx, fy;
14480
14481 rettv->v_type = VAR_FLOAT;
14482 if (get_float_arg(argvars, &fx) == OK
14483 && get_float_arg(&argvars[1], &fy) == OK)
14484 rettv->vval.v_float = pow(fx, fy);
14485 else
14486 rettv->vval.v_float = 0.0;
14487}
14488#endif
14489
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014490/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491 * "prevnonblank()" function
14492 */
14493 static void
14494f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014495 typval_T *argvars;
14496 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014497{
14498 linenr_T lnum;
14499
14500 lnum = get_tv_lnum(argvars);
14501 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14502 lnum = 0;
14503 else
14504 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14505 --lnum;
14506 rettv->vval.v_number = lnum;
14507}
14508
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014509#ifdef HAVE_STDARG_H
14510/* This dummy va_list is here because:
14511 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14512 * - locally in the function results in a "used before set" warning
14513 * - using va_start() to initialize it gives "function with fixed args" error */
14514static va_list ap;
14515#endif
14516
Bram Moolenaar8c711452005-01-14 21:53:12 +000014517/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014518 * "printf()" function
14519 */
14520 static void
14521f_printf(argvars, rettv)
14522 typval_T *argvars;
14523 typval_T *rettv;
14524{
14525 rettv->v_type = VAR_STRING;
14526 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014527#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014528 {
14529 char_u buf[NUMBUFLEN];
14530 int len;
14531 char_u *s;
14532 int saved_did_emsg = did_emsg;
14533 char *fmt;
14534
14535 /* Get the required length, allocate the buffer and do it for real. */
14536 did_emsg = FALSE;
14537 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014538 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014539 if (!did_emsg)
14540 {
14541 s = alloc(len + 1);
14542 if (s != NULL)
14543 {
14544 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014545 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014546 }
14547 }
14548 did_emsg |= saved_did_emsg;
14549 }
14550#endif
14551}
14552
14553/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014554 * "pumvisible()" function
14555 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014556 static void
14557f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014558 typval_T *argvars UNUSED;
14559 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014560{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014561#ifdef FEAT_INS_EXPAND
14562 if (pum_visible())
14563 rettv->vval.v_number = 1;
14564#endif
14565}
14566
Bram Moolenaardb913952012-06-29 12:54:53 +020014567#ifdef FEAT_PYTHON3
14568/*
14569 * "py3eval()" function
14570 */
14571 static void
14572f_py3eval(argvars, rettv)
14573 typval_T *argvars;
14574 typval_T *rettv;
14575{
14576 char_u *str;
14577 char_u buf[NUMBUFLEN];
14578
14579 str = get_tv_string_buf(&argvars[0], buf);
14580 do_py3eval(str, rettv);
14581}
14582#endif
14583
14584#ifdef FEAT_PYTHON
14585/*
14586 * "pyeval()" function
14587 */
14588 static void
14589f_pyeval(argvars, rettv)
14590 typval_T *argvars;
14591 typval_T *rettv;
14592{
14593 char_u *str;
14594 char_u buf[NUMBUFLEN];
14595
14596 str = get_tv_string_buf(&argvars[0], buf);
14597 do_pyeval(str, rettv);
14598}
14599#endif
14600
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014601/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014602 * "range()" function
14603 */
14604 static void
14605f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014606 typval_T *argvars;
14607 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014608{
14609 long start;
14610 long end;
14611 long stride = 1;
14612 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014615 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014616 if (argvars[1].v_type == VAR_UNKNOWN)
14617 {
14618 end = start - 1;
14619 start = 0;
14620 }
14621 else
14622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014624 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014626 }
14627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014628 if (error)
14629 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014630 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014631 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014632 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014633 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014634 else
14635 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014636 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014637 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014638 if (list_append_number(rettv->vval.v_list,
14639 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014640 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014641 }
14642}
14643
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014644/*
14645 * "readfile()" function
14646 */
14647 static void
14648f_readfile(argvars, rettv)
14649 typval_T *argvars;
14650 typval_T *rettv;
14651{
14652 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014653 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014654 char_u *fname;
14655 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014656 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14657 int io_size = sizeof(buf);
14658 int readlen; /* size of last fread() */
14659 char_u *prev = NULL; /* previously read bytes, if any */
14660 long prevlen = 0; /* length of data in prev */
14661 long prevsize = 0; /* size of prev buffer */
14662 long maxline = MAXLNUM;
14663 long cnt = 0;
14664 char_u *p; /* position in buf */
14665 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014666
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014667 if (argvars[1].v_type != VAR_UNKNOWN)
14668 {
14669 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14670 binary = TRUE;
14671 if (argvars[2].v_type != VAR_UNKNOWN)
14672 maxline = get_tv_number(&argvars[2]);
14673 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014674
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014675 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014676 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014677
14678 /* Always open the file in binary mode, library functions have a mind of
14679 * their own about CR-LF conversion. */
14680 fname = get_tv_string(&argvars[0]);
14681 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14682 {
14683 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14684 return;
14685 }
14686
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014687 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014688 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014689 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014690
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014691 /* This for loop processes what was read, but is also entered at end
14692 * of file so that either:
14693 * - an incomplete line gets written
14694 * - a "binary" file gets an empty line at the end if it ends in a
14695 * newline. */
14696 for (p = buf, start = buf;
14697 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14698 ++p)
14699 {
14700 if (*p == '\n' || readlen <= 0)
14701 {
14702 listitem_T *li;
14703 char_u *s = NULL;
14704 long_u len = p - start;
14705
14706 /* Finished a line. Remove CRs before NL. */
14707 if (readlen > 0 && !binary)
14708 {
14709 while (len > 0 && start[len - 1] == '\r')
14710 --len;
14711 /* removal may cross back to the "prev" string */
14712 if (len == 0)
14713 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14714 --prevlen;
14715 }
14716 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014717 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014718 else
14719 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014720 /* Change "prev" buffer to be the right size. This way
14721 * the bytes are only copied once, and very long lines are
14722 * allocated only once. */
14723 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014724 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014725 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014726 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014727 prev = NULL; /* the list will own the string */
14728 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014729 }
14730 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014731 if (s == NULL)
14732 {
14733 do_outofmem_msg((long_u) prevlen + len + 1);
14734 failed = TRUE;
14735 break;
14736 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014737
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014738 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014739 {
14740 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014741 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014742 break;
14743 }
14744 li->li_tv.v_type = VAR_STRING;
14745 li->li_tv.v_lock = 0;
14746 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014747 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014748
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014749 start = p + 1; /* step over newline */
14750 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014751 break;
14752 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 else if (*p == NUL)
14754 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014755#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014756 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14757 * when finding the BF and check the previous two bytes. */
14758 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014759 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014760 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14761 * + 1, these may be in the "prev" string. */
14762 char_u back1 = p >= buf + 1 ? p[-1]
14763 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14764 char_u back2 = p >= buf + 2 ? p[-2]
14765 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14766 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014767
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014768 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014769 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014770 char_u *dest = p - 2;
14771
14772 /* Usually a BOM is at the beginning of a file, and so at
14773 * the beginning of a line; then we can just step over it.
14774 */
14775 if (start == dest)
14776 start = p + 1;
14777 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014778 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014779 /* have to shuffle buf to close gap */
14780 int adjust_prevlen = 0;
14781
14782 if (dest < buf)
14783 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014784 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014785 dest = buf;
14786 }
14787 if (readlen > p - buf + 1)
14788 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14789 readlen -= 3 - adjust_prevlen;
14790 prevlen -= adjust_prevlen;
14791 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014792 }
14793 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014794 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014795#endif
14796 } /* for */
14797
14798 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14799 break;
14800 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014801 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014802 /* There's part of a line in buf, store it in "prev". */
14803 if (p - start + prevlen >= prevsize)
14804 {
14805 /* need bigger "prev" buffer */
14806 char_u *newprev;
14807
14808 /* A common use case is ordinary text files and "prev" gets a
14809 * fragment of a line, so the first allocation is made
14810 * small, to avoid repeatedly 'allocing' large and
14811 * 'reallocing' small. */
14812 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014813 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014814 else
14815 {
14816 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014817 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014818 prevsize = grow50pc > growmin ? grow50pc : growmin;
14819 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014820 newprev = prev == NULL ? alloc(prevsize)
14821 : vim_realloc(prev, prevsize);
14822 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014823 {
14824 do_outofmem_msg((long_u)prevsize);
14825 failed = TRUE;
14826 break;
14827 }
14828 prev = newprev;
14829 }
14830 /* Add the line part to end of "prev". */
14831 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014832 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014833 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014834 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014835
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014836 /*
14837 * For a negative line count use only the lines at the end of the file,
14838 * free the rest.
14839 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014840 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014841 while (cnt > -maxline)
14842 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014843 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014844 --cnt;
14845 }
14846
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014847 if (failed)
14848 {
14849 list_free(rettv->vval.v_list, TRUE);
14850 /* readfile doc says an empty list is returned on error */
14851 rettv->vval.v_list = list_alloc();
14852 }
14853
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014854 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014855 fclose(fd);
14856}
14857
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014858#if defined(FEAT_RELTIME)
14859static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14860
14861/*
14862 * Convert a List to proftime_T.
14863 * Return FAIL when there is something wrong.
14864 */
14865 static int
14866list2proftime(arg, tm)
14867 typval_T *arg;
14868 proftime_T *tm;
14869{
14870 long n1, n2;
14871 int error = FALSE;
14872
14873 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14874 || arg->vval.v_list->lv_len != 2)
14875 return FAIL;
14876 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14877 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14878# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014879 tm->HighPart = n1;
14880 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014881# else
14882 tm->tv_sec = n1;
14883 tm->tv_usec = n2;
14884# endif
14885 return error ? FAIL : OK;
14886}
14887#endif /* FEAT_RELTIME */
14888
14889/*
14890 * "reltime()" function
14891 */
14892 static void
14893f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014894 typval_T *argvars UNUSED;
14895 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014896{
14897#ifdef FEAT_RELTIME
14898 proftime_T res;
14899 proftime_T start;
14900
14901 if (argvars[0].v_type == VAR_UNKNOWN)
14902 {
14903 /* No arguments: get current time. */
14904 profile_start(&res);
14905 }
14906 else if (argvars[1].v_type == VAR_UNKNOWN)
14907 {
14908 if (list2proftime(&argvars[0], &res) == FAIL)
14909 return;
14910 profile_end(&res);
14911 }
14912 else
14913 {
14914 /* Two arguments: compute the difference. */
14915 if (list2proftime(&argvars[0], &start) == FAIL
14916 || list2proftime(&argvars[1], &res) == FAIL)
14917 return;
14918 profile_sub(&res, &start);
14919 }
14920
14921 if (rettv_list_alloc(rettv) == OK)
14922 {
14923 long n1, n2;
14924
14925# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014926 n1 = res.HighPart;
14927 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014928# else
14929 n1 = res.tv_sec;
14930 n2 = res.tv_usec;
14931# endif
14932 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14933 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14934 }
14935#endif
14936}
14937
14938/*
14939 * "reltimestr()" function
14940 */
14941 static void
14942f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014943 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014944 typval_T *rettv;
14945{
14946#ifdef FEAT_RELTIME
14947 proftime_T tm;
14948#endif
14949
14950 rettv->v_type = VAR_STRING;
14951 rettv->vval.v_string = NULL;
14952#ifdef FEAT_RELTIME
14953 if (list2proftime(&argvars[0], &tm) == OK)
14954 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14955#endif
14956}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014957
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14959static void make_connection __ARGS((void));
14960static int check_connection __ARGS((void));
14961
14962 static void
14963make_connection()
14964{
14965 if (X_DISPLAY == NULL
14966# ifdef FEAT_GUI
14967 && !gui.in_use
14968# endif
14969 )
14970 {
14971 x_force_connect = TRUE;
14972 setup_term_clip();
14973 x_force_connect = FALSE;
14974 }
14975}
14976
14977 static int
14978check_connection()
14979{
14980 make_connection();
14981 if (X_DISPLAY == NULL)
14982 {
14983 EMSG(_("E240: No connection to Vim server"));
14984 return FAIL;
14985 }
14986 return OK;
14987}
14988#endif
14989
14990#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014991static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992
14993 static void
14994remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014995 typval_T *argvars;
14996 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997 int expr;
14998{
14999 char_u *server_name;
15000 char_u *keys;
15001 char_u *r = NULL;
15002 char_u buf[NUMBUFLEN];
15003# ifdef WIN32
15004 HWND w;
15005# else
15006 Window w;
15007# endif
15008
15009 if (check_restricted() || check_secure())
15010 return;
15011
15012# ifdef FEAT_X11
15013 if (check_connection() == FAIL)
15014 return;
15015# endif
15016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015017 server_name = get_tv_string_chk(&argvars[0]);
15018 if (server_name == NULL)
15019 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015020 keys = get_tv_string_buf(&argvars[1], buf);
15021# ifdef WIN32
15022 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15023# else
15024 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15025 < 0)
15026# endif
15027 {
15028 if (r != NULL)
15029 EMSG(r); /* sending worked but evaluation failed */
15030 else
15031 EMSG2(_("E241: Unable to send to %s"), server_name);
15032 return;
15033 }
15034
15035 rettv->vval.v_string = r;
15036
15037 if (argvars[2].v_type != VAR_UNKNOWN)
15038 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015039 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015040 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015041 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015042
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015043 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015044 v.di_tv.v_type = VAR_STRING;
15045 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015046 idvar = get_tv_string_chk(&argvars[2]);
15047 if (idvar != NULL)
15048 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015049 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050 }
15051}
15052#endif
15053
15054/*
15055 * "remote_expr()" function
15056 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057 static void
15058f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015059 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015060 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061{
15062 rettv->v_type = VAR_STRING;
15063 rettv->vval.v_string = NULL;
15064#ifdef FEAT_CLIENTSERVER
15065 remote_common(argvars, rettv, TRUE);
15066#endif
15067}
15068
15069/*
15070 * "remote_foreground()" function
15071 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072 static void
15073f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015074 typval_T *argvars UNUSED;
15075 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077#ifdef FEAT_CLIENTSERVER
15078# ifdef WIN32
15079 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015080 {
15081 char_u *server_name = get_tv_string_chk(&argvars[0]);
15082
15083 if (server_name != NULL)
15084 serverForeground(server_name);
15085 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015086# else
15087 /* Send a foreground() expression to the server. */
15088 argvars[1].v_type = VAR_STRING;
15089 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15090 argvars[2].v_type = VAR_UNKNOWN;
15091 remote_common(argvars, rettv, TRUE);
15092 vim_free(argvars[1].vval.v_string);
15093# endif
15094#endif
15095}
15096
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097 static void
15098f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015099 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015101{
15102#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015103 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 char_u *s = NULL;
15105# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015106 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015108 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109
15110 if (check_restricted() || check_secure())
15111 {
15112 rettv->vval.v_number = -1;
15113 return;
15114 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015115 serverid = get_tv_string_chk(&argvars[0]);
15116 if (serverid == NULL)
15117 {
15118 rettv->vval.v_number = -1;
15119 return; /* type error; errmsg already given */
15120 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015122 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123 if (n == 0)
15124 rettv->vval.v_number = -1;
15125 else
15126 {
15127 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15128 rettv->vval.v_number = (s != NULL);
15129 }
15130# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131 if (check_connection() == FAIL)
15132 return;
15133
15134 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015135 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136# endif
15137
15138 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015140 char_u *retvar;
15141
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 v.di_tv.v_type = VAR_STRING;
15143 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015144 retvar = get_tv_string_chk(&argvars[1]);
15145 if (retvar != NULL)
15146 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015148 }
15149#else
15150 rettv->vval.v_number = -1;
15151#endif
15152}
15153
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154 static void
15155f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015156 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158{
15159 char_u *r = NULL;
15160
15161#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015162 char_u *serverid = get_tv_string_chk(&argvars[0]);
15163
15164 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165 {
15166# ifdef WIN32
15167 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015168 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015170 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 if (n != 0)
15172 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15173 if (r == NULL)
15174# else
15175 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015176 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015177# endif
15178 EMSG(_("E277: Unable to read a server reply"));
15179 }
15180#endif
15181 rettv->v_type = VAR_STRING;
15182 rettv->vval.v_string = r;
15183}
15184
15185/*
15186 * "remote_send()" function
15187 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015188 static void
15189f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015190 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015191 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192{
15193 rettv->v_type = VAR_STRING;
15194 rettv->vval.v_string = NULL;
15195#ifdef FEAT_CLIENTSERVER
15196 remote_common(argvars, rettv, FALSE);
15197#endif
15198}
15199
15200/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015201 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015202 */
15203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 typval_T *argvars;
15206 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015207{
Bram Moolenaar33570922005-01-25 22:26:29 +000015208 list_T *l;
15209 listitem_T *item, *item2;
15210 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015211 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015212 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015213 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015214 dict_T *d;
15215 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015216 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015217
Bram Moolenaar8c711452005-01-14 21:53:12 +000015218 if (argvars[0].v_type == VAR_DICT)
15219 {
15220 if (argvars[2].v_type != VAR_UNKNOWN)
15221 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015222 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015223 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225 key = get_tv_string_chk(&argvars[1]);
15226 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015228 di = dict_find(d, key, -1);
15229 if (di == NULL)
15230 EMSG2(_(e_dictkey), key);
15231 else
15232 {
15233 *rettv = di->di_tv;
15234 init_tv(&di->di_tv);
15235 dictitem_remove(d, di);
15236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015237 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015238 }
15239 }
15240 else if (argvars[0].v_type != VAR_LIST)
15241 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015242 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015243 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015245 int error = FALSE;
15246
15247 idx = get_tv_number_chk(&argvars[1], &error);
15248 if (error)
15249 ; /* type error: do nothing, errmsg already given */
15250 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015251 EMSGN(_(e_listidx), idx);
15252 else
15253 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015254 if (argvars[2].v_type == VAR_UNKNOWN)
15255 {
15256 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015257 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015258 *rettv = item->li_tv;
15259 vim_free(item);
15260 }
15261 else
15262 {
15263 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015264 end = get_tv_number_chk(&argvars[2], &error);
15265 if (error)
15266 ; /* type error: do nothing */
15267 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015268 EMSGN(_(e_listidx), end);
15269 else
15270 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015271 int cnt = 0;
15272
15273 for (li = item; li != NULL; li = li->li_next)
15274 {
15275 ++cnt;
15276 if (li == item2)
15277 break;
15278 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015279 if (li == NULL) /* didn't find "item2" after "item" */
15280 EMSG(_(e_invrange));
15281 else
15282 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015283 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015284 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015285 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015286 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015287 l->lv_first = item;
15288 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015289 item->li_prev = NULL;
15290 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015291 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015292 }
15293 }
15294 }
15295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015296 }
15297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298}
15299
15300/*
15301 * "rename({from}, {to})" function
15302 */
15303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015305 typval_T *argvars;
15306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307{
15308 char_u buf[NUMBUFLEN];
15309
15310 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015313 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15314 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315}
15316
15317/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015318 * "repeat()" function
15319 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015320 static void
15321f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015322 typval_T *argvars;
15323 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015324{
15325 char_u *p;
15326 int n;
15327 int slen;
15328 int len;
15329 char_u *r;
15330 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015331
15332 n = get_tv_number(&argvars[1]);
15333 if (argvars[0].v_type == VAR_LIST)
15334 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015335 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015336 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015337 if (list_extend(rettv->vval.v_list,
15338 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015339 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015340 }
15341 else
15342 {
15343 p = get_tv_string(&argvars[0]);
15344 rettv->v_type = VAR_STRING;
15345 rettv->vval.v_string = NULL;
15346
15347 slen = (int)STRLEN(p);
15348 len = slen * n;
15349 if (len <= 0)
15350 return;
15351
15352 r = alloc(len + 1);
15353 if (r != NULL)
15354 {
15355 for (i = 0; i < n; i++)
15356 mch_memmove(r + i * slen, p, (size_t)slen);
15357 r[len] = NUL;
15358 }
15359
15360 rettv->vval.v_string = r;
15361 }
15362}
15363
15364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 * "resolve()" function
15366 */
15367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015368f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015369 typval_T *argvars;
15370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371{
15372 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015373#ifdef HAVE_READLINK
15374 char_u *buf = NULL;
15375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015377 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378#ifdef FEAT_SHORTCUT
15379 {
15380 char_u *v = NULL;
15381
15382 v = mch_resolve_shortcut(p);
15383 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 }
15388#else
15389# ifdef HAVE_READLINK
15390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 char_u *cpy;
15392 int len;
15393 char_u *remain = NULL;
15394 char_u *q;
15395 int is_relative_to_current = FALSE;
15396 int has_trailing_pathsep = FALSE;
15397 int limit = 100;
15398
15399 p = vim_strsave(p);
15400
15401 if (p[0] == '.' && (vim_ispathsep(p[1])
15402 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15403 is_relative_to_current = TRUE;
15404
15405 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015406 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015409 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411
15412 q = getnextcomp(p);
15413 if (*q != NUL)
15414 {
15415 /* Separate the first path component in "p", and keep the
15416 * remainder (beginning with the path separator). */
15417 remain = vim_strsave(q - 1);
15418 q[-1] = NUL;
15419 }
15420
Bram Moolenaard9462e32011-04-11 21:35:11 +020015421 buf = alloc(MAXPATHL + 1);
15422 if (buf == NULL)
15423 goto fail;
15424
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425 for (;;)
15426 {
15427 for (;;)
15428 {
15429 len = readlink((char *)p, (char *)buf, MAXPATHL);
15430 if (len <= 0)
15431 break;
15432 buf[len] = NUL;
15433
15434 if (limit-- == 0)
15435 {
15436 vim_free(p);
15437 vim_free(remain);
15438 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 goto fail;
15441 }
15442
15443 /* Ensure that the result will have a trailing path separator
15444 * if the argument has one. */
15445 if (remain == NULL && has_trailing_pathsep)
15446 add_pathsep(buf);
15447
15448 /* Separate the first path component in the link value and
15449 * concatenate the remainders. */
15450 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15451 if (*q != NUL)
15452 {
15453 if (remain == NULL)
15454 remain = vim_strsave(q - 1);
15455 else
15456 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015457 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 if (cpy != NULL)
15459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 vim_free(remain);
15461 remain = cpy;
15462 }
15463 }
15464 q[-1] = NUL;
15465 }
15466
15467 q = gettail(p);
15468 if (q > p && *q == NUL)
15469 {
15470 /* Ignore trailing path separator. */
15471 q[-1] = NUL;
15472 q = gettail(p);
15473 }
15474 if (q > p && !mch_isFullName(buf))
15475 {
15476 /* symlink is relative to directory of argument */
15477 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15478 if (cpy != NULL)
15479 {
15480 STRCPY(cpy, p);
15481 STRCPY(gettail(cpy), buf);
15482 vim_free(p);
15483 p = cpy;
15484 }
15485 }
15486 else
15487 {
15488 vim_free(p);
15489 p = vim_strsave(buf);
15490 }
15491 }
15492
15493 if (remain == NULL)
15494 break;
15495
15496 /* Append the first path component of "remain" to "p". */
15497 q = getnextcomp(remain + 1);
15498 len = q - remain - (*q != NUL);
15499 cpy = vim_strnsave(p, STRLEN(p) + len);
15500 if (cpy != NULL)
15501 {
15502 STRNCAT(cpy, remain, len);
15503 vim_free(p);
15504 p = cpy;
15505 }
15506 /* Shorten "remain". */
15507 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015508 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 else
15510 {
15511 vim_free(remain);
15512 remain = NULL;
15513 }
15514 }
15515
15516 /* If the result is a relative path name, make it explicitly relative to
15517 * the current directory if and only if the argument had this form. */
15518 if (!vim_ispathsep(*p))
15519 {
15520 if (is_relative_to_current
15521 && *p != NUL
15522 && !(p[0] == '.'
15523 && (p[1] == NUL
15524 || vim_ispathsep(p[1])
15525 || (p[1] == '.'
15526 && (p[2] == NUL
15527 || vim_ispathsep(p[2]))))))
15528 {
15529 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015530 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 if (cpy != NULL)
15532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 vim_free(p);
15534 p = cpy;
15535 }
15536 }
15537 else if (!is_relative_to_current)
15538 {
15539 /* Strip leading "./". */
15540 q = p;
15541 while (q[0] == '.' && vim_ispathsep(q[1]))
15542 q += 2;
15543 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015544 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 }
15546 }
15547
15548 /* Ensure that the result will have no trailing path separator
15549 * if the argument had none. But keep "/" or "//". */
15550 if (!has_trailing_pathsep)
15551 {
15552 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015553 if (after_pathsep(p, q))
15554 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 }
15556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 }
15559# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561# endif
15562#endif
15563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565
15566#ifdef HAVE_READLINK
15567fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015568 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571}
15572
15573/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 */
15576 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015577f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015578 typval_T *argvars;
15579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580{
Bram Moolenaar33570922005-01-25 22:26:29 +000015581 list_T *l;
15582 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583
Bram Moolenaar0d660222005-01-07 21:51:51 +000015584 if (argvars[0].v_type != VAR_LIST)
15585 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015586 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015587 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588 {
15589 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015590 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015591 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 while (li != NULL)
15593 {
15594 ni = li->li_prev;
15595 list_append(l, li);
15596 li = ni;
15597 }
15598 rettv->vval.v_list = l;
15599 rettv->v_type = VAR_LIST;
15600 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015601 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603}
15604
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015605#define SP_NOMOVE 0x01 /* don't move cursor */
15606#define SP_REPEAT 0x02 /* repeat to find outer pair */
15607#define SP_RETCOUNT 0x04 /* return matchcount */
15608#define SP_SETPCMARK 0x08 /* set previous context mark */
15609#define SP_START 0x10 /* accept match at start position */
15610#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15611#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015612
Bram Moolenaar33570922005-01-25 22:26:29 +000015613static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015614
15615/*
15616 * Get flags for a search function.
15617 * Possibly sets "p_ws".
15618 * Returns BACKWARD, FORWARD or zero (for an error).
15619 */
15620 static int
15621get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015622 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015623 int *flagsp;
15624{
15625 int dir = FORWARD;
15626 char_u *flags;
15627 char_u nbuf[NUMBUFLEN];
15628 int mask;
15629
15630 if (varp->v_type != VAR_UNKNOWN)
15631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015632 flags = get_tv_string_buf_chk(varp, nbuf);
15633 if (flags == NULL)
15634 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015635 while (*flags != NUL)
15636 {
15637 switch (*flags)
15638 {
15639 case 'b': dir = BACKWARD; break;
15640 case 'w': p_ws = TRUE; break;
15641 case 'W': p_ws = FALSE; break;
15642 default: mask = 0;
15643 if (flagsp != NULL)
15644 switch (*flags)
15645 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015646 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015647 case 'e': mask = SP_END; break;
15648 case 'm': mask = SP_RETCOUNT; break;
15649 case 'n': mask = SP_NOMOVE; break;
15650 case 'p': mask = SP_SUBPAT; break;
15651 case 'r': mask = SP_REPEAT; break;
15652 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015653 }
15654 if (mask == 0)
15655 {
15656 EMSG2(_(e_invarg2), flags);
15657 dir = 0;
15658 }
15659 else
15660 *flagsp |= mask;
15661 }
15662 if (dir == 0)
15663 break;
15664 ++flags;
15665 }
15666 }
15667 return dir;
15668}
15669
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015671 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015673 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015674search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015675 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015676 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015677 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015679 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 char_u *pat;
15681 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015682 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 int save_p_ws = p_ws;
15684 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015685 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015686 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015687 proftime_T tm;
15688#ifdef FEAT_RELTIME
15689 long time_limit = 0;
15690#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015691 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015692 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015694 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015695 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015696 if (dir == 0)
15697 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015698 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015699 if (flags & SP_START)
15700 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015701 if (flags & SP_END)
15702 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015703
Bram Moolenaar76929292008-01-06 19:07:36 +000015704 /* Optional arguments: line number to stop searching and timeout. */
15705 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015706 {
15707 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15708 if (lnum_stop < 0)
15709 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015710#ifdef FEAT_RELTIME
15711 if (argvars[3].v_type != VAR_UNKNOWN)
15712 {
15713 time_limit = get_tv_number_chk(&argvars[3], NULL);
15714 if (time_limit < 0)
15715 goto theend;
15716 }
15717#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015718 }
15719
Bram Moolenaar76929292008-01-06 19:07:36 +000015720#ifdef FEAT_RELTIME
15721 /* Set the time limit, if there is one. */
15722 profile_setlimit(time_limit, &tm);
15723#endif
15724
Bram Moolenaar231334e2005-07-25 20:46:57 +000015725 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015726 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015727 * Check to make sure only those flags are set.
15728 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15729 * flags cannot be set. Check for that condition also.
15730 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015731 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015732 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015734 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015735 goto theend;
15736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015738 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015739 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015740 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015741 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015743 if (flags & SP_SUBPAT)
15744 retval = subpatnum;
15745 else
15746 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015747 if (flags & SP_SETPCMARK)
15748 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015750 if (match_pos != NULL)
15751 {
15752 /* Store the match cursor position */
15753 match_pos->lnum = pos.lnum;
15754 match_pos->col = pos.col + 1;
15755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 /* "/$" will put the cursor after the end of the line, may need to
15757 * correct that here */
15758 check_cursor();
15759 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015760
15761 /* If 'n' flag is used: restore cursor position. */
15762 if (flags & SP_NOMOVE)
15763 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015764 else
15765 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015766theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015768
15769 return retval;
15770}
15771
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015772#ifdef FEAT_FLOAT
15773/*
15774 * "round({float})" function
15775 */
15776 static void
15777f_round(argvars, rettv)
15778 typval_T *argvars;
15779 typval_T *rettv;
15780{
15781 float_T f;
15782
15783 rettv->v_type = VAR_FLOAT;
15784 if (get_float_arg(argvars, &f) == OK)
15785 /* round() is not in C90, use ceil() or floor() instead. */
15786 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15787 else
15788 rettv->vval.v_float = 0.0;
15789}
15790#endif
15791
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015792/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015793 * "screencol()" function
15794 *
15795 * First column is 1 to be consistent with virtcol().
15796 */
15797 static void
15798f_screencol(argvars, rettv)
15799 typval_T *argvars UNUSED;
15800 typval_T *rettv;
15801{
15802 rettv->vval.v_number = screen_screencol() + 1;
15803}
15804
15805/*
15806 * "screenrow()" function
15807 */
15808 static void
15809f_screenrow(argvars, rettv)
15810 typval_T *argvars UNUSED;
15811 typval_T *rettv;
15812{
15813 rettv->vval.v_number = screen_screenrow() + 1;
15814}
15815
15816/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015817 * "search()" function
15818 */
15819 static void
15820f_search(argvars, rettv)
15821 typval_T *argvars;
15822 typval_T *rettv;
15823{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015824 int flags = 0;
15825
15826 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827}
15828
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015830 * "searchdecl()" function
15831 */
15832 static void
15833f_searchdecl(argvars, rettv)
15834 typval_T *argvars;
15835 typval_T *rettv;
15836{
15837 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015838 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015839 int error = FALSE;
15840 char_u *name;
15841
15842 rettv->vval.v_number = 1; /* default: FAIL */
15843
15844 name = get_tv_string_chk(&argvars[0]);
15845 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015846 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015847 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015848 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15849 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15850 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015851 if (!error && name != NULL)
15852 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015853 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015854}
15855
15856/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015857 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015859 static int
15860searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015861 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015862 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863{
15864 char_u *spat, *mpat, *epat;
15865 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 int dir;
15868 int flags = 0;
15869 char_u nbuf1[NUMBUFLEN];
15870 char_u nbuf2[NUMBUFLEN];
15871 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015872 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015873 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015874 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015877 spat = get_tv_string_chk(&argvars[0]);
15878 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15879 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15880 if (spat == NULL || mpat == NULL || epat == NULL)
15881 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883 /* Handle the optional fourth argument: flags */
15884 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015885 if (dir == 0)
15886 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015887
15888 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015889 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15890 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015891 if ((flags & (SP_END | SP_SUBPAT)) != 0
15892 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015893 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015894 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015895 goto theend;
15896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015898 /* Using 'r' implies 'W', otherwise it doesn't work. */
15899 if (flags & SP_REPEAT)
15900 p_ws = FALSE;
15901
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015902 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015903 if (argvars[3].v_type == VAR_UNKNOWN
15904 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 skip = (char_u *)"";
15906 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015908 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015909 if (argvars[5].v_type != VAR_UNKNOWN)
15910 {
15911 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15912 if (lnum_stop < 0)
15913 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015914#ifdef FEAT_RELTIME
15915 if (argvars[6].v_type != VAR_UNKNOWN)
15916 {
15917 time_limit = get_tv_number_chk(&argvars[6], NULL);
15918 if (time_limit < 0)
15919 goto theend;
15920 }
15921#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015922 }
15923 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 if (skip == NULL)
15925 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015927 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015928 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015929
15930theend:
15931 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015932
15933 return retval;
15934}
15935
15936/*
15937 * "searchpair()" function
15938 */
15939 static void
15940f_searchpair(argvars, rettv)
15941 typval_T *argvars;
15942 typval_T *rettv;
15943{
15944 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15945}
15946
15947/*
15948 * "searchpairpos()" function
15949 */
15950 static void
15951f_searchpairpos(argvars, rettv)
15952 typval_T *argvars;
15953 typval_T *rettv;
15954{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015955 pos_T match_pos;
15956 int lnum = 0;
15957 int col = 0;
15958
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015959 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015960 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015961
15962 if (searchpair_cmn(argvars, &match_pos) > 0)
15963 {
15964 lnum = match_pos.lnum;
15965 col = match_pos.col;
15966 }
15967
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015968 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15969 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015970}
15971
15972/*
15973 * Search for a start/middle/end thing.
15974 * Used by searchpair(), see its documentation for the details.
15975 * Returns 0 or -1 for no match,
15976 */
15977 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015978do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15979 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015980 char_u *spat; /* start pattern */
15981 char_u *mpat; /* middle pattern */
15982 char_u *epat; /* end pattern */
15983 int dir; /* BACKWARD or FORWARD */
15984 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015985 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015986 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015987 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015988 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015989{
15990 char_u *save_cpo;
15991 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15992 long retval = 0;
15993 pos_T pos;
15994 pos_T firstpos;
15995 pos_T foundpos;
15996 pos_T save_cursor;
15997 pos_T save_pos;
15998 int n;
15999 int r;
16000 int nest = 1;
16001 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016002 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016003 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016004
16005 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16006 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016007 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016008
Bram Moolenaar76929292008-01-06 19:07:36 +000016009#ifdef FEAT_RELTIME
16010 /* Set the time limit, if there is one. */
16011 profile_setlimit(time_limit, &tm);
16012#endif
16013
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016014 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16015 * start/middle/end (pat3, for the top pair). */
16016 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16017 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16018 if (pat2 == NULL || pat3 == NULL)
16019 goto theend;
16020 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16021 if (*mpat == NUL)
16022 STRCPY(pat3, pat2);
16023 else
16024 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16025 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016026 if (flags & SP_START)
16027 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016028
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 save_cursor = curwin->w_cursor;
16030 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016031 clearpos(&firstpos);
16032 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 pat = pat3;
16034 for (;;)
16035 {
16036 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016037 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16039 /* didn't find it or found the first match again: FAIL */
16040 break;
16041
16042 if (firstpos.lnum == 0)
16043 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016044 if (equalpos(pos, foundpos))
16045 {
16046 /* Found the same position again. Can happen with a pattern that
16047 * has "\zs" at the end and searching backwards. Advance one
16048 * character and try again. */
16049 if (dir == BACKWARD)
16050 decl(&pos);
16051 else
16052 incl(&pos);
16053 }
16054 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016056 /* clear the start flag to avoid getting stuck here */
16057 options &= ~SEARCH_START;
16058
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 /* If the skip pattern matches, ignore this match. */
16060 if (*skip != NUL)
16061 {
16062 save_pos = curwin->w_cursor;
16063 curwin->w_cursor = pos;
16064 r = eval_to_bool(skip, &err, NULL, FALSE);
16065 curwin->w_cursor = save_pos;
16066 if (err)
16067 {
16068 /* Evaluating {skip} caused an error, break here. */
16069 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016070 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 break;
16072 }
16073 if (r)
16074 continue;
16075 }
16076
16077 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16078 {
16079 /* Found end when searching backwards or start when searching
16080 * forward: nested pair. */
16081 ++nest;
16082 pat = pat2; /* nested, don't search for middle */
16083 }
16084 else
16085 {
16086 /* Found end when searching forward or start when searching
16087 * backward: end of (nested) pair; or found middle in outer pair. */
16088 if (--nest == 1)
16089 pat = pat3; /* outer level, search for middle */
16090 }
16091
16092 if (nest == 0)
16093 {
16094 /* Found the match: return matchcount or line number. */
16095 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016096 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016098 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016099 if (flags & SP_SETPCMARK)
16100 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 curwin->w_cursor = pos;
16102 if (!(flags & SP_REPEAT))
16103 break;
16104 nest = 1; /* search for next unmatched */
16105 }
16106 }
16107
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016108 if (match_pos != NULL)
16109 {
16110 /* Store the match cursor position */
16111 match_pos->lnum = curwin->w_cursor.lnum;
16112 match_pos->col = curwin->w_cursor.col + 1;
16113 }
16114
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016116 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117 curwin->w_cursor = save_cursor;
16118
16119theend:
16120 vim_free(pat2);
16121 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016122 if (p_cpo == empty_option)
16123 p_cpo = save_cpo;
16124 else
16125 /* Darn, evaluating the {skip} expression changed the value. */
16126 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016127
16128 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129}
16130
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016131/*
16132 * "searchpos()" function
16133 */
16134 static void
16135f_searchpos(argvars, rettv)
16136 typval_T *argvars;
16137 typval_T *rettv;
16138{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016139 pos_T match_pos;
16140 int lnum = 0;
16141 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016142 int n;
16143 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016144
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016145 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016146 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016147
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016148 n = search_cmn(argvars, &match_pos, &flags);
16149 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016150 {
16151 lnum = match_pos.lnum;
16152 col = match_pos.col;
16153 }
16154
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016155 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16156 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016157 if (flags & SP_SUBPAT)
16158 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016159}
16160
16161
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 static void
16163f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167#ifdef FEAT_CLIENTSERVER
16168 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016169 char_u *server = get_tv_string_chk(&argvars[0]);
16170 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016173 if (server == NULL || reply == NULL)
16174 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175 if (check_restricted() || check_secure())
16176 return;
16177# ifdef FEAT_X11
16178 if (check_connection() == FAIL)
16179 return;
16180# endif
16181
16182 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016184 EMSG(_("E258: Unable to send to client"));
16185 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187 rettv->vval.v_number = 0;
16188#else
16189 rettv->vval.v_number = -1;
16190#endif
16191}
16192
Bram Moolenaar0d660222005-01-07 21:51:51 +000016193 static void
16194f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016195 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016196 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197{
16198 char_u *r = NULL;
16199
16200#ifdef FEAT_CLIENTSERVER
16201# ifdef WIN32
16202 r = serverGetVimNames();
16203# else
16204 make_connection();
16205 if (X_DISPLAY != NULL)
16206 r = serverGetVimNames(X_DISPLAY);
16207# endif
16208#endif
16209 rettv->v_type = VAR_STRING;
16210 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211}
16212
16213/*
16214 * "setbufvar()" function
16215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016217f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016218 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016219 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220{
16221 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016224 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 char_u nbuf[NUMBUFLEN];
16226
16227 if (check_restricted() || check_secure())
16228 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016229 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16230 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016231 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 varp = &argvars[2];
16233
16234 if (buf != NULL && varname != NULL && varp != NULL)
16235 {
16236 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238
16239 if (*varname == '&')
16240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 long numval;
16242 char_u *strval;
16243 int error = FALSE;
16244
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 numval = get_tv_number_chk(varp, &error);
16247 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016248 if (!error && strval != NULL)
16249 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 }
16251 else
16252 {
16253 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16254 if (bufvarname != NULL)
16255 {
16256 STRCPY(bufvarname, "b:");
16257 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016258 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 vim_free(bufvarname);
16260 }
16261 }
16262
16263 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266}
16267
16268/*
16269 * "setcmdpos()" function
16270 */
16271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016272f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016273 typval_T *argvars;
16274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016276 int pos = (int)get_tv_number(&argvars[0]) - 1;
16277
16278 if (pos >= 0)
16279 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280}
16281
16282/*
16283 * "setline()" function
16284 */
16285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016286f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016287 typval_T *argvars;
16288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289{
16290 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016291 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016292 list_T *l = NULL;
16293 listitem_T *li = NULL;
16294 long added = 0;
16295 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016297 lnum = get_tv_lnum(&argvars[0]);
16298 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016300 l = argvars[1].vval.v_list;
16301 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016303 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016304 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016305
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016306 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016307 for (;;)
16308 {
16309 if (l != NULL)
16310 {
16311 /* list argument, get next string */
16312 if (li == NULL)
16313 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016315 li = li->li_next;
16316 }
16317
16318 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016319 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016320 break;
16321 if (lnum <= curbuf->b_ml.ml_line_count)
16322 {
16323 /* existing line, replace it */
16324 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16325 {
16326 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016327 if (lnum == curwin->w_cursor.lnum)
16328 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016329 rettv->vval.v_number = 0; /* OK */
16330 }
16331 }
16332 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16333 {
16334 /* lnum is one past the last line, append the line */
16335 ++added;
16336 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16337 rettv->vval.v_number = 0; /* OK */
16338 }
16339
16340 if (l == NULL) /* only one string argument */
16341 break;
16342 ++lnum;
16343 }
16344
16345 if (added > 0)
16346 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347}
16348
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016349static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16350
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016352 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016353 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016354 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016355set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016356 win_T *wp UNUSED;
16357 typval_T *list_arg UNUSED;
16358 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016359 typval_T *rettv;
16360{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016361#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016362 char_u *act;
16363 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016364#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016365
Bram Moolenaar2641f772005-03-25 21:58:17 +000016366 rettv->vval.v_number = -1;
16367
16368#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016369 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016370 EMSG(_(e_listreq));
16371 else
16372 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016373 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016374
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016375 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016376 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016377 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016378 if (act == NULL)
16379 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016380 if (*act == 'a' || *act == 'r')
16381 action = *act;
16382 }
16383
Bram Moolenaar81484f42012-12-05 15:16:47 +010016384 if (l != NULL && set_errorlist(wp, l, action,
16385 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016386 rettv->vval.v_number = 0;
16387 }
16388#endif
16389}
16390
16391/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016392 * "setloclist()" function
16393 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016394 static void
16395f_setloclist(argvars, rettv)
16396 typval_T *argvars;
16397 typval_T *rettv;
16398{
16399 win_T *win;
16400
16401 rettv->vval.v_number = -1;
16402
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016403 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016404 if (win != NULL)
16405 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16406}
16407
16408/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016409 * "setmatches()" function
16410 */
16411 static void
16412f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016413 typval_T *argvars UNUSED;
16414 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016415{
16416#ifdef FEAT_SEARCH_EXTRA
16417 list_T *l;
16418 listitem_T *li;
16419 dict_T *d;
16420
16421 rettv->vval.v_number = -1;
16422 if (argvars[0].v_type != VAR_LIST)
16423 {
16424 EMSG(_(e_listreq));
16425 return;
16426 }
16427 if ((l = argvars[0].vval.v_list) != NULL)
16428 {
16429
16430 /* To some extent make sure that we are dealing with a list from
16431 * "getmatches()". */
16432 li = l->lv_first;
16433 while (li != NULL)
16434 {
16435 if (li->li_tv.v_type != VAR_DICT
16436 || (d = li->li_tv.vval.v_dict) == NULL)
16437 {
16438 EMSG(_(e_invarg));
16439 return;
16440 }
16441 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16442 && dict_find(d, (char_u *)"pattern", -1) != NULL
16443 && dict_find(d, (char_u *)"priority", -1) != NULL
16444 && dict_find(d, (char_u *)"id", -1) != NULL))
16445 {
16446 EMSG(_(e_invarg));
16447 return;
16448 }
16449 li = li->li_next;
16450 }
16451
16452 clear_matches(curwin);
16453 li = l->lv_first;
16454 while (li != NULL)
16455 {
16456 d = li->li_tv.vval.v_dict;
16457 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16458 get_dict_string(d, (char_u *)"pattern", FALSE),
16459 (int)get_dict_number(d, (char_u *)"priority"),
16460 (int)get_dict_number(d, (char_u *)"id"));
16461 li = li->li_next;
16462 }
16463 rettv->vval.v_number = 0;
16464 }
16465#endif
16466}
16467
16468/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016469 * "setpos()" function
16470 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016471 static void
16472f_setpos(argvars, rettv)
16473 typval_T *argvars;
16474 typval_T *rettv;
16475{
16476 pos_T pos;
16477 int fnum;
16478 char_u *name;
16479
Bram Moolenaar08250432008-02-13 11:42:46 +000016480 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016481 name = get_tv_string_chk(argvars);
16482 if (name != NULL)
16483 {
16484 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16485 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016486 if (--pos.col < 0)
16487 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016488 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016489 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016490 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016491 if (fnum == curbuf->b_fnum)
16492 {
16493 curwin->w_cursor = pos;
16494 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016495 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016496 }
16497 else
16498 EMSG(_(e_invarg));
16499 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016500 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16501 {
16502 /* set mark */
16503 if (setmark_pos(name[1], &pos, fnum) == OK)
16504 rettv->vval.v_number = 0;
16505 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016506 else
16507 EMSG(_(e_invarg));
16508 }
16509 }
16510}
16511
16512/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016513 * "setqflist()" function
16514 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016515 static void
16516f_setqflist(argvars, rettv)
16517 typval_T *argvars;
16518 typval_T *rettv;
16519{
16520 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16521}
16522
16523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 * "setreg()" function
16525 */
16526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016527f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016528 typval_T *argvars;
16529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530{
16531 int regname;
16532 char_u *strregname;
16533 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016534 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 int append;
16536 char_u yank_type;
16537 long block_len;
16538
16539 block_len = -1;
16540 yank_type = MAUTO;
16541 append = FALSE;
16542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016544 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016546 if (strregname == NULL)
16547 return; /* type error; errmsg already given */
16548 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 if (regname == 0 || regname == '@')
16550 regname = '"';
16551 else if (regname == '=')
16552 return;
16553
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016554 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016556 stropt = get_tv_string_chk(&argvars[2]);
16557 if (stropt == NULL)
16558 return; /* type error */
16559 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 switch (*stropt)
16561 {
16562 case 'a': case 'A': /* append */
16563 append = TRUE;
16564 break;
16565 case 'v': case 'c': /* character-wise selection */
16566 yank_type = MCHAR;
16567 break;
16568 case 'V': case 'l': /* line-wise selection */
16569 yank_type = MLINE;
16570 break;
16571#ifdef FEAT_VISUAL
16572 case 'b': case Ctrl_V: /* block-wise selection */
16573 yank_type = MBLOCK;
16574 if (VIM_ISDIGIT(stropt[1]))
16575 {
16576 ++stropt;
16577 block_len = getdigits(&stropt) - 1;
16578 --stropt;
16579 }
16580 break;
16581#endif
16582 }
16583 }
16584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 strval = get_tv_string_chk(&argvars[1]);
16586 if (strval != NULL)
16587 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016589 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590}
16591
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016592/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016593 * "settabvar()" function
16594 */
16595 static void
16596f_settabvar(argvars, rettv)
16597 typval_T *argvars;
16598 typval_T *rettv;
16599{
16600 tabpage_T *save_curtab;
16601 char_u *varname, *tabvarname;
16602 typval_T *varp;
16603 tabpage_T *tp;
16604
16605 rettv->vval.v_number = 0;
16606
16607 if (check_restricted() || check_secure())
16608 return;
16609
16610 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16611 varname = get_tv_string_chk(&argvars[1]);
16612 varp = &argvars[2];
16613
16614 if (tp != NULL && varname != NULL && varp != NULL)
16615 {
16616 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016617 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016618
16619 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16620 if (tabvarname != NULL)
16621 {
16622 STRCPY(tabvarname, "t:");
16623 STRCPY(tabvarname + 2, varname);
16624 set_var(tabvarname, varp, TRUE);
16625 vim_free(tabvarname);
16626 }
16627
16628 /* Restore current tabpage */
16629 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016630 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016631 }
16632}
16633
16634/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016635 * "settabwinvar()" function
16636 */
16637 static void
16638f_settabwinvar(argvars, rettv)
16639 typval_T *argvars;
16640 typval_T *rettv;
16641{
16642 setwinvar(argvars, rettv, 1);
16643}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644
16645/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016646 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016649f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016650 typval_T *argvars;
16651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016653 setwinvar(argvars, rettv, 0);
16654}
16655
16656/*
16657 * "setwinvar()" and "settabwinvar()" functions
16658 */
16659 static void
16660setwinvar(argvars, rettv, off)
16661 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016662 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016663 int off;
16664{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 win_T *win;
16666#ifdef FEAT_WINDOWS
16667 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016668 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669#endif
16670 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016671 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016673 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674
16675 if (check_restricted() || check_secure())
16676 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016677
16678#ifdef FEAT_WINDOWS
16679 if (off == 1)
16680 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16681 else
16682 tp = curtab;
16683#endif
16684 win = find_win_by_nr(&argvars[off], tp);
16685 varname = get_tv_string_chk(&argvars[off + 1]);
16686 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687
16688 if (win != NULL && varname != NULL && varp != NULL)
16689 {
16690#ifdef FEAT_WINDOWS
16691 /* set curwin to be our win, temporarily */
16692 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016693 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016694 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016695 if (!win_valid(win))
16696 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 curwin = win;
16698 curbuf = curwin->w_buffer;
16699#endif
16700
16701 if (*varname == '&')
16702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 long numval;
16704 char_u *strval;
16705 int error = FALSE;
16706
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708 numval = get_tv_number_chk(varp, &error);
16709 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016710 if (!error && strval != NULL)
16711 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 }
16713 else
16714 {
16715 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16716 if (winvarname != NULL)
16717 {
16718 STRCPY(winvarname, "w:");
16719 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016720 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 vim_free(winvarname);
16722 }
16723 }
16724
16725#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016726 /* Restore current tabpage and window, if still valid (autocomands can
16727 * make them invalid). */
16728 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016729 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 if (win_valid(save_curwin))
16731 {
16732 curwin = save_curwin;
16733 curbuf = curwin->w_buffer;
16734 }
16735#endif
16736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737}
16738
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016739#ifdef FEAT_CRYPT
16740/*
16741 * "sha256({string})" function
16742 */
16743 static void
16744f_sha256(argvars, rettv)
16745 typval_T *argvars;
16746 typval_T *rettv;
16747{
16748 char_u *p;
16749
16750 p = get_tv_string(&argvars[0]);
16751 rettv->vval.v_string = vim_strsave(
16752 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16753 rettv->v_type = VAR_STRING;
16754}
16755#endif /* FEAT_CRYPT */
16756
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016758 * "shellescape({string})" function
16759 */
16760 static void
16761f_shellescape(argvars, rettv)
16762 typval_T *argvars;
16763 typval_T *rettv;
16764{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016765 rettv->vval.v_string = vim_strsave_shellescape(
16766 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016767 rettv->v_type = VAR_STRING;
16768}
16769
16770/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016771 * shiftwidth() function
16772 */
16773 static void
16774f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016775 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016776 typval_T *rettv;
16777{
16778 rettv->vval.v_number = get_sw_value();
16779}
16780
16781/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016782 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 */
16784 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016785f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016786 typval_T *argvars;
16787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016789 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790
Bram Moolenaar0d660222005-01-07 21:51:51 +000016791 p = get_tv_string(&argvars[0]);
16792 rettv->vval.v_string = vim_strsave(p);
16793 simplify_filename(rettv->vval.v_string); /* simplify in place */
16794 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795}
16796
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016797#ifdef FEAT_FLOAT
16798/*
16799 * "sin()" function
16800 */
16801 static void
16802f_sin(argvars, rettv)
16803 typval_T *argvars;
16804 typval_T *rettv;
16805{
16806 float_T f;
16807
16808 rettv->v_type = VAR_FLOAT;
16809 if (get_float_arg(argvars, &f) == OK)
16810 rettv->vval.v_float = sin(f);
16811 else
16812 rettv->vval.v_float = 0.0;
16813}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016814
16815/*
16816 * "sinh()" function
16817 */
16818 static void
16819f_sinh(argvars, rettv)
16820 typval_T *argvars;
16821 typval_T *rettv;
16822{
16823 float_T f;
16824
16825 rettv->v_type = VAR_FLOAT;
16826 if (get_float_arg(argvars, &f) == OK)
16827 rettv->vval.v_float = sinh(f);
16828 else
16829 rettv->vval.v_float = 0.0;
16830}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016831#endif
16832
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833static int
16834#ifdef __BORLANDC__
16835 _RTLENTRYF
16836#endif
16837 item_compare __ARGS((const void *s1, const void *s2));
16838static int
16839#ifdef __BORLANDC__
16840 _RTLENTRYF
16841#endif
16842 item_compare2 __ARGS((const void *s1, const void *s2));
16843
16844static int item_compare_ic;
16845static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016846static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016847static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016848#define ITEM_COMPARE_FAIL 999
16849
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016851 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016853 static int
16854#ifdef __BORLANDC__
16855_RTLENTRYF
16856#endif
16857item_compare(s1, s2)
16858 const void *s1;
16859 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016861 char_u *p1, *p2;
16862 char_u *tofree1, *tofree2;
16863 int res;
16864 char_u numbuf1[NUMBUFLEN];
16865 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016867 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16868 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016869 if (p1 == NULL)
16870 p1 = (char_u *)"";
16871 if (p2 == NULL)
16872 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873 if (item_compare_ic)
16874 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016876 res = STRCMP(p1, p2);
16877 vim_free(tofree1);
16878 vim_free(tofree2);
16879 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880}
16881
16882 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883#ifdef __BORLANDC__
16884_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016886item_compare2(s1, s2)
16887 const void *s1;
16888 const void *s2;
16889{
16890 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016892 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016893 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016895 /* shortcut after failure in previous call; compare all items equal */
16896 if (item_compare_func_err)
16897 return 0;
16898
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016899 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16900 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016901 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16902 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016903
16904 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016905 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016906 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16907 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908 clear_tv(&argv[0]);
16909 clear_tv(&argv[1]);
16910
16911 if (res == FAIL)
16912 res = ITEM_COMPARE_FAIL;
16913 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016914 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16915 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016916 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917 clear_tv(&rettv);
16918 return res;
16919}
16920
16921/*
16922 * "sort({list})" function
16923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016926 typval_T *argvars;
16927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928{
Bram Moolenaar33570922005-01-25 22:26:29 +000016929 list_T *l;
16930 listitem_T *li;
16931 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 long len;
16933 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935 if (argvars[0].v_type != VAR_LIST)
16936 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 else
16938 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016940 if (l == NULL || tv_check_lock(l->lv_lock,
16941 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 return;
16943 rettv->vval.v_list = l;
16944 rettv->v_type = VAR_LIST;
16945 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947 len = list_len(l);
16948 if (len <= 1)
16949 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951 item_compare_ic = FALSE;
16952 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016953 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954 if (argvars[1].v_type != VAR_UNKNOWN)
16955 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016956 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016958 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 else
16960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016961 int error = FALSE;
16962
16963 i = get_tv_number_chk(&argvars[1], &error);
16964 if (error)
16965 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 if (i == 1)
16967 item_compare_ic = TRUE;
16968 else
16969 item_compare_func = get_tv_string(&argvars[1]);
16970 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016971
16972 if (argvars[2].v_type != VAR_UNKNOWN)
16973 {
16974 /* optional third argument: {dict} */
16975 if (argvars[2].v_type != VAR_DICT)
16976 {
16977 EMSG(_(e_dictreq));
16978 return;
16979 }
16980 item_compare_selfdict = argvars[2].vval.v_dict;
16981 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016985 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986 if (ptrs == NULL)
16987 return;
16988 i = 0;
16989 for (li = l->lv_first; li != NULL; li = li->li_next)
16990 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016992 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993 /* test the compare function */
16994 if (item_compare_func != NULL
16995 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16996 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016997 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016999 {
17000 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 item_compare_func == NULL ? item_compare : item_compare2);
17003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017004 if (!item_compare_func_err)
17005 {
17006 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017007 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008 l->lv_len = 0;
17009 for (i = 0; i < len; ++i)
17010 list_append(l, ptrs[i]);
17011 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012 }
17013
17014 vim_free(ptrs);
17015 }
17016}
17017
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017018/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017019 * "soundfold({word})" function
17020 */
17021 static void
17022f_soundfold(argvars, rettv)
17023 typval_T *argvars;
17024 typval_T *rettv;
17025{
17026 char_u *s;
17027
17028 rettv->v_type = VAR_STRING;
17029 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017030#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017031 rettv->vval.v_string = eval_soundfold(s);
17032#else
17033 rettv->vval.v_string = vim_strsave(s);
17034#endif
17035}
17036
17037/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017038 * "spellbadword()" function
17039 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017040 static void
17041f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017042 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017043 typval_T *rettv;
17044{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017045 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017046 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017047 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017048
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017049 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017050 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017051
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017052#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017053 if (argvars[0].v_type == VAR_UNKNOWN)
17054 {
17055 /* Find the start and length of the badly spelled word. */
17056 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17057 if (len != 0)
17058 word = ml_get_cursor();
17059 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017060 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017061 {
17062 char_u *str = get_tv_string_chk(&argvars[0]);
17063 int capcol = -1;
17064
17065 if (str != NULL)
17066 {
17067 /* Check the argument for spelling. */
17068 while (*str != NUL)
17069 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017070 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017071 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017072 {
17073 word = str;
17074 break;
17075 }
17076 str += len;
17077 }
17078 }
17079 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017080#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017081
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017082 list_append_string(rettv->vval.v_list, word, len);
17083 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017084 attr == HLF_SPB ? "bad" :
17085 attr == HLF_SPR ? "rare" :
17086 attr == HLF_SPL ? "local" :
17087 attr == HLF_SPC ? "caps" :
17088 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017089}
17090
17091/*
17092 * "spellsuggest()" function
17093 */
17094 static void
17095f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017096 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017097 typval_T *rettv;
17098{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017099#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017100 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017101 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017102 int maxcount;
17103 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017104 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017105 listitem_T *li;
17106 int need_capital = FALSE;
17107#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017108
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017109 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017110 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017111
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017112#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017113 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017114 {
17115 str = get_tv_string(&argvars[0]);
17116 if (argvars[1].v_type != VAR_UNKNOWN)
17117 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017118 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017119 if (maxcount <= 0)
17120 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017121 if (argvars[2].v_type != VAR_UNKNOWN)
17122 {
17123 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17124 if (typeerr)
17125 return;
17126 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017127 }
17128 else
17129 maxcount = 25;
17130
Bram Moolenaar4770d092006-01-12 23:22:24 +000017131 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017132
17133 for (i = 0; i < ga.ga_len; ++i)
17134 {
17135 str = ((char_u **)ga.ga_data)[i];
17136
17137 li = listitem_alloc();
17138 if (li == NULL)
17139 vim_free(str);
17140 else
17141 {
17142 li->li_tv.v_type = VAR_STRING;
17143 li->li_tv.v_lock = 0;
17144 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017145 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017146 }
17147 }
17148 ga_clear(&ga);
17149 }
17150#endif
17151}
17152
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017154f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017155 typval_T *argvars;
17156 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157{
17158 char_u *str;
17159 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017160 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 regmatch_T regmatch;
17162 char_u patbuf[NUMBUFLEN];
17163 char_u *save_cpo;
17164 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017166 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017167 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017168
17169 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17170 save_cpo = p_cpo;
17171 p_cpo = (char_u *)"";
17172
17173 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017174 if (argvars[1].v_type != VAR_UNKNOWN)
17175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017176 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17177 if (pat == NULL)
17178 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017179 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017180 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017181 }
17182 if (pat == NULL || *pat == NUL)
17183 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017185 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017187 if (typeerr)
17188 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189
Bram Moolenaar0d660222005-01-07 21:51:51 +000017190 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17191 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017194 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017196 if (*str == NUL)
17197 match = FALSE; /* empty item at the end */
17198 else
17199 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017200 if (match)
17201 end = regmatch.startp[0];
17202 else
17203 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017204 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17205 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017207 if (list_append_string(rettv->vval.v_list, str,
17208 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017209 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017210 }
17211 if (!match)
17212 break;
17213 /* Advance to just after the match. */
17214 if (regmatch.endp[0] > str)
17215 col = 0;
17216 else
17217 {
17218 /* Don't get stuck at the same match. */
17219#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017220 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221#else
17222 col = 1;
17223#endif
17224 }
17225 str = regmatch.endp[0];
17226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227
Bram Moolenaar0d660222005-01-07 21:51:51 +000017228 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230
Bram Moolenaar0d660222005-01-07 21:51:51 +000017231 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232}
17233
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017234#ifdef FEAT_FLOAT
17235/*
17236 * "sqrt()" function
17237 */
17238 static void
17239f_sqrt(argvars, rettv)
17240 typval_T *argvars;
17241 typval_T *rettv;
17242{
17243 float_T f;
17244
17245 rettv->v_type = VAR_FLOAT;
17246 if (get_float_arg(argvars, &f) == OK)
17247 rettv->vval.v_float = sqrt(f);
17248 else
17249 rettv->vval.v_float = 0.0;
17250}
17251
17252/*
17253 * "str2float()" function
17254 */
17255 static void
17256f_str2float(argvars, rettv)
17257 typval_T *argvars;
17258 typval_T *rettv;
17259{
17260 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17261
17262 if (*p == '+')
17263 p = skipwhite(p + 1);
17264 (void)string2float(p, &rettv->vval.v_float);
17265 rettv->v_type = VAR_FLOAT;
17266}
17267#endif
17268
Bram Moolenaar2c932302006-03-18 21:42:09 +000017269/*
17270 * "str2nr()" function
17271 */
17272 static void
17273f_str2nr(argvars, rettv)
17274 typval_T *argvars;
17275 typval_T *rettv;
17276{
17277 int base = 10;
17278 char_u *p;
17279 long n;
17280
17281 if (argvars[1].v_type != VAR_UNKNOWN)
17282 {
17283 base = get_tv_number(&argvars[1]);
17284 if (base != 8 && base != 10 && base != 16)
17285 {
17286 EMSG(_(e_invarg));
17287 return;
17288 }
17289 }
17290
17291 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017292 if (*p == '+')
17293 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017294 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17295 rettv->vval.v_number = n;
17296}
17297
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298#ifdef HAVE_STRFTIME
17299/*
17300 * "strftime({format}[, {time}])" function
17301 */
17302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017303f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017304 typval_T *argvars;
17305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306{
17307 char_u result_buf[256];
17308 struct tm *curtime;
17309 time_t seconds;
17310 char_u *p;
17311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017312 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017314 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017315 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 seconds = time(NULL);
17317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017318 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 curtime = localtime(&seconds);
17320 /* MSVC returns NULL for an invalid value of seconds. */
17321 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017322 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 else
17324 {
17325# ifdef FEAT_MBYTE
17326 vimconv_T conv;
17327 char_u *enc;
17328
17329 conv.vc_type = CONV_NONE;
17330 enc = enc_locale();
17331 convert_setup(&conv, p_enc, enc);
17332 if (conv.vc_type != CONV_NONE)
17333 p = string_convert(&conv, p, NULL);
17334# endif
17335 if (p != NULL)
17336 (void)strftime((char *)result_buf, sizeof(result_buf),
17337 (char *)p, curtime);
17338 else
17339 result_buf[0] = NUL;
17340
17341# ifdef FEAT_MBYTE
17342 if (conv.vc_type != CONV_NONE)
17343 vim_free(p);
17344 convert_setup(&conv, enc, p_enc);
17345 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017346 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 else
17348# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017349 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350
17351# ifdef FEAT_MBYTE
17352 /* Release conversion descriptors */
17353 convert_setup(&conv, NULL, NULL);
17354 vim_free(enc);
17355# endif
17356 }
17357}
17358#endif
17359
17360/*
17361 * "stridx()" function
17362 */
17363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 typval_T *argvars;
17366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367{
17368 char_u buf[NUMBUFLEN];
17369 char_u *needle;
17370 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017371 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017375 needle = get_tv_string_chk(&argvars[1]);
17376 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017377 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017378 if (needle == NULL || haystack == NULL)
17379 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380
Bram Moolenaar33570922005-01-25 22:26:29 +000017381 if (argvars[2].v_type != VAR_UNKNOWN)
17382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017383 int error = FALSE;
17384
17385 start_idx = get_tv_number_chk(&argvars[2], &error);
17386 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017387 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017388 if (start_idx >= 0)
17389 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 }
17391
17392 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17393 if (pos != NULL)
17394 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395}
17396
17397/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017398 * "string()" function
17399 */
17400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017401f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 typval_T *argvars;
17403 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017404{
17405 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017406 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017409 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017410 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017411 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017412 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413}
17414
17415/*
17416 * "strlen()" function
17417 */
17418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017419f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017420 typval_T *argvars;
17421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017423 rettv->vval.v_number = (varnumber_T)(STRLEN(
17424 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425}
17426
17427/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017428 * "strchars()" function
17429 */
17430 static void
17431f_strchars(argvars, rettv)
17432 typval_T *argvars;
17433 typval_T *rettv;
17434{
17435 char_u *s = get_tv_string(&argvars[0]);
17436#ifdef FEAT_MBYTE
17437 varnumber_T len = 0;
17438
17439 while (*s != NUL)
17440 {
17441 mb_cptr2char_adv(&s);
17442 ++len;
17443 }
17444 rettv->vval.v_number = len;
17445#else
17446 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17447#endif
17448}
17449
17450/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017451 * "strdisplaywidth()" function
17452 */
17453 static void
17454f_strdisplaywidth(argvars, rettv)
17455 typval_T *argvars;
17456 typval_T *rettv;
17457{
17458 char_u *s = get_tv_string(&argvars[0]);
17459 int col = 0;
17460
17461 if (argvars[1].v_type != VAR_UNKNOWN)
17462 col = get_tv_number(&argvars[1]);
17463
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017464 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017465}
17466
17467/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017468 * "strwidth()" function
17469 */
17470 static void
17471f_strwidth(argvars, rettv)
17472 typval_T *argvars;
17473 typval_T *rettv;
17474{
17475 char_u *s = get_tv_string(&argvars[0]);
17476
17477 rettv->vval.v_number = (varnumber_T)(
17478#ifdef FEAT_MBYTE
17479 mb_string2cells(s, -1)
17480#else
17481 STRLEN(s)
17482#endif
17483 );
17484}
17485
17486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 * "strpart()" function
17488 */
17489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 typval_T *argvars;
17492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493{
17494 char_u *p;
17495 int n;
17496 int len;
17497 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017498 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017500 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 slen = (int)STRLEN(p);
17502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017503 n = get_tv_number_chk(&argvars[1], &error);
17504 if (error)
17505 len = 0;
17506 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 else
17509 len = slen - n; /* default len: all bytes that are available. */
17510
17511 /*
17512 * Only return the overlap between the specified part and the actual
17513 * string.
17514 */
17515 if (n < 0)
17516 {
17517 len += n;
17518 n = 0;
17519 }
17520 else if (n > slen)
17521 n = slen;
17522 if (len < 0)
17523 len = 0;
17524 else if (n + len > slen)
17525 len = slen - n;
17526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527 rettv->v_type = VAR_STRING;
17528 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529}
17530
17531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017532 * "strridx()" function
17533 */
17534 static void
17535f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017536 typval_T *argvars;
17537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017538{
17539 char_u buf[NUMBUFLEN];
17540 char_u *needle;
17541 char_u *haystack;
17542 char_u *rest;
17543 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017544 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017546 needle = get_tv_string_chk(&argvars[1]);
17547 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017548
17549 rettv->vval.v_number = -1;
17550 if (needle == NULL || haystack == NULL)
17551 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017552
17553 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017554 if (argvars[2].v_type != VAR_UNKNOWN)
17555 {
17556 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017557 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017558 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017559 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017560 }
17561 else
17562 end_idx = haystack_len;
17563
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017565 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017566 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017567 lastmatch = haystack + end_idx;
17568 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017570 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017571 for (rest = haystack; *rest != '\0'; ++rest)
17572 {
17573 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017574 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575 break;
17576 lastmatch = rest;
17577 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017578 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579
17580 if (lastmatch == NULL)
17581 rettv->vval.v_number = -1;
17582 else
17583 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17584}
17585
17586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 * "strtrans()" function
17588 */
17589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017590f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017591 typval_T *argvars;
17592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017594 rettv->v_type = VAR_STRING;
17595 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596}
17597
17598/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017599 * "submatch()" function
17600 */
17601 static void
17602f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017603 typval_T *argvars;
17604 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017605{
17606 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017607 rettv->vval.v_string =
17608 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017609}
17610
17611/*
17612 * "substitute()" function
17613 */
17614 static void
17615f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 typval_T *argvars;
17617 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017618{
17619 char_u patbuf[NUMBUFLEN];
17620 char_u subbuf[NUMBUFLEN];
17621 char_u flagsbuf[NUMBUFLEN];
17622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017623 char_u *str = get_tv_string_chk(&argvars[0]);
17624 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17625 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17626 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17627
Bram Moolenaar0d660222005-01-07 21:51:51 +000017628 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017629 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17630 rettv->vval.v_string = NULL;
17631 else
17632 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017633}
17634
17635/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017636 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017639f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017640 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642{
17643 int id = 0;
17644#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017645 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 long col;
17647 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017648 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017650 lnum = get_tv_lnum(argvars); /* -1 on type error */
17651 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17652 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017654 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017655 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017656 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657#endif
17658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017659 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660}
17661
17662/*
17663 * "synIDattr(id, what [, mode])" function
17664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669{
17670 char_u *p = NULL;
17671#ifdef FEAT_SYN_HL
17672 int id;
17673 char_u *what;
17674 char_u *mode;
17675 char_u modebuf[NUMBUFLEN];
17676 int modec;
17677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017678 id = get_tv_number(&argvars[0]);
17679 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017680 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017684 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 modec = 0; /* replace invalid with current */
17686 }
17687 else
17688 {
17689#ifdef FEAT_GUI
17690 if (gui.in_use)
17691 modec = 'g';
17692 else
17693#endif
17694 if (t_colors > 1)
17695 modec = 'c';
17696 else
17697 modec = 't';
17698 }
17699
17700
17701 switch (TOLOWER_ASC(what[0]))
17702 {
17703 case 'b':
17704 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17705 p = highlight_color(id, what, modec);
17706 else /* bold */
17707 p = highlight_has_attr(id, HL_BOLD, modec);
17708 break;
17709
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017710 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 p = highlight_color(id, what, modec);
17712 break;
17713
17714 case 'i':
17715 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17716 p = highlight_has_attr(id, HL_INVERSE, modec);
17717 else /* italic */
17718 p = highlight_has_attr(id, HL_ITALIC, modec);
17719 break;
17720
17721 case 'n': /* name */
17722 p = get_highlight_name(NULL, id - 1);
17723 break;
17724
17725 case 'r': /* reverse */
17726 p = highlight_has_attr(id, HL_INVERSE, modec);
17727 break;
17728
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017729 case 's':
17730 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17731 p = highlight_color(id, what, modec);
17732 else /* standout */
17733 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 break;
17735
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017736 case 'u':
17737 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17738 /* underline */
17739 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17740 else
17741 /* undercurl */
17742 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 break;
17744 }
17745
17746 if (p != NULL)
17747 p = vim_strsave(p);
17748#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749 rettv->v_type = VAR_STRING;
17750 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751}
17752
17753/*
17754 * "synIDtrans(id)" function
17755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017757f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760{
17761 int id;
17762
17763#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765
17766 if (id > 0)
17767 id = syn_get_final_id(id);
17768 else
17769#endif
17770 id = 0;
17771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017772 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773}
17774
17775/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017776 * "synconcealed(lnum, col)" function
17777 */
17778 static void
17779f_synconcealed(argvars, rettv)
17780 typval_T *argvars UNUSED;
17781 typval_T *rettv;
17782{
17783#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17784 long lnum;
17785 long col;
17786 int syntax_flags = 0;
17787 int cchar;
17788 int matchid = 0;
17789 char_u str[NUMBUFLEN];
17790#endif
17791
17792 rettv->v_type = VAR_LIST;
17793 rettv->vval.v_list = NULL;
17794
17795#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17796 lnum = get_tv_lnum(argvars); /* -1 on type error */
17797 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17798
17799 vim_memset(str, NUL, sizeof(str));
17800
17801 if (rettv_list_alloc(rettv) != FAIL)
17802 {
17803 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17804 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17805 && curwin->w_p_cole > 0)
17806 {
17807 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17808 syntax_flags = get_syntax_info(&matchid);
17809
17810 /* get the conceal character */
17811 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17812 {
17813 cchar = syn_get_sub_char();
17814 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17815 cchar = lcs_conceal;
17816 if (cchar != NUL)
17817 {
17818# ifdef FEAT_MBYTE
17819 if (has_mbyte)
17820 (*mb_char2bytes)(cchar, str);
17821 else
17822# endif
17823 str[0] = cchar;
17824 }
17825 }
17826 }
17827
17828 list_append_number(rettv->vval.v_list,
17829 (syntax_flags & HL_CONCEAL) != 0);
17830 /* -1 to auto-determine strlen */
17831 list_append_string(rettv->vval.v_list, str, -1);
17832 list_append_number(rettv->vval.v_list, matchid);
17833 }
17834#endif
17835}
17836
17837/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017838 * "synstack(lnum, col)" function
17839 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017840 static void
17841f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017842 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017843 typval_T *rettv;
17844{
17845#ifdef FEAT_SYN_HL
17846 long lnum;
17847 long col;
17848 int i;
17849 int id;
17850#endif
17851
17852 rettv->v_type = VAR_LIST;
17853 rettv->vval.v_list = NULL;
17854
17855#ifdef FEAT_SYN_HL
17856 lnum = get_tv_lnum(argvars); /* -1 on type error */
17857 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17858
17859 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017860 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017861 && rettv_list_alloc(rettv) != FAIL)
17862 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017863 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017864 for (i = 0; ; ++i)
17865 {
17866 id = syn_get_stack_item(i);
17867 if (id < 0)
17868 break;
17869 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17870 break;
17871 }
17872 }
17873#endif
17874}
17875
17876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 * "system()" function
17878 */
17879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017880f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017881 typval_T *argvars;
17882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017884 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017886 char_u *infile = NULL;
17887 char_u buf[NUMBUFLEN];
17888 int err = FALSE;
17889 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017891 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017892 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017893
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017894 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017895 {
17896 /*
17897 * Write the string to a temp file, to be used for input of the shell
17898 * command.
17899 */
17900 if ((infile = vim_tempname('i')) == NULL)
17901 {
17902 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017903 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017904 }
17905
17906 fd = mch_fopen((char *)infile, WRITEBIN);
17907 if (fd == NULL)
17908 {
17909 EMSG2(_(e_notopen), infile);
17910 goto done;
17911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017912 p = get_tv_string_buf_chk(&argvars[1], buf);
17913 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017914 {
17915 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017916 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017917 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017918 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17919 err = TRUE;
17920 if (fclose(fd) != 0)
17921 err = TRUE;
17922 if (err)
17923 {
17924 EMSG(_("E677: Error writing temp file"));
17925 goto done;
17926 }
17927 }
17928
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017929 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17930 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017931
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932#ifdef USE_CR
17933 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017934 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 {
17936 char_u *s;
17937
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017938 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 {
17940 if (*s == CAR)
17941 *s = NL;
17942 }
17943 }
17944#else
17945# ifdef USE_CRNL
17946 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017947 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 {
17949 char_u *s, *d;
17950
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017951 d = res;
17952 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 {
17954 if (s[0] == CAR && s[1] == NL)
17955 ++s;
17956 *d++ = *s;
17957 }
17958 *d = NUL;
17959 }
17960# endif
17961#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017962
17963done:
17964 if (infile != NULL)
17965 {
17966 mch_remove(infile);
17967 vim_free(infile);
17968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017969 rettv->v_type = VAR_STRING;
17970 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971}
17972
17973/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017974 * "tabpagebuflist()" function
17975 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017976 static void
17977f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017978 typval_T *argvars UNUSED;
17979 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017980{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017981#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017982 tabpage_T *tp;
17983 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017984
17985 if (argvars[0].v_type == VAR_UNKNOWN)
17986 wp = firstwin;
17987 else
17988 {
17989 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17990 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017991 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017992 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017993 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017994 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017995 for (; wp != NULL; wp = wp->w_next)
17996 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017997 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017998 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017999 }
18000#endif
18001}
18002
18003
18004/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018005 * "tabpagenr()" function
18006 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018007 static void
18008f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018009 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018010 typval_T *rettv;
18011{
18012 int nr = 1;
18013#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018014 char_u *arg;
18015
18016 if (argvars[0].v_type != VAR_UNKNOWN)
18017 {
18018 arg = get_tv_string_chk(&argvars[0]);
18019 nr = 0;
18020 if (arg != NULL)
18021 {
18022 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018023 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018024 else
18025 EMSG2(_(e_invexpr2), arg);
18026 }
18027 }
18028 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018029 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018030#endif
18031 rettv->vval.v_number = nr;
18032}
18033
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018034
18035#ifdef FEAT_WINDOWS
18036static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18037
18038/*
18039 * Common code for tabpagewinnr() and winnr().
18040 */
18041 static int
18042get_winnr(tp, argvar)
18043 tabpage_T *tp;
18044 typval_T *argvar;
18045{
18046 win_T *twin;
18047 int nr = 1;
18048 win_T *wp;
18049 char_u *arg;
18050
18051 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18052 if (argvar->v_type != VAR_UNKNOWN)
18053 {
18054 arg = get_tv_string_chk(argvar);
18055 if (arg == NULL)
18056 nr = 0; /* type error; errmsg already given */
18057 else if (STRCMP(arg, "$") == 0)
18058 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18059 else if (STRCMP(arg, "#") == 0)
18060 {
18061 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18062 if (twin == NULL)
18063 nr = 0;
18064 }
18065 else
18066 {
18067 EMSG2(_(e_invexpr2), arg);
18068 nr = 0;
18069 }
18070 }
18071
18072 if (nr > 0)
18073 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18074 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018075 {
18076 if (wp == NULL)
18077 {
18078 /* didn't find it in this tabpage */
18079 nr = 0;
18080 break;
18081 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018082 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018083 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018084 return nr;
18085}
18086#endif
18087
18088/*
18089 * "tabpagewinnr()" function
18090 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018091 static void
18092f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018093 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018094 typval_T *rettv;
18095{
18096 int nr = 1;
18097#ifdef FEAT_WINDOWS
18098 tabpage_T *tp;
18099
18100 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18101 if (tp == NULL)
18102 nr = 0;
18103 else
18104 nr = get_winnr(tp, &argvars[1]);
18105#endif
18106 rettv->vval.v_number = nr;
18107}
18108
18109
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018110/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018111 * "tagfiles()" function
18112 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018113 static void
18114f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018115 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018116 typval_T *rettv;
18117{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018118 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018119 tagname_T tn;
18120 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018121
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018122 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018123 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018124 fname = alloc(MAXPATHL);
18125 if (fname == NULL)
18126 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018127
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018128 for (first = TRUE; ; first = FALSE)
18129 if (get_tagfname(&tn, first, fname) == FAIL
18130 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018131 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018132 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018133 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018134}
18135
18136/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018137 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018138 */
18139 static void
18140f_taglist(argvars, rettv)
18141 typval_T *argvars;
18142 typval_T *rettv;
18143{
18144 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018145
18146 tag_pattern = get_tv_string(&argvars[0]);
18147
18148 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018149 if (*tag_pattern == NUL)
18150 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018151
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018152 if (rettv_list_alloc(rettv) == OK)
18153 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018154}
18155
18156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 * "tempname()" function
18158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018160f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018161 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163{
18164 static int x = 'A';
18165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018166 rettv->v_type = VAR_STRING;
18167 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168
18169 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18170 * names. Skip 'I' and 'O', they are used for shell redirection. */
18171 do
18172 {
18173 if (x == 'Z')
18174 x = '0';
18175 else if (x == '9')
18176 x = 'A';
18177 else
18178 {
18179#ifdef EBCDIC
18180 if (x == 'I')
18181 x = 'J';
18182 else if (x == 'R')
18183 x = 'S';
18184 else
18185#endif
18186 ++x;
18187 }
18188 } while (x == 'I' || x == 'O');
18189}
18190
18191/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018192 * "test(list)" function: Just checking the walls...
18193 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018194 static void
18195f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018196 typval_T *argvars UNUSED;
18197 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018198{
18199 /* Used for unit testing. Change the code below to your liking. */
18200#if 0
18201 listitem_T *li;
18202 list_T *l;
18203 char_u *bad, *good;
18204
18205 if (argvars[0].v_type != VAR_LIST)
18206 return;
18207 l = argvars[0].vval.v_list;
18208 if (l == NULL)
18209 return;
18210 li = l->lv_first;
18211 if (li == NULL)
18212 return;
18213 bad = get_tv_string(&li->li_tv);
18214 li = li->li_next;
18215 if (li == NULL)
18216 return;
18217 good = get_tv_string(&li->li_tv);
18218 rettv->vval.v_number = test_edit_score(bad, good);
18219#endif
18220}
18221
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018222#ifdef FEAT_FLOAT
18223/*
18224 * "tan()" function
18225 */
18226 static void
18227f_tan(argvars, rettv)
18228 typval_T *argvars;
18229 typval_T *rettv;
18230{
18231 float_T f;
18232
18233 rettv->v_type = VAR_FLOAT;
18234 if (get_float_arg(argvars, &f) == OK)
18235 rettv->vval.v_float = tan(f);
18236 else
18237 rettv->vval.v_float = 0.0;
18238}
18239
18240/*
18241 * "tanh()" function
18242 */
18243 static void
18244f_tanh(argvars, rettv)
18245 typval_T *argvars;
18246 typval_T *rettv;
18247{
18248 float_T f;
18249
18250 rettv->v_type = VAR_FLOAT;
18251 if (get_float_arg(argvars, &f) == OK)
18252 rettv->vval.v_float = tanh(f);
18253 else
18254 rettv->vval.v_float = 0.0;
18255}
18256#endif
18257
Bram Moolenaard52d9742005-08-21 22:20:28 +000018258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 * "tolower(string)" function
18260 */
18261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018262f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018263 typval_T *argvars;
18264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265{
18266 char_u *p;
18267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018268 p = vim_strsave(get_tv_string(&argvars[0]));
18269 rettv->v_type = VAR_STRING;
18270 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271
18272 if (p != NULL)
18273 while (*p != NUL)
18274 {
18275#ifdef FEAT_MBYTE
18276 int l;
18277
18278 if (enc_utf8)
18279 {
18280 int c, lc;
18281
18282 c = utf_ptr2char(p);
18283 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018284 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285 /* TODO: reallocate string when byte count changes. */
18286 if (utf_char2len(lc) == l)
18287 utf_char2bytes(lc, p);
18288 p += l;
18289 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018290 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 p += l; /* skip multi-byte character */
18292 else
18293#endif
18294 {
18295 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18296 ++p;
18297 }
18298 }
18299}
18300
18301/*
18302 * "toupper(string)" function
18303 */
18304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018305f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018306 typval_T *argvars;
18307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018309 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018310 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311}
18312
18313/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018314 * "tr(string, fromstr, tostr)" function
18315 */
18316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018317f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018318 typval_T *argvars;
18319 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018320{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018321 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018322 char_u *fromstr;
18323 char_u *tostr;
18324 char_u *p;
18325#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018326 int inlen;
18327 int fromlen;
18328 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018329 int idx;
18330 char_u *cpstr;
18331 int cplen;
18332 int first = TRUE;
18333#endif
18334 char_u buf[NUMBUFLEN];
18335 char_u buf2[NUMBUFLEN];
18336 garray_T ga;
18337
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018338 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018339 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18340 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018341
18342 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018343 rettv->v_type = VAR_STRING;
18344 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018345 if (fromstr == NULL || tostr == NULL)
18346 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018347 ga_init2(&ga, (int)sizeof(char), 80);
18348
18349#ifdef FEAT_MBYTE
18350 if (!has_mbyte)
18351#endif
18352 /* not multi-byte: fromstr and tostr must be the same length */
18353 if (STRLEN(fromstr) != STRLEN(tostr))
18354 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018355#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018356error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018357#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018358 EMSG2(_(e_invarg2), fromstr);
18359 ga_clear(&ga);
18360 return;
18361 }
18362
18363 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018364 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018365 {
18366#ifdef FEAT_MBYTE
18367 if (has_mbyte)
18368 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018369 inlen = (*mb_ptr2len)(in_str);
18370 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018371 cplen = inlen;
18372 idx = 0;
18373 for (p = fromstr; *p != NUL; p += fromlen)
18374 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018375 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018376 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018377 {
18378 for (p = tostr; *p != NUL; p += tolen)
18379 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018380 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018381 if (idx-- == 0)
18382 {
18383 cplen = tolen;
18384 cpstr = p;
18385 break;
18386 }
18387 }
18388 if (*p == NUL) /* tostr is shorter than fromstr */
18389 goto error;
18390 break;
18391 }
18392 ++idx;
18393 }
18394
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018395 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018396 {
18397 /* Check that fromstr and tostr have the same number of
18398 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018399 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018400 first = FALSE;
18401 for (p = tostr; *p != NUL; p += tolen)
18402 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018403 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018404 --idx;
18405 }
18406 if (idx != 0)
18407 goto error;
18408 }
18409
18410 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018411 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018412 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018413
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018414 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018415 }
18416 else
18417#endif
18418 {
18419 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018420 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018421 if (p != NULL)
18422 ga_append(&ga, tostr[p - fromstr]);
18423 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018424 ga_append(&ga, *in_str);
18425 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018426 }
18427 }
18428
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018429 /* add a terminating NUL */
18430 ga_grow(&ga, 1);
18431 ga_append(&ga, NUL);
18432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018433 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018434}
18435
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018436#ifdef FEAT_FLOAT
18437/*
18438 * "trunc({float})" function
18439 */
18440 static void
18441f_trunc(argvars, rettv)
18442 typval_T *argvars;
18443 typval_T *rettv;
18444{
18445 float_T f;
18446
18447 rettv->v_type = VAR_FLOAT;
18448 if (get_float_arg(argvars, &f) == OK)
18449 /* trunc() is not in C90, use floor() or ceil() instead. */
18450 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18451 else
18452 rettv->vval.v_float = 0.0;
18453}
18454#endif
18455
Bram Moolenaar8299df92004-07-10 09:47:34 +000018456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 * "type(expr)" function
18458 */
18459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018460f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018461 typval_T *argvars;
18462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018464 int n;
18465
18466 switch (argvars[0].v_type)
18467 {
18468 case VAR_NUMBER: n = 0; break;
18469 case VAR_STRING: n = 1; break;
18470 case VAR_FUNC: n = 2; break;
18471 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018472 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018473#ifdef FEAT_FLOAT
18474 case VAR_FLOAT: n = 5; break;
18475#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018476 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18477 }
18478 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479}
18480
18481/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018482 * "undofile(name)" function
18483 */
18484 static void
18485f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018486 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018487 typval_T *rettv;
18488{
18489 rettv->v_type = VAR_STRING;
18490#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018491 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018492 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018493
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018494 if (*fname == NUL)
18495 {
18496 /* If there is no file name there will be no undo file. */
18497 rettv->vval.v_string = NULL;
18498 }
18499 else
18500 {
18501 char_u *ffname = FullName_save(fname, FALSE);
18502
18503 if (ffname != NULL)
18504 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18505 vim_free(ffname);
18506 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018507 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018508#else
18509 rettv->vval.v_string = NULL;
18510#endif
18511}
18512
18513/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018514 * "undotree()" function
18515 */
18516 static void
18517f_undotree(argvars, rettv)
18518 typval_T *argvars UNUSED;
18519 typval_T *rettv;
18520{
18521 if (rettv_dict_alloc(rettv) == OK)
18522 {
18523 dict_T *dict = rettv->vval.v_dict;
18524 list_T *list;
18525
Bram Moolenaar730cde92010-06-27 05:18:54 +020018526 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018527 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018528 dict_add_nr_str(dict, "save_last",
18529 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018530 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18531 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018532 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018533
18534 list = list_alloc();
18535 if (list != NULL)
18536 {
18537 u_eval_tree(curbuf->b_u_oldhead, list);
18538 dict_add_list(dict, "entries", list);
18539 }
18540 }
18541}
18542
18543/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018544 * "values(dict)" function
18545 */
18546 static void
18547f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018548 typval_T *argvars;
18549 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018550{
18551 dict_list(argvars, rettv, 1);
18552}
18553
18554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 * "virtcol(string)" function
18556 */
18557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018559 typval_T *argvars;
18560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561{
18562 colnr_T vcol = 0;
18563 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018564 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018566 fp = var2fpos(&argvars[0], FALSE, &fnum);
18567 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18568 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 {
18570 getvvcol(curwin, fp, NULL, NULL, &vcol);
18571 ++vcol;
18572 }
18573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018574 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575}
18576
18577/*
18578 * "visualmode()" function
18579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018581f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018582 typval_T *argvars UNUSED;
18583 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584{
18585#ifdef FEAT_VISUAL
18586 char_u str[2];
18587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 str[0] = curbuf->b_visual_mode_eval;
18590 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018591 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592
18593 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018594 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596#endif
18597}
18598
18599/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018600 * "wildmenumode()" function
18601 */
18602 static void
18603f_wildmenumode(argvars, rettv)
18604 typval_T *argvars UNUSED;
18605 typval_T *rettv UNUSED;
18606{
18607#ifdef FEAT_WILDMENU
18608 if (wild_menu_showing)
18609 rettv->vval.v_number = 1;
18610#endif
18611}
18612
18613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 * "winbufnr(nr)" function
18615 */
18616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018618 typval_T *argvars;
18619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620{
18621 win_T *wp;
18622
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018623 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018627 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628}
18629
18630/*
18631 * "wincol()" function
18632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018634f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637{
18638 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018639 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640}
18641
18642/*
18643 * "winheight(nr)" function
18644 */
18645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018647 typval_T *argvars;
18648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649{
18650 win_T *wp;
18651
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018652 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018654 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657}
18658
18659/*
18660 * "winline()" function
18661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666{
18667 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018668 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669}
18670
18671/*
18672 * "winnr()" function
18673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018676 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678{
18679 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018680
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018682 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685}
18686
18687/*
18688 * "winrestcmd()" function
18689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018691f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018692 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694{
18695#ifdef FEAT_WINDOWS
18696 win_T *wp;
18697 int winnr = 1;
18698 garray_T ga;
18699 char_u buf[50];
18700
18701 ga_init2(&ga, (int)sizeof(char), 70);
18702 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18703 {
18704 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18705 ga_concat(&ga, buf);
18706# ifdef FEAT_VERTSPLIT
18707 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18708 ga_concat(&ga, buf);
18709# endif
18710 ++winnr;
18711 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018712 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018718 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719}
18720
18721/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018722 * "winrestview()" function
18723 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018724 static void
18725f_winrestview(argvars, rettv)
18726 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018727 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018728{
18729 dict_T *dict;
18730
18731 if (argvars[0].v_type != VAR_DICT
18732 || (dict = argvars[0].vval.v_dict) == NULL)
18733 EMSG(_(e_invarg));
18734 else
18735 {
18736 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18737 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18738#ifdef FEAT_VIRTUALEDIT
18739 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18740#endif
18741 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018742 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018743
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018744 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018745#ifdef FEAT_DIFF
18746 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18747#endif
18748 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18749 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18750
18751 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018752 win_new_height(curwin, curwin->w_height);
18753# ifdef FEAT_VERTSPLIT
18754 win_new_width(curwin, W_WIDTH(curwin));
18755# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018756 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018757
18758 if (curwin->w_topline == 0)
18759 curwin->w_topline = 1;
18760 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18761 curwin->w_topline = curbuf->b_ml.ml_line_count;
18762#ifdef FEAT_DIFF
18763 check_topfill(curwin, TRUE);
18764#endif
18765 }
18766}
18767
18768/*
18769 * "winsaveview()" function
18770 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018771 static void
18772f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018773 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018774 typval_T *rettv;
18775{
18776 dict_T *dict;
18777
Bram Moolenaara800b422010-06-27 01:15:55 +020018778 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018779 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018780 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018781
18782 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18783 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18784#ifdef FEAT_VIRTUALEDIT
18785 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18786#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018787 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018788 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18789
18790 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18791#ifdef FEAT_DIFF
18792 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18793#endif
18794 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18795 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18796}
18797
18798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 * "winwidth(nr)" function
18800 */
18801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018802f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018803 typval_T *argvars;
18804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805{
18806 win_T *wp;
18807
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018808 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 else
18812#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018815 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816#endif
18817}
18818
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018820 * "writefile()" function
18821 */
18822 static void
18823f_writefile(argvars, rettv)
18824 typval_T *argvars;
18825 typval_T *rettv;
18826{
18827 int binary = FALSE;
18828 char_u *fname;
18829 FILE *fd;
18830 listitem_T *li;
18831 char_u *s;
18832 int ret = 0;
18833 int c;
18834
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018835 if (check_restricted() || check_secure())
18836 return;
18837
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018838 if (argvars[0].v_type != VAR_LIST)
18839 {
18840 EMSG2(_(e_listarg), "writefile()");
18841 return;
18842 }
18843 if (argvars[0].vval.v_list == NULL)
18844 return;
18845
18846 if (argvars[2].v_type != VAR_UNKNOWN
18847 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18848 binary = TRUE;
18849
18850 /* Always open the file in binary mode, library functions have a mind of
18851 * their own about CR-LF conversion. */
18852 fname = get_tv_string(&argvars[1]);
18853 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18854 {
18855 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18856 ret = -1;
18857 }
18858 else
18859 {
18860 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18861 li = li->li_next)
18862 {
18863 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18864 {
18865 if (*s == '\n')
18866 c = putc(NUL, fd);
18867 else
18868 c = putc(*s, fd);
18869 if (c == EOF)
18870 {
18871 ret = -1;
18872 break;
18873 }
18874 }
18875 if (!binary || li->li_next != NULL)
18876 if (putc('\n', fd) == EOF)
18877 {
18878 ret = -1;
18879 break;
18880 }
18881 if (ret < 0)
18882 {
18883 EMSG(_(e_write));
18884 break;
18885 }
18886 }
18887 fclose(fd);
18888 }
18889
18890 rettv->vval.v_number = ret;
18891}
18892
18893/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018894 * "xor(expr, expr)" function
18895 */
18896 static void
18897f_xor(argvars, rettv)
18898 typval_T *argvars;
18899 typval_T *rettv;
18900{
18901 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18902 ^ get_tv_number_chk(&argvars[1], NULL);
18903}
18904
18905
18906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018908 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 */
18910 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018911var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018912 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018913 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018914 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018916 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018918 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919
Bram Moolenaara5525202006-03-02 22:52:09 +000018920 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018921 if (varp->v_type == VAR_LIST)
18922 {
18923 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018924 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018925 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018926 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018927
18928 l = varp->vval.v_list;
18929 if (l == NULL)
18930 return NULL;
18931
18932 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018933 pos.lnum = list_find_nr(l, 0L, &error);
18934 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018935 return NULL; /* invalid line number */
18936
18937 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018938 pos.col = list_find_nr(l, 1L, &error);
18939 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018940 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018941 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018942
18943 /* We accept "$" for the column number: last column. */
18944 li = list_find(l, 1L);
18945 if (li != NULL && li->li_tv.v_type == VAR_STRING
18946 && li->li_tv.vval.v_string != NULL
18947 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18948 pos.col = len + 1;
18949
Bram Moolenaara5525202006-03-02 22:52:09 +000018950 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018951 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018952 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018953 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018954
Bram Moolenaara5525202006-03-02 22:52:09 +000018955#ifdef FEAT_VIRTUALEDIT
18956 /* Get the virtual offset. Defaults to zero. */
18957 pos.coladd = list_find_nr(l, 2L, &error);
18958 if (error)
18959 pos.coladd = 0;
18960#endif
18961
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018962 return &pos;
18963 }
18964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018965 name = get_tv_string_chk(varp);
18966 if (name == NULL)
18967 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018968 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018970#ifdef FEAT_VISUAL
18971 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18972 {
18973 if (VIsual_active)
18974 return &VIsual;
18975 return &curwin->w_cursor;
18976 }
18977#endif
18978 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018980 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18982 return NULL;
18983 return pp;
18984 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018985
18986#ifdef FEAT_VIRTUALEDIT
18987 pos.coladd = 0;
18988#endif
18989
Bram Moolenaar477933c2007-07-17 14:32:23 +000018990 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018991 {
18992 pos.col = 0;
18993 if (name[1] == '0') /* "w0": first visible line */
18994 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018995 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018996 pos.lnum = curwin->w_topline;
18997 return &pos;
18998 }
18999 else if (name[1] == '$') /* "w$": last visible line */
19000 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019001 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019002 pos.lnum = curwin->w_botline - 1;
19003 return &pos;
19004 }
19005 }
19006 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019008 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 {
19010 pos.lnum = curbuf->b_ml.ml_line_count;
19011 pos.col = 0;
19012 }
19013 else
19014 {
19015 pos.lnum = curwin->w_cursor.lnum;
19016 pos.col = (colnr_T)STRLEN(ml_get_curline());
19017 }
19018 return &pos;
19019 }
19020 return NULL;
19021}
19022
19023/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019024 * Convert list in "arg" into a position and optional file number.
19025 * When "fnump" is NULL there is no file number, only 3 items.
19026 * Note that the column is passed on as-is, the caller may want to decrement
19027 * it to use 1 for the first column.
19028 * Return FAIL when conversion is not possible, doesn't check the position for
19029 * validity.
19030 */
19031 static int
19032list2fpos(arg, posp, fnump)
19033 typval_T *arg;
19034 pos_T *posp;
19035 int *fnump;
19036{
19037 list_T *l = arg->vval.v_list;
19038 long i = 0;
19039 long n;
19040
Bram Moolenaarbde35262006-07-23 20:12:24 +000019041 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19042 * when "fnump" isn't NULL and "coladd" is optional. */
19043 if (arg->v_type != VAR_LIST
19044 || l == NULL
19045 || l->lv_len < (fnump == NULL ? 2 : 3)
19046 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019047 return FAIL;
19048
19049 if (fnump != NULL)
19050 {
19051 n = list_find_nr(l, i++, NULL); /* fnum */
19052 if (n < 0)
19053 return FAIL;
19054 if (n == 0)
19055 n = curbuf->b_fnum; /* current buffer */
19056 *fnump = n;
19057 }
19058
19059 n = list_find_nr(l, i++, NULL); /* lnum */
19060 if (n < 0)
19061 return FAIL;
19062 posp->lnum = n;
19063
19064 n = list_find_nr(l, i++, NULL); /* col */
19065 if (n < 0)
19066 return FAIL;
19067 posp->col = n;
19068
19069#ifdef FEAT_VIRTUALEDIT
19070 n = list_find_nr(l, i, NULL);
19071 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019072 posp->coladd = 0;
19073 else
19074 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019075#endif
19076
19077 return OK;
19078}
19079
19080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 * Get the length of an environment variable name.
19082 * Advance "arg" to the first character after the name.
19083 * Return 0 for error.
19084 */
19085 static int
19086get_env_len(arg)
19087 char_u **arg;
19088{
19089 char_u *p;
19090 int len;
19091
19092 for (p = *arg; vim_isIDc(*p); ++p)
19093 ;
19094 if (p == *arg) /* no name found */
19095 return 0;
19096
19097 len = (int)(p - *arg);
19098 *arg = p;
19099 return len;
19100}
19101
19102/*
19103 * Get the length of the name of a function or internal variable.
19104 * "arg" is advanced to the first non-white character after the name.
19105 * Return 0 if something is wrong.
19106 */
19107 static int
19108get_id_len(arg)
19109 char_u **arg;
19110{
19111 char_u *p;
19112 int len;
19113
19114 /* Find the end of the name. */
19115 for (p = *arg; eval_isnamec(*p); ++p)
19116 ;
19117 if (p == *arg) /* no name found */
19118 return 0;
19119
19120 len = (int)(p - *arg);
19121 *arg = skipwhite(p);
19122
19123 return len;
19124}
19125
19126/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019127 * Get the length of the name of a variable or function.
19128 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019130 * Return -1 if curly braces expansion failed.
19131 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 * If the name contains 'magic' {}'s, expand them and return the
19133 * expanded name in an allocated string via 'alias' - caller must free.
19134 */
19135 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019136get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 char_u **arg;
19138 char_u **alias;
19139 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019140 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141{
19142 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 char_u *p;
19144 char_u *expr_start;
19145 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146
19147 *alias = NULL; /* default to no alias */
19148
19149 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19150 && (*arg)[2] == (int)KE_SNR)
19151 {
19152 /* hard coded <SNR>, already translated */
19153 *arg += 3;
19154 return get_id_len(arg) + 3;
19155 }
19156 len = eval_fname_script(*arg);
19157 if (len > 0)
19158 {
19159 /* literal "<SID>", "s:" or "<SNR>" */
19160 *arg += len;
19161 }
19162
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019164 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019166 p = find_name_end(*arg, &expr_start, &expr_end,
19167 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 if (expr_start != NULL)
19169 {
19170 char_u *temp_string;
19171
19172 if (!evaluate)
19173 {
19174 len += (int)(p - *arg);
19175 *arg = skipwhite(p);
19176 return len;
19177 }
19178
19179 /*
19180 * Include any <SID> etc in the expanded string:
19181 * Thus the -len here.
19182 */
19183 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19184 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019185 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186 *alias = temp_string;
19187 *arg = skipwhite(p);
19188 return (int)STRLEN(temp_string);
19189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190
19191 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019192 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193 EMSG2(_(e_invexpr2), *arg);
19194
19195 return len;
19196}
19197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019198/*
19199 * Find the end of a variable or function name, taking care of magic braces.
19200 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19201 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019202 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 * Return a pointer to just after the name. Equal to "arg" if there is no
19204 * valid name.
19205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019207find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208 char_u *arg;
19209 char_u **expr_start;
19210 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019211 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019213 int mb_nest = 0;
19214 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215 char_u *p;
19216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219 *expr_start = NULL;
19220 *expr_end = NULL;
19221 }
19222
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019223 /* Quick check for valid starting character. */
19224 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19225 return arg;
19226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019227 for (p = arg; *p != NUL
19228 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019229 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019230 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019232 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019233 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019234 if (*p == '\'')
19235 {
19236 /* skip over 'string' to avoid counting [ and ] inside it. */
19237 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19238 ;
19239 if (*p == NUL)
19240 break;
19241 }
19242 else if (*p == '"')
19243 {
19244 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19245 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19246 if (*p == '\\' && p[1] != NUL)
19247 ++p;
19248 if (*p == NUL)
19249 break;
19250 }
19251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 if (*p == '[')
19255 ++br_nest;
19256 else if (*p == ']')
19257 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019262 if (*p == '{')
19263 {
19264 mb_nest++;
19265 if (expr_start != NULL && *expr_start == NULL)
19266 *expr_start = p;
19267 }
19268 else if (*p == '}')
19269 {
19270 mb_nest--;
19271 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19272 *expr_end = p;
19273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 }
19276
19277 return p;
19278}
19279
19280/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019281 * Expands out the 'magic' {}'s in a variable/function name.
19282 * Note that this can call itself recursively, to deal with
19283 * constructs like foo{bar}{baz}{bam}
19284 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19285 * "in_start" ^
19286 * "expr_start" ^
19287 * "expr_end" ^
19288 * "in_end" ^
19289 *
19290 * Returns a new allocated string, which the caller must free.
19291 * Returns NULL for failure.
19292 */
19293 static char_u *
19294make_expanded_name(in_start, expr_start, expr_end, in_end)
19295 char_u *in_start;
19296 char_u *expr_start;
19297 char_u *expr_end;
19298 char_u *in_end;
19299{
19300 char_u c1;
19301 char_u *retval = NULL;
19302 char_u *temp_result;
19303 char_u *nextcmd = NULL;
19304
19305 if (expr_end == NULL || in_end == NULL)
19306 return NULL;
19307 *expr_start = NUL;
19308 *expr_end = NUL;
19309 c1 = *in_end;
19310 *in_end = NUL;
19311
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019312 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019313 if (temp_result != NULL && nextcmd == NULL)
19314 {
19315 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19316 + (in_end - expr_end) + 1));
19317 if (retval != NULL)
19318 {
19319 STRCPY(retval, in_start);
19320 STRCAT(retval, temp_result);
19321 STRCAT(retval, expr_end + 1);
19322 }
19323 }
19324 vim_free(temp_result);
19325
19326 *in_end = c1; /* put char back for error messages */
19327 *expr_start = '{';
19328 *expr_end = '}';
19329
19330 if (retval != NULL)
19331 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019332 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019333 if (expr_start != NULL)
19334 {
19335 /* Further expansion! */
19336 temp_result = make_expanded_name(retval, expr_start,
19337 expr_end, temp_result);
19338 vim_free(retval);
19339 retval = temp_result;
19340 }
19341 }
19342
19343 return retval;
19344}
19345
19346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019348 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 */
19350 static int
19351eval_isnamec(c)
19352 int c;
19353{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019354 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19355}
19356
19357/*
19358 * Return TRUE if character "c" can be used as the first character in a
19359 * variable or function name (excluding '{' and '}').
19360 */
19361 static int
19362eval_isnamec1(c)
19363 int c;
19364{
19365 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366}
19367
19368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 * Set number v: variable to "val".
19370 */
19371 void
19372set_vim_var_nr(idx, val)
19373 int idx;
19374 long val;
19375{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019376 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377}
19378
19379/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019380 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 */
19382 long
19383get_vim_var_nr(idx)
19384 int idx;
19385{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019386 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387}
19388
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019389/*
19390 * Get string v: variable value. Uses a static buffer, can only be used once.
19391 */
19392 char_u *
19393get_vim_var_str(idx)
19394 int idx;
19395{
19396 return get_tv_string(&vimvars[idx].vv_tv);
19397}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019398
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019400 * Get List v: variable value. Caller must take care of reference count when
19401 * needed.
19402 */
19403 list_T *
19404get_vim_var_list(idx)
19405 int idx;
19406{
19407 return vimvars[idx].vv_list;
19408}
19409
19410/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019411 * Set v:char to character "c".
19412 */
19413 void
19414set_vim_var_char(c)
19415 int c;
19416{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019417 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019418
19419#ifdef FEAT_MBYTE
19420 if (has_mbyte)
19421 buf[(*mb_char2bytes)(c, buf)] = NUL;
19422 else
19423#endif
19424 {
19425 buf[0] = c;
19426 buf[1] = NUL;
19427 }
19428 set_vim_var_string(VV_CHAR, buf, -1);
19429}
19430
19431/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019432 * Set v:count to "count" and v:count1 to "count1".
19433 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 */
19435 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019436set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 long count;
19438 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019439 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019441 if (set_prevcount)
19442 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019443 vimvars[VV_COUNT].vv_nr = count;
19444 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445}
19446
19447/*
19448 * Set string v: variable to a copy of "val".
19449 */
19450 void
19451set_vim_var_string(idx, val, len)
19452 int idx;
19453 char_u *val;
19454 int len; /* length of "val" to use or -1 (whole string) */
19455{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019456 /* Need to do this (at least) once, since we can't initialize a union.
19457 * Will always be invoked when "v:progname" is set. */
19458 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19459
Bram Moolenaare9a41262005-01-15 22:18:47 +000019460 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019462 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019464 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019466 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467}
19468
19469/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019470 * Set List v: variable to "val".
19471 */
19472 void
19473set_vim_var_list(idx, val)
19474 int idx;
19475 list_T *val;
19476{
19477 list_unref(vimvars[idx].vv_list);
19478 vimvars[idx].vv_list = val;
19479 if (val != NULL)
19480 ++val->lv_refcount;
19481}
19482
19483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 * Set v:register if needed.
19485 */
19486 void
19487set_reg_var(c)
19488 int c;
19489{
19490 char_u regname;
19491
19492 if (c == 0 || c == ' ')
19493 regname = '"';
19494 else
19495 regname = c;
19496 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019497 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 set_vim_var_string(VV_REG, &regname, 1);
19499}
19500
19501/*
19502 * Get or set v:exception. If "oldval" == NULL, return the current value.
19503 * Otherwise, restore the value to "oldval" and return NULL.
19504 * Must always be called in pairs to save and restore v:exception! Does not
19505 * take care of memory allocations.
19506 */
19507 char_u *
19508v_exception(oldval)
19509 char_u *oldval;
19510{
19511 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019512 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513
Bram Moolenaare9a41262005-01-15 22:18:47 +000019514 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 return NULL;
19516}
19517
19518/*
19519 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19520 * Otherwise, restore the value to "oldval" and return NULL.
19521 * Must always be called in pairs to save and restore v:throwpoint! Does not
19522 * take care of memory allocations.
19523 */
19524 char_u *
19525v_throwpoint(oldval)
19526 char_u *oldval;
19527{
19528 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019529 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530
Bram Moolenaare9a41262005-01-15 22:18:47 +000019531 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 return NULL;
19533}
19534
19535#if defined(FEAT_AUTOCMD) || defined(PROTO)
19536/*
19537 * Set v:cmdarg.
19538 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19539 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19540 * Must always be called in pairs!
19541 */
19542 char_u *
19543set_cmdarg(eap, oldarg)
19544 exarg_T *eap;
19545 char_u *oldarg;
19546{
19547 char_u *oldval;
19548 char_u *newval;
19549 unsigned len;
19550
Bram Moolenaare9a41262005-01-15 22:18:47 +000019551 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019552 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019554 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019555 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019556 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 }
19558
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019559 if (eap->force_bin == FORCE_BIN)
19560 len = 6;
19561 else if (eap->force_bin == FORCE_NOBIN)
19562 len = 8;
19563 else
19564 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019565
19566 if (eap->read_edit)
19567 len += 7;
19568
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019569 if (eap->force_ff != 0)
19570 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19571# ifdef FEAT_MBYTE
19572 if (eap->force_enc != 0)
19573 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019574 if (eap->bad_char != 0)
19575 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019576# endif
19577
19578 newval = alloc(len + 1);
19579 if (newval == NULL)
19580 return NULL;
19581
19582 if (eap->force_bin == FORCE_BIN)
19583 sprintf((char *)newval, " ++bin");
19584 else if (eap->force_bin == FORCE_NOBIN)
19585 sprintf((char *)newval, " ++nobin");
19586 else
19587 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019588
19589 if (eap->read_edit)
19590 STRCAT(newval, " ++edit");
19591
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019592 if (eap->force_ff != 0)
19593 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19594 eap->cmd + eap->force_ff);
19595# ifdef FEAT_MBYTE
19596 if (eap->force_enc != 0)
19597 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19598 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019599 if (eap->bad_char == BAD_KEEP)
19600 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19601 else if (eap->bad_char == BAD_DROP)
19602 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19603 else if (eap->bad_char != 0)
19604 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019605# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019606 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019607 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608}
19609#endif
19610
19611/*
19612 * Get the value of internal variable "name".
19613 * Return OK or FAIL.
19614 */
19615 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019616get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 char_u *name;
19618 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019620 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621{
19622 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019623 typval_T *tv = NULL;
19624 typval_T atv;
19625 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627
19628 /* truncate the name, so that we can use strcmp() */
19629 cc = name[len];
19630 name[len] = NUL;
19631
19632 /*
19633 * Check for "b:changedtick".
19634 */
19635 if (STRCMP(name, "b:changedtick") == 0)
19636 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019637 atv.v_type = VAR_NUMBER;
19638 atv.vval.v_number = curbuf->b_changedtick;
19639 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 }
19641
19642 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 * Check for user-defined variables.
19644 */
19645 else
19646 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019647 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019649 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650 }
19651
Bram Moolenaare9a41262005-01-15 22:18:47 +000019652 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019654 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019655 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 ret = FAIL;
19657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019658 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019659 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660
19661 name[len] = cc;
19662
19663 return ret;
19664}
19665
19666/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019667 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19668 * Also handle function call with Funcref variable: func(expr)
19669 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19670 */
19671 static int
19672handle_subscript(arg, rettv, evaluate, verbose)
19673 char_u **arg;
19674 typval_T *rettv;
19675 int evaluate; /* do more than finding the end */
19676 int verbose; /* give error messages */
19677{
19678 int ret = OK;
19679 dict_T *selfdict = NULL;
19680 char_u *s;
19681 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019682 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019683
19684 while (ret == OK
19685 && (**arg == '['
19686 || (**arg == '.' && rettv->v_type == VAR_DICT)
19687 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19688 && !vim_iswhite(*(*arg - 1)))
19689 {
19690 if (**arg == '(')
19691 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019692 /* need to copy the funcref so that we can clear rettv */
19693 functv = *rettv;
19694 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019695
19696 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019697 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019698 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019699 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19700 &len, evaluate, selfdict);
19701
19702 /* Clear the funcref afterwards, so that deleting it while
19703 * evaluating the arguments is possible (see test55). */
19704 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019705
19706 /* Stop the expression evaluation when immediately aborting on
19707 * error, or when an interrupt occurred or an exception was thrown
19708 * but not caught. */
19709 if (aborting())
19710 {
19711 if (ret == OK)
19712 clear_tv(rettv);
19713 ret = FAIL;
19714 }
19715 dict_unref(selfdict);
19716 selfdict = NULL;
19717 }
19718 else /* **arg == '[' || **arg == '.' */
19719 {
19720 dict_unref(selfdict);
19721 if (rettv->v_type == VAR_DICT)
19722 {
19723 selfdict = rettv->vval.v_dict;
19724 if (selfdict != NULL)
19725 ++selfdict->dv_refcount;
19726 }
19727 else
19728 selfdict = NULL;
19729 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19730 {
19731 clear_tv(rettv);
19732 ret = FAIL;
19733 }
19734 }
19735 }
19736 dict_unref(selfdict);
19737 return ret;
19738}
19739
19740/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019741 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019742 * value).
19743 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019744 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019745alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019746{
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019748}
19749
19750/*
19751 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 * The string "s" must have been allocated, it is consumed.
19753 * Return NULL for out of memory, the variable otherwise.
19754 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019755 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 char_u *s;
19758{
Bram Moolenaar33570922005-01-25 22:26:29 +000019759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019761 rettv = alloc_tv();
19762 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764 rettv->v_type = VAR_STRING;
19765 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 }
19767 else
19768 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770}
19771
19772/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019773 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019775 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019776free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019777 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778{
19779 if (varp != NULL)
19780 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019781 switch (varp->v_type)
19782 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019783 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019784 func_unref(varp->vval.v_string);
19785 /*FALLTHROUGH*/
19786 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019787 vim_free(varp->vval.v_string);
19788 break;
19789 case VAR_LIST:
19790 list_unref(varp->vval.v_list);
19791 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019792 case VAR_DICT:
19793 dict_unref(varp->vval.v_dict);
19794 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019795 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019796#ifdef FEAT_FLOAT
19797 case VAR_FLOAT:
19798#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019799 case VAR_UNKNOWN:
19800 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019801 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019802 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019803 break;
19804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 vim_free(varp);
19806 }
19807}
19808
19809/*
19810 * Free the memory for a variable value and set the value to NULL or 0.
19811 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019812 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019813clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815{
19816 if (varp != NULL)
19817 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019818 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019820 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019821 func_unref(varp->vval.v_string);
19822 /*FALLTHROUGH*/
19823 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019824 vim_free(varp->vval.v_string);
19825 varp->vval.v_string = NULL;
19826 break;
19827 case VAR_LIST:
19828 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019829 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019830 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019831 case VAR_DICT:
19832 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019833 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019834 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019836 varp->vval.v_number = 0;
19837 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019838#ifdef FEAT_FLOAT
19839 case VAR_FLOAT:
19840 varp->vval.v_float = 0.0;
19841 break;
19842#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 case VAR_UNKNOWN:
19844 break;
19845 default:
19846 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019848 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 }
19850}
19851
19852/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019853 * Set the value of a variable to NULL without freeing items.
19854 */
19855 static void
19856init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019857 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019858{
19859 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019860 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019861}
19862
19863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864 * Get the number value of a variable.
19865 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019866 * For incompatible types, return 0.
19867 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19868 * caller of incompatible types: it sets *denote to TRUE if "denote"
19869 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870 */
19871 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019872get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019873 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019875 int error = FALSE;
19876
19877 return get_tv_number_chk(varp, &error); /* return 0L on error */
19878}
19879
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019880 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019881get_tv_number_chk(varp, denote)
19882 typval_T *varp;
19883 int *denote;
19884{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019885 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019887 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019889 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019890 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019891#ifdef FEAT_FLOAT
19892 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019893 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019894 break;
19895#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019896 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019897 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019898 break;
19899 case VAR_STRING:
19900 if (varp->vval.v_string != NULL)
19901 vim_str2nr(varp->vval.v_string, NULL, NULL,
19902 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019903 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019904 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019905 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019906 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019907 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019908 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019909 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019910 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019911 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019912 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019914 if (denote == NULL) /* useful for values that must be unsigned */
19915 n = -1;
19916 else
19917 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019918 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919}
19920
19921/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019922 * Get the lnum from the first argument.
19923 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019924 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 */
19926 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019927get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019928 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929{
Bram Moolenaar33570922005-01-25 22:26:29 +000019930 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 linenr_T lnum;
19932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019933 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 if (lnum == 0) /* no valid number, try using line() */
19935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019936 rettv.v_type = VAR_NUMBER;
19937 f_line(argvars, &rettv);
19938 lnum = rettv.vval.v_number;
19939 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 }
19941 return lnum;
19942}
19943
19944/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019945 * Get the lnum from the first argument.
19946 * Also accepts "$", then "buf" is used.
19947 * Returns 0 on error.
19948 */
19949 static linenr_T
19950get_tv_lnum_buf(argvars, buf)
19951 typval_T *argvars;
19952 buf_T *buf;
19953{
19954 if (argvars[0].v_type == VAR_STRING
19955 && argvars[0].vval.v_string != NULL
19956 && argvars[0].vval.v_string[0] == '$'
19957 && buf != NULL)
19958 return buf->b_ml.ml_line_count;
19959 return get_tv_number_chk(&argvars[0], NULL);
19960}
19961
19962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 * Get the string value of a variable.
19964 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019965 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19966 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 * If the String variable has never been set, return an empty string.
19968 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019969 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19970 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 */
19972 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019974 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975{
19976 static char_u mybuf[NUMBUFLEN];
19977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979}
19980
19981 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019984 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019986 char_u *res = get_tv_string_buf_chk(varp, buf);
19987
19988 return res != NULL ? res : (char_u *)"";
19989}
19990
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019991 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019992get_tv_string_chk(varp)
19993 typval_T *varp;
19994{
19995 static char_u mybuf[NUMBUFLEN];
19996
19997 return get_tv_string_buf_chk(varp, mybuf);
19998}
19999
20000 static char_u *
20001get_tv_string_buf_chk(varp, buf)
20002 typval_T *varp;
20003 char_u *buf;
20004{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020005 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020007 case VAR_NUMBER:
20008 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20009 return buf;
20010 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020011 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020012 break;
20013 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020014 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020015 break;
20016 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020017 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020018 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020019#ifdef FEAT_FLOAT
20020 case VAR_FLOAT:
20021 EMSG(_("E806: using Float as a String"));
20022 break;
20023#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020024 case VAR_STRING:
20025 if (varp->vval.v_string != NULL)
20026 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020027 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020028 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020030 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020032 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033}
20034
20035/*
20036 * Find variable "name" in the list of variables.
20037 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020038 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020039 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020042 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020043find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020045 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020048 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049
Bram Moolenaara7043832005-01-21 11:56:39 +000020050 ht = find_var_ht(name, &varname);
20051 if (htp != NULL)
20052 *htp = ht;
20053 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020055 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056}
20057
20058/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020059 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020060 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020063find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020065 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020066 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020067 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020068{
Bram Moolenaar33570922005-01-25 22:26:29 +000020069 hashitem_T *hi;
20070
20071 if (*varname == NUL)
20072 {
20073 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020074 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020076 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 case 'g': return &globvars_var;
20078 case 'v': return &vimvars_var;
20079 case 'b': return &curbuf->b_bufvar;
20080 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020081#ifdef FEAT_WINDOWS
20082 case 't': return &curtab->tp_winvar;
20083#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020084 case 'l': return current_funccal == NULL
20085 ? NULL : &current_funccal->l_vars_var;
20086 case 'a': return current_funccal == NULL
20087 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 }
20089 return NULL;
20090 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020091
20092 hi = hash_find(ht, varname);
20093 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020094 {
20095 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020096 * worked find the variable again. Don't auto-load a script if it was
20097 * loaded already, otherwise it would be loaded every time when
20098 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020099 if (ht == &globvarht && !writing)
20100 {
20101 /* Note: script_autoload() may make "hi" invalid. It must either
20102 * be obtained again or not used. */
20103 if (!script_autoload(varname, FALSE) || aborting())
20104 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020105 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020106 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020107 if (HASHITEM_EMPTY(hi))
20108 return NULL;
20109 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020110 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020111}
20112
20113/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020115 * Set "varname" to the start of name without ':'.
20116 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020117 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020118find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 char_u *name;
20120 char_u **varname;
20121{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020122 hashitem_T *hi;
20123
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 if (name[1] != ':')
20125 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020126 /* The name must not start with a colon or #. */
20127 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 return NULL;
20129 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020130
20131 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020132 hi = hash_find(&compat_hashtab, name);
20133 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020134 return &compat_hashtab;
20135
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020137 return &globvarht; /* global variable */
20138 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 }
20140 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020141 if (*name == 'g') /* global variable */
20142 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020143 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20144 */
20145 if (vim_strchr(name + 2, ':') != NULL
20146 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020147 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020149 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020151 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020152#ifdef FEAT_WINDOWS
20153 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020154 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020155#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 if (*name == 'v') /* v: variable */
20157 return &vimvarht;
20158 if (*name == 'a' && current_funccal != NULL) /* function argument */
20159 return &current_funccal->l_avars.dv_hashtab;
20160 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20161 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 if (*name == 's' /* script variable */
20163 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20164 return &SCRIPT_VARS(current_SID);
20165 return NULL;
20166}
20167
20168/*
20169 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020170 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 * Returns NULL when it doesn't exist.
20172 */
20173 char_u *
20174get_var_value(name)
20175 char_u *name;
20176{
Bram Moolenaar33570922005-01-25 22:26:29 +000020177 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178
Bram Moolenaara7043832005-01-21 11:56:39 +000020179 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 if (v == NULL)
20181 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020182 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183}
20184
20185/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 * sourcing this script and when executing functions defined in the script.
20188 */
20189 void
20190new_script_vars(id)
20191 scid_T id;
20192{
Bram Moolenaara7043832005-01-21 11:56:39 +000020193 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020194 hashtab_T *ht;
20195 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020196
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20198 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020199 /* Re-allocating ga_data means that an ht_array pointing to
20200 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020202 for (i = 1; i <= ga_scripts.ga_len; ++i)
20203 {
20204 ht = &SCRIPT_VARS(i);
20205 if (ht->ht_mask == HT_INIT_SIZE - 1)
20206 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020207 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020208 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020209 }
20210
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 while (ga_scripts.ga_len < id)
20212 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020213 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020214 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020215 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 }
20218 }
20219}
20220
20221/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020222 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20223 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 */
20225 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020226init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020227 dict_T *dict;
20228 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020229 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230{
Bram Moolenaar33570922005-01-25 22:26:29 +000020231 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020232 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020233 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020234 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020235 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 dict_var->di_tv.vval.v_dict = dict;
20237 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020238 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020239 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20240 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241}
20242
20243/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020244 * Unreference a dictionary initialized by init_var_dict().
20245 */
20246 void
20247unref_var_dict(dict)
20248 dict_T *dict;
20249{
20250 /* Now the dict needs to be freed if no one else is using it, go back to
20251 * normal reference counting. */
20252 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20253 dict_unref(dict);
20254}
20255
20256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020258 * Frees all allocated variables and the value they contain.
20259 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 */
20261 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020262vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020263 hashtab_T *ht;
20264{
20265 vars_clear_ext(ht, TRUE);
20266}
20267
20268/*
20269 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20270 */
20271 static void
20272vars_clear_ext(ht, free_val)
20273 hashtab_T *ht;
20274 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275{
Bram Moolenaara7043832005-01-21 11:56:39 +000020276 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020277 hashitem_T *hi;
20278 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020281 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020282 for (hi = ht->ht_array; todo > 0; ++hi)
20283 {
20284 if (!HASHITEM_EMPTY(hi))
20285 {
20286 --todo;
20287
Bram Moolenaar33570922005-01-25 22:26:29 +000020288 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020289 * ht_array might change then. hash_clear() takes care of it
20290 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020291 v = HI2DI(hi);
20292 if (free_val)
20293 clear_tv(&v->di_tv);
20294 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20295 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020296 }
20297 }
20298 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020299 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300}
20301
Bram Moolenaara7043832005-01-21 11:56:39 +000020302/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 * Delete a variable from hashtab "ht" at item "hi".
20304 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020307delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020308 hashtab_T *ht;
20309 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310{
Bram Moolenaar33570922005-01-25 22:26:29 +000020311 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020312
20313 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020314 clear_tv(&di->di_tv);
20315 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316}
20317
20318/*
20319 * List the value of one internal variable.
20320 */
20321 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020322list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020325 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020327 char_u *tofree;
20328 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020329 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020330
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020331 current_copyID += COPYID_INC;
20332 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020333 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020334 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020335 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336}
20337
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020339list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 char_u *prefix;
20341 char_u *name;
20342 int type;
20343 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020344 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345{
Bram Moolenaar31859182007-08-14 20:41:13 +000020346 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20347 msg_start();
20348 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 if (name != NULL) /* "a:" vars don't have a name stored */
20350 msg_puts(name);
20351 msg_putchar(' ');
20352 msg_advance(22);
20353 if (type == VAR_NUMBER)
20354 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020355 else if (type == VAR_FUNC)
20356 msg_putchar('*');
20357 else if (type == VAR_LIST)
20358 {
20359 msg_putchar('[');
20360 if (*string == '[')
20361 ++string;
20362 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020363 else if (type == VAR_DICT)
20364 {
20365 msg_putchar('{');
20366 if (*string == '{')
20367 ++string;
20368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 else
20370 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020371
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020373
20374 if (type == VAR_FUNC)
20375 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020376 if (*first)
20377 {
20378 msg_clr_eos();
20379 *first = FALSE;
20380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381}
20382
20383/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020384 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 * If the variable already exists, the value is updated.
20386 * Otherwise the variable is created.
20387 */
20388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020389set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020391 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020392 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393{
Bram Moolenaar33570922005-01-25 22:26:29 +000020394 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020396 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020398 ht = find_var_ht(name, &varname);
20399 if (ht == NULL || *varname == NUL)
20400 {
20401 EMSG2(_(e_illvar), name);
20402 return;
20403 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020404 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020405
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020406 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20407 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020408
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020411 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020412 if (var_check_ro(v->di_flags, name)
20413 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020414 return;
20415 if (v->di_tv.v_type != tv->v_type
20416 && !((v->di_tv.v_type == VAR_STRING
20417 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020418 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020419 || tv->v_type == VAR_NUMBER))
20420#ifdef FEAT_FLOAT
20421 && !((v->di_tv.v_type == VAR_NUMBER
20422 || v->di_tv.v_type == VAR_FLOAT)
20423 && (tv->v_type == VAR_NUMBER
20424 || tv->v_type == VAR_FLOAT))
20425#endif
20426 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020427 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020428 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020429 return;
20430 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020431
20432 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020433 * Handle setting internal v: variables separately: we don't change
20434 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020435 */
20436 if (ht == &vimvarht)
20437 {
20438 if (v->di_tv.v_type == VAR_STRING)
20439 {
20440 vim_free(v->di_tv.vval.v_string);
20441 if (copy || tv->v_type != VAR_STRING)
20442 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20443 else
20444 {
20445 /* Take over the string to avoid an extra alloc/free. */
20446 v->di_tv.vval.v_string = tv->vval.v_string;
20447 tv->vval.v_string = NULL;
20448 }
20449 }
20450 else if (v->di_tv.v_type != VAR_NUMBER)
20451 EMSG2(_(e_intern2), "set_var()");
20452 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020453 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020455 if (STRCMP(varname, "searchforward") == 0)
20456 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20457 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020458 return;
20459 }
20460
20461 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 }
20463 else /* add a new variable */
20464 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020465 /* Can't add "v:" variable. */
20466 if (ht == &vimvarht)
20467 {
20468 EMSG2(_(e_illvar), name);
20469 return;
20470 }
20471
Bram Moolenaar92124a32005-06-17 22:03:40 +000020472 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020473 if (!valid_varname(varname))
20474 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020475
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020476 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20477 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020478 if (v == NULL)
20479 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020480 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020481 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020483 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 return;
20485 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020486 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020488
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020489 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020490 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020491 else
20492 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020494 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497}
20498
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020499/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020500 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020501 * Also give an error message.
20502 */
20503 static int
20504var_check_ro(flags, name)
20505 int flags;
20506 char_u *name;
20507{
20508 if (flags & DI_FLAGS_RO)
20509 {
20510 EMSG2(_(e_readonlyvar), name);
20511 return TRUE;
20512 }
20513 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20514 {
20515 EMSG2(_(e_readonlysbx), name);
20516 return TRUE;
20517 }
20518 return FALSE;
20519}
20520
20521/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020522 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20523 * Also give an error message.
20524 */
20525 static int
20526var_check_fixed(flags, name)
20527 int flags;
20528 char_u *name;
20529{
20530 if (flags & DI_FLAGS_FIX)
20531 {
20532 EMSG2(_("E795: Cannot delete variable %s"), name);
20533 return TRUE;
20534 }
20535 return FALSE;
20536}
20537
20538/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020539 * Check if a funcref is assigned to a valid variable name.
20540 * Return TRUE and give an error if not.
20541 */
20542 static int
20543var_check_func_name(name, new_var)
20544 char_u *name; /* points to start of variable name */
20545 int new_var; /* TRUE when creating the variable */
20546{
20547 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20548 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20549 ? name[2] : name[0]))
20550 {
20551 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20552 name);
20553 return TRUE;
20554 }
20555 /* Don't allow hiding a function. When "v" is not NULL we might be
20556 * assigning another function to the same var, the type is checked
20557 * below. */
20558 if (new_var && function_exists(name))
20559 {
20560 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20561 name);
20562 return TRUE;
20563 }
20564 return FALSE;
20565}
20566
20567/*
20568 * Check if a variable name is valid.
20569 * Return FALSE and give an error if not.
20570 */
20571 static int
20572valid_varname(varname)
20573 char_u *varname;
20574{
20575 char_u *p;
20576
20577 for (p = varname; *p != NUL; ++p)
20578 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20579 && *p != AUTOLOAD_CHAR)
20580 {
20581 EMSG2(_(e_illvar), varname);
20582 return FALSE;
20583 }
20584 return TRUE;
20585}
20586
20587/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020588 * Return TRUE if typeval "tv" is set to be locked (immutable).
20589 * Also give an error message, using "name".
20590 */
20591 static int
20592tv_check_lock(lock, name)
20593 int lock;
20594 char_u *name;
20595{
20596 if (lock & VAR_LOCKED)
20597 {
20598 EMSG2(_("E741: Value is locked: %s"),
20599 name == NULL ? (char_u *)_("Unknown") : name);
20600 return TRUE;
20601 }
20602 if (lock & VAR_FIXED)
20603 {
20604 EMSG2(_("E742: Cannot change value of %s"),
20605 name == NULL ? (char_u *)_("Unknown") : name);
20606 return TRUE;
20607 }
20608 return FALSE;
20609}
20610
20611/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020612 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020613 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020614 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020615 * It is OK for "from" and "to" to point to the same item. This is used to
20616 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020617 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020618 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020619copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020620 typval_T *from;
20621 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020623 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020624 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020625 switch (from->v_type)
20626 {
20627 case VAR_NUMBER:
20628 to->vval.v_number = from->vval.v_number;
20629 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020630#ifdef FEAT_FLOAT
20631 case VAR_FLOAT:
20632 to->vval.v_float = from->vval.v_float;
20633 break;
20634#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020635 case VAR_STRING:
20636 case VAR_FUNC:
20637 if (from->vval.v_string == NULL)
20638 to->vval.v_string = NULL;
20639 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020641 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020642 if (from->v_type == VAR_FUNC)
20643 func_ref(to->vval.v_string);
20644 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020645 break;
20646 case VAR_LIST:
20647 if (from->vval.v_list == NULL)
20648 to->vval.v_list = NULL;
20649 else
20650 {
20651 to->vval.v_list = from->vval.v_list;
20652 ++to->vval.v_list->lv_refcount;
20653 }
20654 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020655 case VAR_DICT:
20656 if (from->vval.v_dict == NULL)
20657 to->vval.v_dict = NULL;
20658 else
20659 {
20660 to->vval.v_dict = from->vval.v_dict;
20661 ++to->vval.v_dict->dv_refcount;
20662 }
20663 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020664 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020665 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020666 break;
20667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668}
20669
20670/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020671 * Make a copy of an item.
20672 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020673 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20674 * reference to an already copied list/dict can be used.
20675 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020676 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020677 static int
20678item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020679 typval_T *from;
20680 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020681 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020682 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020683{
20684 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020685 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020686
Bram Moolenaar33570922005-01-25 22:26:29 +000020687 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020688 {
20689 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020690 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020691 }
20692 ++recurse;
20693
20694 switch (from->v_type)
20695 {
20696 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020697#ifdef FEAT_FLOAT
20698 case VAR_FLOAT:
20699#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020700 case VAR_STRING:
20701 case VAR_FUNC:
20702 copy_tv(from, to);
20703 break;
20704 case VAR_LIST:
20705 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020706 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020707 if (from->vval.v_list == NULL)
20708 to->vval.v_list = NULL;
20709 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20710 {
20711 /* use the copy made earlier */
20712 to->vval.v_list = from->vval.v_list->lv_copylist;
20713 ++to->vval.v_list->lv_refcount;
20714 }
20715 else
20716 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20717 if (to->vval.v_list == NULL)
20718 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020719 break;
20720 case VAR_DICT:
20721 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020722 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020723 if (from->vval.v_dict == NULL)
20724 to->vval.v_dict = NULL;
20725 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20726 {
20727 /* use the copy made earlier */
20728 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20729 ++to->vval.v_dict->dv_refcount;
20730 }
20731 else
20732 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20733 if (to->vval.v_dict == NULL)
20734 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020735 break;
20736 default:
20737 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020738 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020739 }
20740 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020741 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020742}
20743
20744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 * ":echo expr1 ..." print each argument separated with a space, add a
20746 * newline at the end.
20747 * ":echon expr1 ..." print each argument plain.
20748 */
20749 void
20750ex_echo(eap)
20751 exarg_T *eap;
20752{
20753 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020754 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020755 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 char_u *p;
20757 int needclr = TRUE;
20758 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020759 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760
20761 if (eap->skip)
20762 ++emsg_skip;
20763 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20764 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020765 /* If eval1() causes an error message the text from the command may
20766 * still need to be cleared. E.g., "echo 22,44". */
20767 need_clr_eos = needclr;
20768
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020770 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 {
20772 /*
20773 * Report the invalid expression unless the expression evaluation
20774 * has been cancelled due to an aborting error, an interrupt, or an
20775 * exception.
20776 */
20777 if (!aborting())
20778 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020779 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 break;
20781 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020782 need_clr_eos = FALSE;
20783
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 if (!eap->skip)
20785 {
20786 if (atstart)
20787 {
20788 atstart = FALSE;
20789 /* Call msg_start() after eval1(), evaluating the expression
20790 * may cause a message to appear. */
20791 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020792 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020793 /* Mark the saved text as finishing the line, so that what
20794 * follows is displayed on a new line when scrolling back
20795 * at the more prompt. */
20796 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 }
20800 else if (eap->cmdidx == CMD_echo)
20801 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020802 current_copyID += COPYID_INC;
20803 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020804 if (p != NULL)
20805 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020807 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020809 if (*p != TAB && needclr)
20810 {
20811 /* remove any text still there from the command */
20812 msg_clr_eos();
20813 needclr = FALSE;
20814 }
20815 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 }
20817 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020818 {
20819#ifdef FEAT_MBYTE
20820 if (has_mbyte)
20821 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020822 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020823
20824 (void)msg_outtrans_len_attr(p, i, echo_attr);
20825 p += i - 1;
20826 }
20827 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020829 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020832 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020834 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 arg = skipwhite(arg);
20836 }
20837 eap->nextcmd = check_nextcmd(arg);
20838
20839 if (eap->skip)
20840 --emsg_skip;
20841 else
20842 {
20843 /* remove text that may still be there from the command */
20844 if (needclr)
20845 msg_clr_eos();
20846 if (eap->cmdidx == CMD_echo)
20847 msg_end();
20848 }
20849}
20850
20851/*
20852 * ":echohl {name}".
20853 */
20854 void
20855ex_echohl(eap)
20856 exarg_T *eap;
20857{
20858 int id;
20859
20860 id = syn_name2id(eap->arg);
20861 if (id == 0)
20862 echo_attr = 0;
20863 else
20864 echo_attr = syn_id2attr(id);
20865}
20866
20867/*
20868 * ":execute expr1 ..." execute the result of an expression.
20869 * ":echomsg expr1 ..." Print a message
20870 * ":echoerr expr1 ..." Print an error
20871 * Each gets spaces around each argument and a newline at the end for
20872 * echo commands
20873 */
20874 void
20875ex_execute(eap)
20876 exarg_T *eap;
20877{
20878 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020879 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 int ret = OK;
20881 char_u *p;
20882 garray_T ga;
20883 int len;
20884 int save_did_emsg;
20885
20886 ga_init2(&ga, 1, 80);
20887
20888 if (eap->skip)
20889 ++emsg_skip;
20890 while (*arg != NUL && *arg != '|' && *arg != '\n')
20891 {
20892 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020893 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 {
20895 /*
20896 * Report the invalid expression unless the expression evaluation
20897 * has been cancelled due to an aborting error, an interrupt, or an
20898 * exception.
20899 */
20900 if (!aborting())
20901 EMSG2(_(e_invexpr2), p);
20902 ret = FAIL;
20903 break;
20904 }
20905
20906 if (!eap->skip)
20907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020908 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 len = (int)STRLEN(p);
20910 if (ga_grow(&ga, len + 2) == FAIL)
20911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020912 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 ret = FAIL;
20914 break;
20915 }
20916 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 ga.ga_len += len;
20920 }
20921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020922 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 arg = skipwhite(arg);
20924 }
20925
20926 if (ret != FAIL && ga.ga_data != NULL)
20927 {
20928 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020931 out_flush();
20932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 else if (eap->cmdidx == CMD_echoerr)
20934 {
20935 /* We don't want to abort following commands, restore did_emsg. */
20936 save_did_emsg = did_emsg;
20937 EMSG((char_u *)ga.ga_data);
20938 if (!force_abort)
20939 did_emsg = save_did_emsg;
20940 }
20941 else if (eap->cmdidx == CMD_execute)
20942 do_cmdline((char_u *)ga.ga_data,
20943 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20944 }
20945
20946 ga_clear(&ga);
20947
20948 if (eap->skip)
20949 --emsg_skip;
20950
20951 eap->nextcmd = check_nextcmd(arg);
20952}
20953
20954/*
20955 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20956 * "arg" points to the "&" or '+' when called, to "option" when returning.
20957 * Returns NULL when no option name found. Otherwise pointer to the char
20958 * after the option name.
20959 */
20960 static char_u *
20961find_option_end(arg, opt_flags)
20962 char_u **arg;
20963 int *opt_flags;
20964{
20965 char_u *p = *arg;
20966
20967 ++p;
20968 if (*p == 'g' && p[1] == ':')
20969 {
20970 *opt_flags = OPT_GLOBAL;
20971 p += 2;
20972 }
20973 else if (*p == 'l' && p[1] == ':')
20974 {
20975 *opt_flags = OPT_LOCAL;
20976 p += 2;
20977 }
20978 else
20979 *opt_flags = 0;
20980
20981 if (!ASCII_ISALPHA(*p))
20982 return NULL;
20983 *arg = p;
20984
20985 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20986 p += 4; /* termcap option */
20987 else
20988 while (ASCII_ISALPHA(*p))
20989 ++p;
20990 return p;
20991}
20992
20993/*
20994 * ":function"
20995 */
20996 void
20997ex_function(eap)
20998 exarg_T *eap;
20999{
21000 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021001 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 int j;
21003 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 char_u *name = NULL;
21006 char_u *p;
21007 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021008 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 garray_T newargs;
21010 garray_T newlines;
21011 int varargs = FALSE;
21012 int mustend = FALSE;
21013 int flags = 0;
21014 ufunc_T *fp;
21015 int indent;
21016 int nesting;
21017 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021018 dictitem_T *v;
21019 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021020 static int func_nr = 0; /* number for nameless function */
21021 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021022 hashtab_T *ht;
21023 int todo;
21024 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021025 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026
21027 /*
21028 * ":function" without argument: list functions.
21029 */
21030 if (ends_excmd(*eap->arg))
21031 {
21032 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021033 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021034 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021035 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021036 {
21037 if (!HASHITEM_EMPTY(hi))
21038 {
21039 --todo;
21040 fp = HI2UF(hi);
21041 if (!isdigit(*fp->uf_name))
21042 list_func_head(fp, FALSE);
21043 }
21044 }
21045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 eap->nextcmd = check_nextcmd(eap->arg);
21047 return;
21048 }
21049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021050 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021051 * ":function /pat": list functions matching pattern.
21052 */
21053 if (*eap->arg == '/')
21054 {
21055 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21056 if (!eap->skip)
21057 {
21058 regmatch_T regmatch;
21059
21060 c = *p;
21061 *p = NUL;
21062 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21063 *p = c;
21064 if (regmatch.regprog != NULL)
21065 {
21066 regmatch.rm_ic = p_ic;
21067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021068 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021069 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21070 {
21071 if (!HASHITEM_EMPTY(hi))
21072 {
21073 --todo;
21074 fp = HI2UF(hi);
21075 if (!isdigit(*fp->uf_name)
21076 && vim_regexec(&regmatch, fp->uf_name, 0))
21077 list_func_head(fp, FALSE);
21078 }
21079 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021080 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021081 }
21082 }
21083 if (*p == '/')
21084 ++p;
21085 eap->nextcmd = check_nextcmd(p);
21086 return;
21087 }
21088
21089 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021090 * Get the function name. There are these situations:
21091 * func normal function name
21092 * "name" == func, "fudi.fd_dict" == NULL
21093 * dict.func new dictionary entry
21094 * "name" == NULL, "fudi.fd_dict" set,
21095 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21096 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021097 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021098 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21099 * dict.func existing dict entry that's not a Funcref
21100 * "name" == NULL, "fudi.fd_dict" set,
21101 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021104 name = trans_function_name(&p, eap->skip, 0, &fudi);
21105 paren = (vim_strchr(p, '(') != NULL);
21106 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 {
21108 /*
21109 * Return on an invalid expression in braces, unless the expression
21110 * evaluation has been cancelled due to an aborting error, an
21111 * interrupt, or an exception.
21112 */
21113 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021115 if (!eap->skip && fudi.fd_newkey != NULL)
21116 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021117 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 else
21121 eap->skip = TRUE;
21122 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021123
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 /* An error in a function call during evaluation of an expression in magic
21125 * braces should not cause the function not to be defined. */
21126 saved_did_emsg = did_emsg;
21127 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128
21129 /*
21130 * ":function func" with only function name: list function.
21131 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021132 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 {
21134 if (!ends_excmd(*skipwhite(p)))
21135 {
21136 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021137 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 }
21139 eap->nextcmd = check_nextcmd(p);
21140 if (eap->nextcmd != NULL)
21141 *p = NUL;
21142 if (!eap->skip && !got_int)
21143 {
21144 fp = find_func(name);
21145 if (fp != NULL)
21146 {
21147 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021148 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021150 if (FUNCLINE(fp, j) == NULL)
21151 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 msg_putchar('\n');
21153 msg_outnum((long)(j + 1));
21154 if (j < 9)
21155 msg_putchar(' ');
21156 if (j < 99)
21157 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021158 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 out_flush(); /* show a line at a time */
21160 ui_breakcheck();
21161 }
21162 if (!got_int)
21163 {
21164 msg_putchar('\n');
21165 msg_puts((char_u *)" endfunction");
21166 }
21167 }
21168 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021169 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021171 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 }
21173
21174 /*
21175 * ":function name(arg1, arg2)" Define function.
21176 */
21177 p = skipwhite(p);
21178 if (*p != '(')
21179 {
21180 if (!eap->skip)
21181 {
21182 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021183 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 }
21185 /* attempt to continue by skipping some text */
21186 if (vim_strchr(p, '(') != NULL)
21187 p = vim_strchr(p, '(');
21188 }
21189 p = skipwhite(p + 1);
21190
21191 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21192 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21193
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021194 if (!eap->skip)
21195 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021196 /* Check the name of the function. Unless it's a dictionary function
21197 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021198 if (name != NULL)
21199 arg = name;
21200 else
21201 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021202 if (arg != NULL && (fudi.fd_di == NULL
21203 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021204 {
21205 if (*arg == K_SPECIAL)
21206 j = 3;
21207 else
21208 j = 0;
21209 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21210 : eval_isnamec(arg[j])))
21211 ++j;
21212 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021213 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021214 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021215 /* Disallow using the g: dict. */
21216 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21217 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021218 }
21219
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 /*
21221 * Isolate the arguments: "arg1, arg2, ...)"
21222 */
21223 while (*p != ')')
21224 {
21225 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21226 {
21227 varargs = TRUE;
21228 p += 3;
21229 mustend = TRUE;
21230 }
21231 else
21232 {
21233 arg = p;
21234 while (ASCII_ISALNUM(*p) || *p == '_')
21235 ++p;
21236 if (arg == p || isdigit(*arg)
21237 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21238 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21239 {
21240 if (!eap->skip)
21241 EMSG2(_("E125: Illegal argument: %s"), arg);
21242 break;
21243 }
21244 if (ga_grow(&newargs, 1) == FAIL)
21245 goto erret;
21246 c = *p;
21247 *p = NUL;
21248 arg = vim_strsave(arg);
21249 if (arg == NULL)
21250 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021251
21252 /* Check for duplicate argument name. */
21253 for (i = 0; i < newargs.ga_len; ++i)
21254 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21255 {
21256 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21257 goto erret;
21258 }
21259
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21261 *p = c;
21262 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 if (*p == ',')
21264 ++p;
21265 else
21266 mustend = TRUE;
21267 }
21268 p = skipwhite(p);
21269 if (mustend && *p != ')')
21270 {
21271 if (!eap->skip)
21272 EMSG2(_(e_invarg2), eap->arg);
21273 break;
21274 }
21275 }
21276 ++p; /* skip the ')' */
21277
Bram Moolenaare9a41262005-01-15 22:18:47 +000021278 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 for (;;)
21280 {
21281 p = skipwhite(p);
21282 if (STRNCMP(p, "range", 5) == 0)
21283 {
21284 flags |= FC_RANGE;
21285 p += 5;
21286 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021287 else if (STRNCMP(p, "dict", 4) == 0)
21288 {
21289 flags |= FC_DICT;
21290 p += 4;
21291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 else if (STRNCMP(p, "abort", 5) == 0)
21293 {
21294 flags |= FC_ABORT;
21295 p += 5;
21296 }
21297 else
21298 break;
21299 }
21300
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021301 /* When there is a line break use what follows for the function body.
21302 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21303 if (*p == '\n')
21304 line_arg = p + 1;
21305 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 EMSG(_(e_trailing));
21307
21308 /*
21309 * Read the body of the function, until ":endfunction" is found.
21310 */
21311 if (KeyTyped)
21312 {
21313 /* Check if the function already exists, don't let the user type the
21314 * whole function before telling him it doesn't work! For a script we
21315 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021316 if (!eap->skip && !eap->forceit)
21317 {
21318 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21319 EMSG(_(e_funcdict));
21320 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021321 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021324 if (!eap->skip && did_emsg)
21325 goto erret;
21326
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 msg_putchar('\n'); /* don't overwrite the function name */
21328 cmdline_row = msg_row;
21329 }
21330
21331 indent = 2;
21332 nesting = 0;
21333 for (;;)
21334 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021335 if (KeyTyped)
21336 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021338 sourcing_lnum_off = sourcing_lnum;
21339
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021340 if (line_arg != NULL)
21341 {
21342 /* Use eap->arg, split up in parts by line breaks. */
21343 theline = line_arg;
21344 p = vim_strchr(theline, '\n');
21345 if (p == NULL)
21346 line_arg += STRLEN(line_arg);
21347 else
21348 {
21349 *p = NUL;
21350 line_arg = p + 1;
21351 }
21352 }
21353 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 theline = getcmdline(':', 0L, indent);
21355 else
21356 theline = eap->getline(':', eap->cookie, indent);
21357 if (KeyTyped)
21358 lines_left = Rows - 1;
21359 if (theline == NULL)
21360 {
21361 EMSG(_("E126: Missing :endfunction"));
21362 goto erret;
21363 }
21364
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021365 /* Detect line continuation: sourcing_lnum increased more than one. */
21366 if (sourcing_lnum > sourcing_lnum_off + 1)
21367 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21368 else
21369 sourcing_lnum_off = 0;
21370
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 if (skip_until != NULL)
21372 {
21373 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21374 * don't check for ":endfunc". */
21375 if (STRCMP(theline, skip_until) == 0)
21376 {
21377 vim_free(skip_until);
21378 skip_until = NULL;
21379 }
21380 }
21381 else
21382 {
21383 /* skip ':' and blanks*/
21384 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21385 ;
21386
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021387 /* Check for "endfunction". */
21388 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021390 if (line_arg == NULL)
21391 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 break;
21393 }
21394
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021395 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 * at "end". */
21397 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21398 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021399 else if (STRNCMP(p, "if", 2) == 0
21400 || STRNCMP(p, "wh", 2) == 0
21401 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 || STRNCMP(p, "try", 3) == 0)
21403 indent += 2;
21404
21405 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021406 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021408 if (*p == '!')
21409 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 p += eval_fname_script(p);
21411 if (ASCII_ISALPHA(*p))
21412 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021413 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 if (*skipwhite(p) == '(')
21415 {
21416 ++nesting;
21417 indent += 2;
21418 }
21419 }
21420 }
21421
21422 /* Check for ":append" or ":insert". */
21423 p = skip_range(p, NULL);
21424 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21425 || (p[0] == 'i'
21426 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21427 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21428 skip_until = vim_strsave((char_u *)".");
21429
21430 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21431 arg = skipwhite(skiptowhite(p));
21432 if (arg[0] == '<' && arg[1] =='<'
21433 && ((p[0] == 'p' && p[1] == 'y'
21434 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21435 || (p[0] == 'p' && p[1] == 'e'
21436 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21437 || (p[0] == 't' && p[1] == 'c'
21438 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021439 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21440 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21442 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021443 || (p[0] == 'm' && p[1] == 'z'
21444 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 ))
21446 {
21447 /* ":python <<" continues until a dot, like ":append" */
21448 p = skipwhite(arg + 2);
21449 if (*p == NUL)
21450 skip_until = vim_strsave((char_u *)".");
21451 else
21452 skip_until = vim_strsave(p);
21453 }
21454 }
21455
21456 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021457 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021458 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021459 if (line_arg == NULL)
21460 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021462 }
21463
21464 /* Copy the line to newly allocated memory. get_one_sourceline()
21465 * allocates 250 bytes per line, this saves 80% on average. The cost
21466 * is an extra alloc/free. */
21467 p = vim_strsave(theline);
21468 if (p != NULL)
21469 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021470 if (line_arg == NULL)
21471 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021472 theline = p;
21473 }
21474
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021475 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21476
21477 /* Add NULL lines for continuation lines, so that the line count is
21478 * equal to the index in the growarray. */
21479 while (sourcing_lnum_off-- > 0)
21480 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021481
21482 /* Check for end of eap->arg. */
21483 if (line_arg != NULL && *line_arg == NUL)
21484 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 }
21486
21487 /* Don't define the function when skipping commands or when an error was
21488 * detected. */
21489 if (eap->skip || did_emsg)
21490 goto erret;
21491
21492 /*
21493 * If there are no errors, add the function
21494 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021495 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021496 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021497 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021498 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021499 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021500 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021501 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 goto erret;
21503 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021504
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 fp = find_func(name);
21506 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021508 if (!eap->forceit)
21509 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021510 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021511 goto erret;
21512 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021513 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021514 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021515 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021516 name);
21517 goto erret;
21518 }
21519 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021520 ga_clear_strings(&(fp->uf_args));
21521 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021522 vim_free(name);
21523 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 }
21526 else
21527 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021528 char numbuf[20];
21529
21530 fp = NULL;
21531 if (fudi.fd_newkey == NULL && !eap->forceit)
21532 {
21533 EMSG(_(e_funcdict));
21534 goto erret;
21535 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021536 if (fudi.fd_di == NULL)
21537 {
21538 /* Can't add a function to a locked dictionary */
21539 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21540 goto erret;
21541 }
21542 /* Can't change an existing function if it is locked */
21543 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21544 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021545
21546 /* Give the function a sequential number. Can only be used with a
21547 * Funcref! */
21548 vim_free(name);
21549 sprintf(numbuf, "%d", ++func_nr);
21550 name = vim_strsave((char_u *)numbuf);
21551 if (name == NULL)
21552 goto erret;
21553 }
21554
21555 if (fp == NULL)
21556 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021557 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021558 {
21559 int slen, plen;
21560 char_u *scriptname;
21561
21562 /* Check that the autoload name matches the script name. */
21563 j = FAIL;
21564 if (sourcing_name != NULL)
21565 {
21566 scriptname = autoload_name(name);
21567 if (scriptname != NULL)
21568 {
21569 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021570 plen = (int)STRLEN(p);
21571 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021572 if (slen > plen && fnamecmp(p,
21573 sourcing_name + slen - plen) == 0)
21574 j = OK;
21575 vim_free(scriptname);
21576 }
21577 }
21578 if (j == FAIL)
21579 {
21580 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21581 goto erret;
21582 }
21583 }
21584
21585 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 if (fp == NULL)
21587 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021588
21589 if (fudi.fd_dict != NULL)
21590 {
21591 if (fudi.fd_di == NULL)
21592 {
21593 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021594 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021595 if (fudi.fd_di == NULL)
21596 {
21597 vim_free(fp);
21598 goto erret;
21599 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021600 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21601 {
21602 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021603 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021604 goto erret;
21605 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021606 }
21607 else
21608 /* overwrite existing dict entry */
21609 clear_tv(&fudi.fd_di->di_tv);
21610 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021611 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021612 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021613 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021614
21615 /* behave like "dict" was used */
21616 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021617 }
21618
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021620 STRCPY(fp->uf_name, name);
21621 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021623 fp->uf_args = newargs;
21624 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021625#ifdef FEAT_PROFILE
21626 fp->uf_tml_count = NULL;
21627 fp->uf_tml_total = NULL;
21628 fp->uf_tml_self = NULL;
21629 fp->uf_profiling = FALSE;
21630 if (prof_def_func())
21631 func_do_profile(fp);
21632#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021633 fp->uf_varargs = varargs;
21634 fp->uf_flags = flags;
21635 fp->uf_calls = 0;
21636 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021637 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638
21639erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 ga_clear_strings(&newargs);
21641 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021642ret_free:
21643 vim_free(skip_until);
21644 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647}
21648
21649/*
21650 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021651 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021653 * flags:
21654 * TFN_INT: internal function name OK
21655 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 * Advances "pp" to just after the function name (if no error).
21657 */
21658 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021659trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 char_u **pp;
21661 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021662 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021665 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 char_u *start;
21667 char_u *end;
21668 int lead;
21669 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021671 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021672
21673 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021674 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021676
21677 /* Check for hard coded <SNR>: already translated function ID (from a user
21678 * command). */
21679 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21680 && (*pp)[2] == (int)KE_SNR)
21681 {
21682 *pp += 3;
21683 len = get_id_len(pp) + 3;
21684 return vim_strnsave(start, len);
21685 }
21686
21687 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21688 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021690 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021692
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021693 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21694 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021695 if (end == start)
21696 {
21697 if (!skip)
21698 EMSG(_("E129: Function name required"));
21699 goto theend;
21700 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021701 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021702 {
21703 /*
21704 * Report an invalid expression in braces, unless the expression
21705 * evaluation has been cancelled due to an aborting error, an
21706 * interrupt, or an exception.
21707 */
21708 if (!aborting())
21709 {
21710 if (end != NULL)
21711 EMSG2(_(e_invarg2), start);
21712 }
21713 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021714 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021715 goto theend;
21716 }
21717
21718 if (lv.ll_tv != NULL)
21719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021720 if (fdp != NULL)
21721 {
21722 fdp->fd_dict = lv.ll_dict;
21723 fdp->fd_newkey = lv.ll_newkey;
21724 lv.ll_newkey = NULL;
21725 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021726 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021727 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21728 {
21729 name = vim_strsave(lv.ll_tv->vval.v_string);
21730 *pp = end;
21731 }
21732 else
21733 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021734 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21735 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021736 EMSG(_(e_funcref));
21737 else
21738 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021739 name = NULL;
21740 }
21741 goto theend;
21742 }
21743
21744 if (lv.ll_name == NULL)
21745 {
21746 /* Error found, but continue after the function name. */
21747 *pp = end;
21748 goto theend;
21749 }
21750
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021751 /* Check if the name is a Funcref. If so, use the value. */
21752 if (lv.ll_exp_name != NULL)
21753 {
21754 len = (int)STRLEN(lv.ll_exp_name);
21755 name = deref_func_name(lv.ll_exp_name, &len);
21756 if (name == lv.ll_exp_name)
21757 name = NULL;
21758 }
21759 else
21760 {
21761 len = (int)(end - *pp);
21762 name = deref_func_name(*pp, &len);
21763 if (name == *pp)
21764 name = NULL;
21765 }
21766 if (name != NULL)
21767 {
21768 name = vim_strsave(name);
21769 *pp = end;
21770 goto theend;
21771 }
21772
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021773 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021774 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021775 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021776 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21777 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21778 {
21779 /* When there was "s:" already or the name expanded to get a
21780 * leading "s:" then remove it. */
21781 lv.ll_name += 2;
21782 len -= 2;
21783 lead = 2;
21784 }
21785 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021786 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021787 {
21788 if (lead == 2) /* skip over "s:" */
21789 lv.ll_name += 2;
21790 len = (int)(end - lv.ll_name);
21791 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021792
21793 /*
21794 * Copy the function name to allocated memory.
21795 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21796 * Accept <SNR>123_name() outside a script.
21797 */
21798 if (skip)
21799 lead = 0; /* do nothing */
21800 else if (lead > 0)
21801 {
21802 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021803 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21804 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021805 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021806 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021807 if (current_SID <= 0)
21808 {
21809 EMSG(_(e_usingsid));
21810 goto theend;
21811 }
21812 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21813 lead += (int)STRLEN(sid_buf);
21814 }
21815 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021816 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021817 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021818 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021819 goto theend;
21820 }
21821 name = alloc((unsigned)(len + lead + 1));
21822 if (name != NULL)
21823 {
21824 if (lead > 0)
21825 {
21826 name[0] = K_SPECIAL;
21827 name[1] = KS_EXTRA;
21828 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021829 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021830 STRCPY(name + 3, sid_buf);
21831 }
21832 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21833 name[len + lead] = NUL;
21834 }
21835 *pp = end;
21836
21837theend:
21838 clear_lval(&lv);
21839 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840}
21841
21842/*
21843 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21844 * Return 2 if "p" starts with "s:".
21845 * Return 0 otherwise.
21846 */
21847 static int
21848eval_fname_script(p)
21849 char_u *p;
21850{
21851 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21852 || STRNICMP(p + 1, "SNR>", 4) == 0))
21853 return 5;
21854 if (p[0] == 's' && p[1] == ':')
21855 return 2;
21856 return 0;
21857}
21858
21859/*
21860 * Return TRUE if "p" starts with "<SID>" or "s:".
21861 * Only works if eval_fname_script() returned non-zero for "p"!
21862 */
21863 static int
21864eval_fname_sid(p)
21865 char_u *p;
21866{
21867 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21868}
21869
21870/*
21871 * List the head of the function: "name(arg1, arg2)".
21872 */
21873 static void
21874list_func_head(fp, indent)
21875 ufunc_T *fp;
21876 int indent;
21877{
21878 int j;
21879
21880 msg_start();
21881 if (indent)
21882 MSG_PUTS(" ");
21883 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021884 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 {
21886 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021887 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 }
21889 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021890 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021892 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 {
21894 if (j)
21895 MSG_PUTS(", ");
21896 msg_puts(FUNCARG(fp, j));
21897 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021898 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 {
21900 if (j)
21901 MSG_PUTS(", ");
21902 MSG_PUTS("...");
21903 }
21904 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021905 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021906 if (p_verbose > 0)
21907 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908}
21909
21910/*
21911 * Find a function by name, return pointer to it in ufuncs.
21912 * Return NULL for unknown function.
21913 */
21914 static ufunc_T *
21915find_func(name)
21916 char_u *name;
21917{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021918 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021920 hi = hash_find(&func_hashtab, name);
21921 if (!HASHITEM_EMPTY(hi))
21922 return HI2UF(hi);
21923 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924}
21925
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021926#if defined(EXITFREE) || defined(PROTO)
21927 void
21928free_all_functions()
21929{
21930 hashitem_T *hi;
21931
21932 /* Need to start all over every time, because func_free() may change the
21933 * hash table. */
21934 while (func_hashtab.ht_used > 0)
21935 for (hi = func_hashtab.ht_array; ; ++hi)
21936 if (!HASHITEM_EMPTY(hi))
21937 {
21938 func_free(HI2UF(hi));
21939 break;
21940 }
21941}
21942#endif
21943
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021944/*
21945 * Return TRUE if a function "name" exists.
21946 */
21947 static int
21948function_exists(name)
21949 char_u *name;
21950{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021951 char_u *nm = name;
21952 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021953 int n = FALSE;
21954
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021955 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021956 nm = skipwhite(nm);
21957
21958 /* Only accept "funcname", "funcname ", "funcname (..." and
21959 * "funcname(...", not "funcname!...". */
21960 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021961 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021962 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021963 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021964 else
21965 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021966 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021967 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021968 return n;
21969}
21970
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021971/*
21972 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021973 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021974 */
21975 static int
21976builtin_function(name)
21977 char_u *name;
21978{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021979 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21980 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021981}
21982
Bram Moolenaar05159a02005-02-26 23:04:13 +000021983#if defined(FEAT_PROFILE) || defined(PROTO)
21984/*
21985 * Start profiling function "fp".
21986 */
21987 static void
21988func_do_profile(fp)
21989 ufunc_T *fp;
21990{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021991 int len = fp->uf_lines.ga_len;
21992
21993 if (len == 0)
21994 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021995 fp->uf_tm_count = 0;
21996 profile_zero(&fp->uf_tm_self);
21997 profile_zero(&fp->uf_tm_total);
21998 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021999 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022000 if (fp->uf_tml_total == NULL)
22001 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022002 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022003 if (fp->uf_tml_self == NULL)
22004 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022005 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022006 fp->uf_tml_idx = -1;
22007 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22008 || fp->uf_tml_self == NULL)
22009 return; /* out of memory */
22010
22011 fp->uf_profiling = TRUE;
22012}
22013
22014/*
22015 * Dump the profiling results for all functions in file "fd".
22016 */
22017 void
22018func_dump_profile(fd)
22019 FILE *fd;
22020{
22021 hashitem_T *hi;
22022 int todo;
22023 ufunc_T *fp;
22024 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022025 ufunc_T **sorttab;
22026 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022027
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022028 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022029 if (todo == 0)
22030 return; /* nothing to dump */
22031
Bram Moolenaar73830342005-02-28 22:48:19 +000022032 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22033
Bram Moolenaar05159a02005-02-26 23:04:13 +000022034 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22035 {
22036 if (!HASHITEM_EMPTY(hi))
22037 {
22038 --todo;
22039 fp = HI2UF(hi);
22040 if (fp->uf_profiling)
22041 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022042 if (sorttab != NULL)
22043 sorttab[st_len++] = fp;
22044
Bram Moolenaar05159a02005-02-26 23:04:13 +000022045 if (fp->uf_name[0] == K_SPECIAL)
22046 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22047 else
22048 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22049 if (fp->uf_tm_count == 1)
22050 fprintf(fd, "Called 1 time\n");
22051 else
22052 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22053 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22054 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22055 fprintf(fd, "\n");
22056 fprintf(fd, "count total (s) self (s)\n");
22057
22058 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22059 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022060 if (FUNCLINE(fp, i) == NULL)
22061 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022062 prof_func_line(fd, fp->uf_tml_count[i],
22063 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022064 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22065 }
22066 fprintf(fd, "\n");
22067 }
22068 }
22069 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022070
22071 if (sorttab != NULL && st_len > 0)
22072 {
22073 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22074 prof_total_cmp);
22075 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22076 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22077 prof_self_cmp);
22078 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22079 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022080
22081 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022082}
Bram Moolenaar73830342005-02-28 22:48:19 +000022083
22084 static void
22085prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22086 FILE *fd;
22087 ufunc_T **sorttab;
22088 int st_len;
22089 char *title;
22090 int prefer_self; /* when equal print only self time */
22091{
22092 int i;
22093 ufunc_T *fp;
22094
22095 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22096 fprintf(fd, "count total (s) self (s) function\n");
22097 for (i = 0; i < 20 && i < st_len; ++i)
22098 {
22099 fp = sorttab[i];
22100 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22101 prefer_self);
22102 if (fp->uf_name[0] == K_SPECIAL)
22103 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22104 else
22105 fprintf(fd, " %s()\n", fp->uf_name);
22106 }
22107 fprintf(fd, "\n");
22108}
22109
22110/*
22111 * Print the count and times for one function or function line.
22112 */
22113 static void
22114prof_func_line(fd, count, total, self, prefer_self)
22115 FILE *fd;
22116 int count;
22117 proftime_T *total;
22118 proftime_T *self;
22119 int prefer_self; /* when equal print only self time */
22120{
22121 if (count > 0)
22122 {
22123 fprintf(fd, "%5d ", count);
22124 if (prefer_self && profile_equal(total, self))
22125 fprintf(fd, " ");
22126 else
22127 fprintf(fd, "%s ", profile_msg(total));
22128 if (!prefer_self && profile_equal(total, self))
22129 fprintf(fd, " ");
22130 else
22131 fprintf(fd, "%s ", profile_msg(self));
22132 }
22133 else
22134 fprintf(fd, " ");
22135}
22136
22137/*
22138 * Compare function for total time sorting.
22139 */
22140 static int
22141#ifdef __BORLANDC__
22142_RTLENTRYF
22143#endif
22144prof_total_cmp(s1, s2)
22145 const void *s1;
22146 const void *s2;
22147{
22148 ufunc_T *p1, *p2;
22149
22150 p1 = *(ufunc_T **)s1;
22151 p2 = *(ufunc_T **)s2;
22152 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22153}
22154
22155/*
22156 * Compare function for self time sorting.
22157 */
22158 static int
22159#ifdef __BORLANDC__
22160_RTLENTRYF
22161#endif
22162prof_self_cmp(s1, s2)
22163 const void *s1;
22164 const void *s2;
22165{
22166 ufunc_T *p1, *p2;
22167
22168 p1 = *(ufunc_T **)s1;
22169 p2 = *(ufunc_T **)s2;
22170 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22171}
22172
Bram Moolenaar05159a02005-02-26 23:04:13 +000022173#endif
22174
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022175/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022176 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022177 * Return TRUE if a package was loaded.
22178 */
22179 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022180script_autoload(name, reload)
22181 char_u *name;
22182 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022183{
22184 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022185 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022186 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022187 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022188
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022189 /* Return quickly when autoload disabled. */
22190 if (no_autoload)
22191 return FALSE;
22192
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022193 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022194 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022195 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022196 return FALSE;
22197
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022198 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022199
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022200 /* Find the name in the list of previously loaded package names. Skip
22201 * "autoload/", it's always the same. */
22202 for (i = 0; i < ga_loaded.ga_len; ++i)
22203 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22204 break;
22205 if (!reload && i < ga_loaded.ga_len)
22206 ret = FALSE; /* was loaded already */
22207 else
22208 {
22209 /* Remember the name if it wasn't loaded already. */
22210 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22211 {
22212 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22213 tofree = NULL;
22214 }
22215
22216 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022217 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022218 ret = TRUE;
22219 }
22220
22221 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022222 return ret;
22223}
22224
22225/*
22226 * Return the autoload script name for a function or variable name.
22227 * Returns NULL when out of memory.
22228 */
22229 static char_u *
22230autoload_name(name)
22231 char_u *name;
22232{
22233 char_u *p;
22234 char_u *scriptname;
22235
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022236 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022237 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22238 if (scriptname == NULL)
22239 return FALSE;
22240 STRCPY(scriptname, "autoload/");
22241 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022242 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022243 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022244 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022245 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022246 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022247}
22248
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22250
22251/*
22252 * Function given to ExpandGeneric() to obtain the list of user defined
22253 * function names.
22254 */
22255 char_u *
22256get_user_func_name(xp, idx)
22257 expand_T *xp;
22258 int idx;
22259{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022260 static long_u done;
22261 static hashitem_T *hi;
22262 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263
22264 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022266 done = 0;
22267 hi = func_hashtab.ht_array;
22268 }
22269 if (done < func_hashtab.ht_used)
22270 {
22271 if (done++ > 0)
22272 ++hi;
22273 while (HASHITEM_EMPTY(hi))
22274 ++hi;
22275 fp = HI2UF(hi);
22276
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022277 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022278 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022279
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022280 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22281 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282
22283 cat_func_name(IObuff, fp);
22284 if (xp->xp_context != EXPAND_USER_FUNC)
22285 {
22286 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022287 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 STRCAT(IObuff, ")");
22289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 return IObuff;
22291 }
22292 return NULL;
22293}
22294
22295#endif /* FEAT_CMDL_COMPL */
22296
22297/*
22298 * Copy the function name of "fp" to buffer "buf".
22299 * "buf" must be able to hold the function name plus three bytes.
22300 * Takes care of script-local function names.
22301 */
22302 static void
22303cat_func_name(buf, fp)
22304 char_u *buf;
22305 ufunc_T *fp;
22306{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022307 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 {
22309 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022310 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311 }
22312 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022313 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314}
22315
22316/*
22317 * ":delfunction {name}"
22318 */
22319 void
22320ex_delfunction(eap)
22321 exarg_T *eap;
22322{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022323 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 char_u *p;
22325 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022326 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327
22328 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022329 name = trans_function_name(&p, eap->skip, 0, &fudi);
22330 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022332 {
22333 if (fudi.fd_dict != NULL && !eap->skip)
22334 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 if (!ends_excmd(*skipwhite(p)))
22338 {
22339 vim_free(name);
22340 EMSG(_(e_trailing));
22341 return;
22342 }
22343 eap->nextcmd = check_nextcmd(p);
22344 if (eap->nextcmd != NULL)
22345 *p = NUL;
22346
22347 if (!eap->skip)
22348 fp = find_func(name);
22349 vim_free(name);
22350
22351 if (!eap->skip)
22352 {
22353 if (fp == NULL)
22354 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022355 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 return;
22357 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022358 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359 {
22360 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22361 return;
22362 }
22363
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022364 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022366 /* Delete the dict item that refers to the function, it will
22367 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022368 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022370 else
22371 func_free(fp);
22372 }
22373}
22374
22375/*
22376 * Free a function and remove it from the list of functions.
22377 */
22378 static void
22379func_free(fp)
22380 ufunc_T *fp;
22381{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022382 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022383
22384 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022385 ga_clear_strings(&(fp->uf_args));
22386 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022387#ifdef FEAT_PROFILE
22388 vim_free(fp->uf_tml_count);
22389 vim_free(fp->uf_tml_total);
22390 vim_free(fp->uf_tml_self);
22391#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022392
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022393 /* remove the function from the function hashtable */
22394 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22395 if (HASHITEM_EMPTY(hi))
22396 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022397 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022398 hash_remove(&func_hashtab, hi);
22399
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022400 vim_free(fp);
22401}
22402
22403/*
22404 * Unreference a Function: decrement the reference count and free it when it
22405 * becomes zero. Only for numbered functions.
22406 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022407 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022408func_unref(name)
22409 char_u *name;
22410{
22411 ufunc_T *fp;
22412
22413 if (name != NULL && isdigit(*name))
22414 {
22415 fp = find_func(name);
22416 if (fp == NULL)
22417 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022418 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022419 {
22420 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022421 * when "uf_calls" becomes zero. */
22422 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022423 func_free(fp);
22424 }
22425 }
22426}
22427
22428/*
22429 * Count a reference to a Function.
22430 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022431 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022432func_ref(name)
22433 char_u *name;
22434{
22435 ufunc_T *fp;
22436
22437 if (name != NULL && isdigit(*name))
22438 {
22439 fp = find_func(name);
22440 if (fp == NULL)
22441 EMSG2(_(e_intern2), "func_ref()");
22442 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022443 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 }
22445}
22446
22447/*
22448 * Call a user function.
22449 */
22450 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022451call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 ufunc_T *fp; /* pointer to function */
22453 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022454 typval_T *argvars; /* arguments */
22455 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 linenr_T firstline; /* first line of range */
22457 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459{
Bram Moolenaar33570922005-01-25 22:26:29 +000022460 char_u *save_sourcing_name;
22461 linenr_T save_sourcing_lnum;
22462 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022463 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022464 int save_did_emsg;
22465 static int depth = 0;
22466 dictitem_T *v;
22467 int fixvar_idx = 0; /* index in fixvar[] */
22468 int i;
22469 int ai;
22470 char_u numbuf[NUMBUFLEN];
22471 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022472#ifdef FEAT_PROFILE
22473 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022474 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476
22477 /* If depth of calling is getting too high, don't execute the function */
22478 if (depth >= p_mfd)
22479 {
22480 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022481 rettv->v_type = VAR_NUMBER;
22482 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483 return;
22484 }
22485 ++depth;
22486
22487 line_breakcheck(); /* check for CTRL-C hit */
22488
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022489 fc = (funccall_T *)alloc(sizeof(funccall_T));
22490 fc->caller = current_funccal;
22491 current_funccal = fc;
22492 fc->func = fp;
22493 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022494 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022495 fc->linenr = 0;
22496 fc->returned = FALSE;
22497 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022499 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22500 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501
Bram Moolenaar33570922005-01-25 22:26:29 +000022502 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022503 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022504 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22505 * each argument variable and saves a lot of time.
22506 */
22507 /*
22508 * Init l: variables.
22509 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022510 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022511 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022512 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022513 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22514 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022515 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022516 name = v->di_key;
22517 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022518 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022519 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022520 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022521 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022522 v->di_tv.vval.v_dict = selfdict;
22523 ++selfdict->dv_refcount;
22524 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022525
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 /*
22527 * Init a: variables.
22528 * Set a:0 to "argcount".
22529 * Set a:000 to a list with room for the "..." arguments.
22530 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022531 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022532 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022533 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022534 /* Use "name" to avoid a warning from some compiler that checks the
22535 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022536 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022537 name = v->di_key;
22538 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022540 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022541 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022542 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022543 v->di_tv.vval.v_list = &fc->l_varlist;
22544 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22545 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22546 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022547
22548 /*
22549 * Set a:firstline to "firstline" and a:lastline to "lastline".
22550 * Set a:name to named arguments.
22551 * Set a:N to the "..." arguments.
22552 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022553 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022554 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022555 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022556 (varnumber_T)lastline);
22557 for (i = 0; i < argcount; ++i)
22558 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022559 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 if (ai < 0)
22561 /* named argument a:name */
22562 name = FUNCARG(fp, i);
22563 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022564 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022565 /* "..." argument a:1, a:2, etc. */
22566 sprintf((char *)numbuf, "%d", ai + 1);
22567 name = numbuf;
22568 }
22569 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22570 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022571 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022572 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22573 }
22574 else
22575 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022576 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22577 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 if (v == NULL)
22579 break;
22580 v->di_flags = DI_FLAGS_RO;
22581 }
22582 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022583 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022584
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022585 /* Note: the values are copied directly to avoid alloc/free.
22586 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022587 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022588 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022589
22590 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22591 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022592 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22593 fc->l_listitems[ai].li_tv = argvars[i];
22594 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022595 }
22596 }
22597
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 /* Don't redraw while executing the function. */
22599 ++RedrawingDisabled;
22600 save_sourcing_name = sourcing_name;
22601 save_sourcing_lnum = sourcing_lnum;
22602 sourcing_lnum = 1;
22603 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022604 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 if (sourcing_name != NULL)
22606 {
22607 if (save_sourcing_name != NULL
22608 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22609 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22610 else
22611 STRCPY(sourcing_name, "function ");
22612 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22613
22614 if (p_verbose >= 12)
22615 {
22616 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022617 verbose_enter_scroll();
22618
Bram Moolenaar555b2802005-05-19 21:08:39 +000022619 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 if (p_verbose >= 14)
22621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022623 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022624 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022625 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626
22627 msg_puts((char_u *)"(");
22628 for (i = 0; i < argcount; ++i)
22629 {
22630 if (i > 0)
22631 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022632 if (argvars[i].v_type == VAR_NUMBER)
22633 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 else
22635 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022636 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22637 if (s != NULL)
22638 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022639 if (vim_strsize(s) > MSG_BUF_CLEN)
22640 {
22641 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22642 s = buf;
22643 }
22644 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022645 vim_free(tofree);
22646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 }
22648 }
22649 msg_puts((char_u *)")");
22650 }
22651 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022652
22653 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 --no_wait_return;
22655 }
22656 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022657#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022658 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022659 {
22660 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22661 func_do_profile(fp);
22662 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022663 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022664 {
22665 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022666 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022667 profile_zero(&fp->uf_tm_children);
22668 }
22669 script_prof_save(&wait_start);
22670 }
22671#endif
22672
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022674 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 save_did_emsg = did_emsg;
22676 did_emsg = FALSE;
22677
22678 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022679 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22681
22682 --RedrawingDisabled;
22683
22684 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022685 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022687 clear_tv(rettv);
22688 rettv->v_type = VAR_NUMBER;
22689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 }
22691
Bram Moolenaar05159a02005-02-26 23:04:13 +000022692#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022693 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022694 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022695 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022696 profile_end(&call_start);
22697 profile_sub_wait(&wait_start, &call_start);
22698 profile_add(&fp->uf_tm_total, &call_start);
22699 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022700 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022701 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022702 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22703 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022704 }
22705 }
22706#endif
22707
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 /* when being verbose, mention the return value */
22709 if (p_verbose >= 12)
22710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022712 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022715 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022716 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022717 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022718 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022719 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022721 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022722 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022723 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022724 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022725
Bram Moolenaar555b2802005-05-19 21:08:39 +000022726 /* The value may be very long. Skip the middle part, so that we
22727 * have some idea how it starts and ends. smsg() would always
22728 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022729 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022730 if (s != NULL)
22731 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022732 if (vim_strsize(s) > MSG_BUF_CLEN)
22733 {
22734 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22735 s = buf;
22736 }
22737 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022738 vim_free(tofree);
22739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 }
22741 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022742
22743 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 --no_wait_return;
22745 }
22746
22747 vim_free(sourcing_name);
22748 sourcing_name = save_sourcing_name;
22749 sourcing_lnum = save_sourcing_lnum;
22750 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022751#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022752 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022753 script_prof_restore(&wait_start);
22754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
22756 if (p_verbose >= 12 && sourcing_name != NULL)
22757 {
22758 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022759 verbose_enter_scroll();
22760
Bram Moolenaar555b2802005-05-19 21:08:39 +000022761 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022763
22764 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 --no_wait_return;
22766 }
22767
22768 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022769 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022771
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022772 /* If the a:000 list and the l: and a: dicts are not referenced we can
22773 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022774 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22775 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22776 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22777 {
22778 free_funccal(fc, FALSE);
22779 }
22780 else
22781 {
22782 hashitem_T *hi;
22783 listitem_T *li;
22784 int todo;
22785
22786 /* "fc" is still in use. This can happen when returning "a:000" or
22787 * assigning "l:" to a global variable.
22788 * Link "fc" in the list for garbage collection later. */
22789 fc->caller = previous_funccal;
22790 previous_funccal = fc;
22791
22792 /* Make a copy of the a: variables, since we didn't do that above. */
22793 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22794 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22795 {
22796 if (!HASHITEM_EMPTY(hi))
22797 {
22798 --todo;
22799 v = HI2DI(hi);
22800 copy_tv(&v->di_tv, &v->di_tv);
22801 }
22802 }
22803
22804 /* Make a copy of the a:000 items, since we didn't do that above. */
22805 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22806 copy_tv(&li->li_tv, &li->li_tv);
22807 }
22808}
22809
22810/*
22811 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022812 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022813 */
22814 static int
22815can_free_funccal(fc, copyID)
22816 funccall_T *fc;
22817 int copyID;
22818{
22819 return (fc->l_varlist.lv_copyID != copyID
22820 && fc->l_vars.dv_copyID != copyID
22821 && fc->l_avars.dv_copyID != copyID);
22822}
22823
22824/*
22825 * Free "fc" and what it contains.
22826 */
22827 static void
22828free_funccal(fc, free_val)
22829 funccall_T *fc;
22830 int free_val; /* a: vars were allocated */
22831{
22832 listitem_T *li;
22833
22834 /* The a: variables typevals may not have been allocated, only free the
22835 * allocated variables. */
22836 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22837
22838 /* free all l: variables */
22839 vars_clear(&fc->l_vars.dv_hashtab);
22840
22841 /* Free the a:000 variables if they were allocated. */
22842 if (free_val)
22843 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22844 clear_tv(&li->li_tv);
22845
22846 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847}
22848
22849/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022850 * Add a number variable "name" to dict "dp" with value "nr".
22851 */
22852 static void
22853add_nr_var(dp, v, name, nr)
22854 dict_T *dp;
22855 dictitem_T *v;
22856 char *name;
22857 varnumber_T nr;
22858{
22859 STRCPY(v->di_key, name);
22860 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22861 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22862 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022863 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022864 v->di_tv.vval.v_number = nr;
22865}
22866
22867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 * ":return [expr]"
22869 */
22870 void
22871ex_return(eap)
22872 exarg_T *eap;
22873{
22874 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022875 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 int returning = FALSE;
22877
22878 if (current_funccal == NULL)
22879 {
22880 EMSG(_("E133: :return not inside a function"));
22881 return;
22882 }
22883
22884 if (eap->skip)
22885 ++emsg_skip;
22886
22887 eap->nextcmd = NULL;
22888 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022889 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 {
22891 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022892 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 }
22896 /* It's safer to return also on error. */
22897 else if (!eap->skip)
22898 {
22899 /*
22900 * Return unless the expression evaluation has been cancelled due to an
22901 * aborting error, an interrupt, or an exception.
22902 */
22903 if (!aborting())
22904 returning = do_return(eap, FALSE, TRUE, NULL);
22905 }
22906
22907 /* When skipping or the return gets pending, advance to the next command
22908 * in this line (!returning). Otherwise, ignore the rest of the line.
22909 * Following lines will be ignored by get_func_line(). */
22910 if (returning)
22911 eap->nextcmd = NULL;
22912 else if (eap->nextcmd == NULL) /* no argument */
22913 eap->nextcmd = check_nextcmd(arg);
22914
22915 if (eap->skip)
22916 --emsg_skip;
22917}
22918
22919/*
22920 * Return from a function. Possibly makes the return pending. Also called
22921 * for a pending return at the ":endtry" or after returning from an extra
22922 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022923 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022924 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 * FALSE when the return gets pending.
22926 */
22927 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022928do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 exarg_T *eap;
22930 int reanimate;
22931 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022932 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933{
22934 int idx;
22935 struct condstack *cstack = eap->cstack;
22936
22937 if (reanimate)
22938 /* Undo the return. */
22939 current_funccal->returned = FALSE;
22940
22941 /*
22942 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22943 * not in its finally clause (which then is to be executed next) is found.
22944 * In this case, make the ":return" pending for execution at the ":endtry".
22945 * Otherwise, return normally.
22946 */
22947 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22948 if (idx >= 0)
22949 {
22950 cstack->cs_pending[idx] = CSTP_RETURN;
22951
22952 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022953 /* A pending return again gets pending. "rettv" points to an
22954 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022956 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 else
22958 {
22959 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022960 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022962 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022964 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 {
22966 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022967 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022968 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 else
22970 EMSG(_(e_outofmem));
22971 }
22972 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022973 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974
22975 if (reanimate)
22976 {
22977 /* The pending return value could be overwritten by a ":return"
22978 * without argument in a finally clause; reset the default
22979 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022980 current_funccal->rettv->v_type = VAR_NUMBER;
22981 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 }
22983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022984 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 }
22986 else
22987 {
22988 current_funccal->returned = TRUE;
22989
22990 /* If the return is carried out now, store the return value. For
22991 * a return immediately after reanimation, the value is already
22992 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022993 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022995 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022996 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022998 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 }
23000 }
23001
23002 return idx < 0;
23003}
23004
23005/*
23006 * Free the variable with a pending return value.
23007 */
23008 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023009discard_pending_return(rettv)
23010 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011{
Bram Moolenaar33570922005-01-25 22:26:29 +000023012 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013}
23014
23015/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023016 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 * is an allocated string. Used by report_pending() for verbose messages.
23018 */
23019 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023020get_return_cmd(rettv)
23021 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023023 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023024 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023025 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023027 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023028 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023029 if (s == NULL)
23030 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023031
23032 STRCPY(IObuff, ":return ");
23033 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23034 if (STRLEN(s) + 8 >= IOSIZE)
23035 STRCPY(IObuff + IOSIZE - 4, "...");
23036 vim_free(tofree);
23037 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038}
23039
23040/*
23041 * Get next function line.
23042 * Called by do_cmdline() to get the next line.
23043 * Returns allocated string, or NULL for end of function.
23044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 char_u *
23046get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023047 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023049 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050{
Bram Moolenaar33570922005-01-25 22:26:29 +000023051 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023052 ufunc_T *fp = fcp->func;
23053 char_u *retval;
23054 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055
23056 /* If breakpoints have been added/deleted need to check for it. */
23057 if (fcp->dbg_tick != debug_tick)
23058 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023059 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 sourcing_lnum);
23061 fcp->dbg_tick = debug_tick;
23062 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023063#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023064 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023065 func_line_end(cookie);
23066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067
Bram Moolenaar05159a02005-02-26 23:04:13 +000023068 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023069 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23070 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 retval = NULL;
23072 else
23073 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023074 /* Skip NULL lines (continuation lines). */
23075 while (fcp->linenr < gap->ga_len
23076 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23077 ++fcp->linenr;
23078 if (fcp->linenr >= gap->ga_len)
23079 retval = NULL;
23080 else
23081 {
23082 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23083 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023084#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023085 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023086 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023087#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 }
23090
23091 /* Did we encounter a breakpoint? */
23092 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23093 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023094 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023096 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 sourcing_lnum);
23098 fcp->dbg_tick = debug_tick;
23099 }
23100
23101 return retval;
23102}
23103
Bram Moolenaar05159a02005-02-26 23:04:13 +000023104#if defined(FEAT_PROFILE) || defined(PROTO)
23105/*
23106 * Called when starting to read a function line.
23107 * "sourcing_lnum" must be correct!
23108 * When skipping lines it may not actually be executed, but we won't find out
23109 * until later and we need to store the time now.
23110 */
23111 void
23112func_line_start(cookie)
23113 void *cookie;
23114{
23115 funccall_T *fcp = (funccall_T *)cookie;
23116 ufunc_T *fp = fcp->func;
23117
23118 if (fp->uf_profiling && sourcing_lnum >= 1
23119 && sourcing_lnum <= fp->uf_lines.ga_len)
23120 {
23121 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023122 /* Skip continuation lines. */
23123 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23124 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023125 fp->uf_tml_execed = FALSE;
23126 profile_start(&fp->uf_tml_start);
23127 profile_zero(&fp->uf_tml_children);
23128 profile_get_wait(&fp->uf_tml_wait);
23129 }
23130}
23131
23132/*
23133 * Called when actually executing a function line.
23134 */
23135 void
23136func_line_exec(cookie)
23137 void *cookie;
23138{
23139 funccall_T *fcp = (funccall_T *)cookie;
23140 ufunc_T *fp = fcp->func;
23141
23142 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23143 fp->uf_tml_execed = TRUE;
23144}
23145
23146/*
23147 * Called when done with a function line.
23148 */
23149 void
23150func_line_end(cookie)
23151 void *cookie;
23152{
23153 funccall_T *fcp = (funccall_T *)cookie;
23154 ufunc_T *fp = fcp->func;
23155
23156 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23157 {
23158 if (fp->uf_tml_execed)
23159 {
23160 ++fp->uf_tml_count[fp->uf_tml_idx];
23161 profile_end(&fp->uf_tml_start);
23162 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023163 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023164 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23165 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023166 }
23167 fp->uf_tml_idx = -1;
23168 }
23169}
23170#endif
23171
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172/*
23173 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023174 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 */
23176 int
23177func_has_ended(cookie)
23178 void *cookie;
23179{
Bram Moolenaar33570922005-01-25 22:26:29 +000023180 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181
23182 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23183 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023184 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 || fcp->returned);
23186}
23187
23188/*
23189 * return TRUE if cookie indicates a function which "abort"s on errors.
23190 */
23191 int
23192func_has_abort(cookie)
23193 void *cookie;
23194{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023195 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196}
23197
23198#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23199typedef enum
23200{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023201 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23202 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23203 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204} var_flavour_T;
23205
23206static var_flavour_T var_flavour __ARGS((char_u *varname));
23207
23208 static var_flavour_T
23209var_flavour(varname)
23210 char_u *varname;
23211{
23212 char_u *p = varname;
23213
23214 if (ASCII_ISUPPER(*p))
23215 {
23216 while (*(++p))
23217 if (ASCII_ISLOWER(*p))
23218 return VAR_FLAVOUR_SESSION;
23219 return VAR_FLAVOUR_VIMINFO;
23220 }
23221 else
23222 return VAR_FLAVOUR_DEFAULT;
23223}
23224#endif
23225
23226#if defined(FEAT_VIMINFO) || defined(PROTO)
23227/*
23228 * Restore global vars that start with a capital from the viminfo file
23229 */
23230 int
23231read_viminfo_varlist(virp, writing)
23232 vir_T *virp;
23233 int writing;
23234{
23235 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023236 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023237 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238
23239 if (!writing && (find_viminfo_parameter('!') != NULL))
23240 {
23241 tab = vim_strchr(virp->vir_line + 1, '\t');
23242 if (tab != NULL)
23243 {
23244 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023245 switch (*tab)
23246 {
23247 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023248#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023249 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023250#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023251 case 'D': type = VAR_DICT; break;
23252 case 'L': type = VAR_LIST; break;
23253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254
23255 tab = vim_strchr(tab, '\t');
23256 if (tab != NULL)
23257 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023258 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023259 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023260 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023262#ifdef FEAT_FLOAT
23263 else if (type == VAR_FLOAT)
23264 (void)string2float(tab + 1, &tv.vval.v_float);
23265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023267 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023268 if (type == VAR_DICT || type == VAR_LIST)
23269 {
23270 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23271
23272 if (etv == NULL)
23273 /* Failed to parse back the dict or list, use it as a
23274 * string. */
23275 tv.v_type = VAR_STRING;
23276 else
23277 {
23278 vim_free(tv.vval.v_string);
23279 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023280 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023281 }
23282 }
23283
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023284 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023285
23286 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023287 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023288 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23289 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 }
23291 }
23292 }
23293
23294 return viminfo_readline(virp);
23295}
23296
23297/*
23298 * Write global vars that start with a capital to the viminfo file
23299 */
23300 void
23301write_viminfo_varlist(fp)
23302 FILE *fp;
23303{
Bram Moolenaar33570922005-01-25 22:26:29 +000023304 hashitem_T *hi;
23305 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023306 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023307 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023308 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023309 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023310 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311
23312 if (find_viminfo_parameter('!') == NULL)
23313 return;
23314
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023315 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023316
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023317 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023318 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023320 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023322 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023323 this_var = HI2DI(hi);
23324 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023325 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023326 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023327 {
23328 case VAR_STRING: s = "STR"; break;
23329 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023330#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023331 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023332#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023333 case VAR_DICT: s = "DIC"; break;
23334 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023335 default: continue;
23336 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023338 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023339 if (p != NULL)
23340 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023341 vim_free(tofree);
23342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343 }
23344 }
23345}
23346#endif
23347
23348#if defined(FEAT_SESSION) || defined(PROTO)
23349 int
23350store_session_globals(fd)
23351 FILE *fd;
23352{
Bram Moolenaar33570922005-01-25 22:26:29 +000023353 hashitem_T *hi;
23354 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023355 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 char_u *p, *t;
23357
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023358 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023359 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023361 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023363 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023364 this_var = HI2DI(hi);
23365 if ((this_var->di_tv.v_type == VAR_NUMBER
23366 || this_var->di_tv.v_type == VAR_STRING)
23367 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023368 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023369 /* Escape special characters with a backslash. Turn a LF and
23370 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023371 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023372 (char_u *)"\\\"\n\r");
23373 if (p == NULL) /* out of memory */
23374 break;
23375 for (t = p; *t != NUL; ++t)
23376 if (*t == '\n')
23377 *t = 'n';
23378 else if (*t == '\r')
23379 *t = 'r';
23380 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 this_var->di_key,
23382 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23383 : ' ',
23384 p,
23385 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23386 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023387 || put_eol(fd) == FAIL)
23388 {
23389 vim_free(p);
23390 return FAIL;
23391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 vim_free(p);
23393 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023394#ifdef FEAT_FLOAT
23395 else if (this_var->di_tv.v_type == VAR_FLOAT
23396 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23397 {
23398 float_T f = this_var->di_tv.vval.v_float;
23399 int sign = ' ';
23400
23401 if (f < 0)
23402 {
23403 f = -f;
23404 sign = '-';
23405 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023406 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023407 this_var->di_key, sign, f) < 0)
23408 || put_eol(fd) == FAIL)
23409 return FAIL;
23410 }
23411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412 }
23413 }
23414 return OK;
23415}
23416#endif
23417
Bram Moolenaar661b1822005-07-28 22:36:45 +000023418/*
23419 * Display script name where an item was last set.
23420 * Should only be invoked when 'verbose' is non-zero.
23421 */
23422 void
23423last_set_msg(scriptID)
23424 scid_T scriptID;
23425{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023426 char_u *p;
23427
Bram Moolenaar661b1822005-07-28 22:36:45 +000023428 if (scriptID != 0)
23429 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023430 p = home_replace_save(NULL, get_scriptname(scriptID));
23431 if (p != NULL)
23432 {
23433 verbose_enter();
23434 MSG_PUTS(_("\n\tLast set from "));
23435 MSG_PUTS(p);
23436 vim_free(p);
23437 verbose_leave();
23438 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023439 }
23440}
23441
Bram Moolenaard812df62008-11-09 12:46:09 +000023442/*
23443 * List v:oldfiles in a nice way.
23444 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023445 void
23446ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023447 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023448{
23449 list_T *l = vimvars[VV_OLDFILES].vv_list;
23450 listitem_T *li;
23451 int nr = 0;
23452
23453 if (l == NULL)
23454 msg((char_u *)_("No old files"));
23455 else
23456 {
23457 msg_start();
23458 msg_scroll = TRUE;
23459 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23460 {
23461 msg_outnum((long)++nr);
23462 MSG_PUTS(": ");
23463 msg_outtrans(get_tv_string(&li->li_tv));
23464 msg_putchar('\n');
23465 out_flush(); /* output one line at a time */
23466 ui_breakcheck();
23467 }
23468 /* Assume "got_int" was set to truncate the listing. */
23469 got_int = FALSE;
23470
23471#ifdef FEAT_BROWSE_CMD
23472 if (cmdmod.browse)
23473 {
23474 quit_more = FALSE;
23475 nr = prompt_for_number(FALSE);
23476 msg_starthere();
23477 if (nr > 0)
23478 {
23479 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23480 (long)nr);
23481
23482 if (p != NULL)
23483 {
23484 p = expand_env_save(p);
23485 eap->arg = p;
23486 eap->cmdidx = CMD_edit;
23487 cmdmod.browse = FALSE;
23488 do_exedit(eap, NULL);
23489 vim_free(p);
23490 }
23491 }
23492 }
23493#endif
23494 }
23495}
23496
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497#endif /* FEAT_EVAL */
23498
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023500#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501
23502#ifdef WIN3264
23503/*
23504 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23505 */
23506static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23507static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23508static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23509
23510/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023511 * Get the short path (8.3) for the filename in "fnamep".
23512 * Only works for a valid file name.
23513 * When the path gets longer "fnamep" is changed and the allocated buffer
23514 * is put in "bufp".
23515 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23516 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 */
23518 static int
23519get_short_pathname(fnamep, bufp, fnamelen)
23520 char_u **fnamep;
23521 char_u **bufp;
23522 int *fnamelen;
23523{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023524 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 char_u *newbuf;
23526
23527 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 l = GetShortPathName(*fnamep, *fnamep, len);
23529 if (l > len - 1)
23530 {
23531 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023532 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 newbuf = vim_strnsave(*fnamep, l);
23534 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023535 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536
23537 vim_free(*bufp);
23538 *fnamep = *bufp = newbuf;
23539
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023540 /* Really should always succeed, as the buffer is big enough. */
23541 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 }
23543
23544 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023545 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546}
23547
23548/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023549 * Get the short path (8.3) for the filename in "fname". The converted
23550 * path is returned in "bufp".
23551 *
23552 * Some of the directories specified in "fname" may not exist. This function
23553 * will shorten the existing directories at the beginning of the path and then
23554 * append the remaining non-existing path.
23555 *
23556 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023557 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023558 * bufp - Pointer to an allocated buffer for the filename.
23559 * fnamelen - Length of the filename pointed to by fname
23560 *
23561 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 */
23563 static int
23564shortpath_for_invalid_fname(fname, bufp, fnamelen)
23565 char_u **fname;
23566 char_u **bufp;
23567 int *fnamelen;
23568{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023569 char_u *short_fname, *save_fname, *pbuf_unused;
23570 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023572 int old_len, len;
23573 int new_len, sfx_len;
23574 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575
23576 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023577 old_len = *fnamelen;
23578 save_fname = vim_strnsave(*fname, old_len);
23579 pbuf_unused = NULL;
23580 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023582 endp = save_fname + old_len - 1; /* Find the end of the copy */
23583 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023585 /*
23586 * Try shortening the supplied path till it succeeds by removing one
23587 * directory at a time from the tail of the path.
23588 */
23589 len = 0;
23590 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023592 /* go back one path-separator */
23593 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23594 --endp;
23595 if (endp <= save_fname)
23596 break; /* processed the complete path */
23597
23598 /*
23599 * Replace the path separator with a NUL and try to shorten the
23600 * resulting path.
23601 */
23602 ch = *endp;
23603 *endp = 0;
23604 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023605 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023606 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23607 {
23608 retval = FAIL;
23609 goto theend;
23610 }
23611 *endp = ch; /* preserve the string */
23612
23613 if (len > 0)
23614 break; /* successfully shortened the path */
23615
23616 /* failed to shorten the path. Skip the path separator */
23617 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 }
23619
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023620 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023622 /*
23623 * Succeeded in shortening the path. Now concatenate the shortened
23624 * path with the remaining path at the tail.
23625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023627 /* Compute the length of the new path. */
23628 sfx_len = (int)(save_endp - endp) + 1;
23629 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023631 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023633 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023635 /* There is not enough space in the currently allocated string,
23636 * copy it to a buffer big enough. */
23637 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023639 {
23640 retval = FAIL;
23641 goto theend;
23642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 }
23644 else
23645 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023646 /* Transfer short_fname to the main buffer (it's big enough),
23647 * unless get_short_pathname() did its work in-place. */
23648 *fname = *bufp = save_fname;
23649 if (short_fname != save_fname)
23650 vim_strncpy(save_fname, short_fname, len);
23651 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023653
23654 /* concat the not-shortened part of the path */
23655 vim_strncpy(*fname + len, endp, sfx_len);
23656 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023658
23659theend:
23660 vim_free(pbuf_unused);
23661 vim_free(save_fname);
23662
23663 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664}
23665
23666/*
23667 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023668 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669 */
23670 static int
23671shortpath_for_partial(fnamep, bufp, fnamelen)
23672 char_u **fnamep;
23673 char_u **bufp;
23674 int *fnamelen;
23675{
23676 int sepcount, len, tflen;
23677 char_u *p;
23678 char_u *pbuf, *tfname;
23679 int hasTilde;
23680
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023681 /* Count up the path separators from the RHS.. so we know which part
23682 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023684 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 if (vim_ispathsep(*p))
23686 ++sepcount;
23687
23688 /* Need full path first (use expand_env() to remove a "~/") */
23689 hasTilde = (**fnamep == '~');
23690 if (hasTilde)
23691 pbuf = tfname = expand_env_save(*fnamep);
23692 else
23693 pbuf = tfname = FullName_save(*fnamep, FALSE);
23694
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023695 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023697 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23698 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699
23700 if (len == 0)
23701 {
23702 /* Don't have a valid filename, so shorten the rest of the
23703 * path if we can. This CAN give us invalid 8.3 filenames, but
23704 * there's not a lot of point in guessing what it might be.
23705 */
23706 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023707 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23708 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 }
23710
23711 /* Count the paths backward to find the beginning of the desired string. */
23712 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023713 {
23714#ifdef FEAT_MBYTE
23715 if (has_mbyte)
23716 p -= mb_head_off(tfname, p);
23717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 if (vim_ispathsep(*p))
23719 {
23720 if (sepcount == 0 || (hasTilde && sepcount == 1))
23721 break;
23722 else
23723 sepcount --;
23724 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 if (hasTilde)
23727 {
23728 --p;
23729 if (p >= tfname)
23730 *p = '~';
23731 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023732 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733 }
23734 else
23735 ++p;
23736
23737 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23738 vim_free(*bufp);
23739 *fnamelen = (int)STRLEN(p);
23740 *bufp = pbuf;
23741 *fnamep = p;
23742
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023743 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023744}
23745#endif /* WIN3264 */
23746
23747/*
23748 * Adjust a filename, according to a string of modifiers.
23749 * *fnamep must be NUL terminated when called. When returning, the length is
23750 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023751 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 * When there is an error, *fnamep is set to NULL.
23753 */
23754 int
23755modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23756 char_u *src; /* string with modifiers */
23757 int *usedlen; /* characters after src that are used */
23758 char_u **fnamep; /* file name so far */
23759 char_u **bufp; /* buffer for allocated file name or NULL */
23760 int *fnamelen; /* length of fnamep */
23761{
23762 int valid = 0;
23763 char_u *tail;
23764 char_u *s, *p, *pbuf;
23765 char_u dirname[MAXPATHL];
23766 int c;
23767 int has_fullname = 0;
23768#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023769 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 int has_shortname = 0;
23771#endif
23772
23773repeat:
23774 /* ":p" - full path/file_name */
23775 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23776 {
23777 has_fullname = 1;
23778
23779 valid |= VALID_PATH;
23780 *usedlen += 2;
23781
23782 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23783 if ((*fnamep)[0] == '~'
23784#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23785 && ((*fnamep)[1] == '/'
23786# ifdef BACKSLASH_IN_FILENAME
23787 || (*fnamep)[1] == '\\'
23788# endif
23789 || (*fnamep)[1] == NUL)
23790
23791#endif
23792 )
23793 {
23794 *fnamep = expand_env_save(*fnamep);
23795 vim_free(*bufp); /* free any allocated file name */
23796 *bufp = *fnamep;
23797 if (*fnamep == NULL)
23798 return -1;
23799 }
23800
23801 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023802 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 {
23804 if (vim_ispathsep(*p)
23805 && p[1] == '.'
23806 && (p[2] == NUL
23807 || vim_ispathsep(p[2])
23808 || (p[2] == '.'
23809 && (p[3] == NUL || vim_ispathsep(p[3])))))
23810 break;
23811 }
23812
23813 /* FullName_save() is slow, don't use it when not needed. */
23814 if (*p != NUL || !vim_isAbsName(*fnamep))
23815 {
23816 *fnamep = FullName_save(*fnamep, *p != NUL);
23817 vim_free(*bufp); /* free any allocated file name */
23818 *bufp = *fnamep;
23819 if (*fnamep == NULL)
23820 return -1;
23821 }
23822
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023823#ifdef WIN3264
23824# if _WIN32_WINNT >= 0x0500
23825 if (vim_strchr(*fnamep, '~') != NULL)
23826 {
23827 /* Expand 8.3 filename to full path. Needed to make sure the same
23828 * file does not have two different names.
23829 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23830 p = alloc(_MAX_PATH + 1);
23831 if (p != NULL)
23832 {
23833 if (GetLongPathName(*fnamep, p, MAXPATHL))
23834 {
23835 vim_free(*bufp);
23836 *bufp = *fnamep = p;
23837 }
23838 else
23839 vim_free(p);
23840 }
23841 }
23842# endif
23843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 /* Append a path separator to a directory. */
23845 if (mch_isdir(*fnamep))
23846 {
23847 /* Make room for one or two extra characters. */
23848 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23849 vim_free(*bufp); /* free any allocated file name */
23850 *bufp = *fnamep;
23851 if (*fnamep == NULL)
23852 return -1;
23853 add_pathsep(*fnamep);
23854 }
23855 }
23856
23857 /* ":." - path relative to the current directory */
23858 /* ":~" - path relative to the home directory */
23859 /* ":8" - shortname path - postponed till after */
23860 while (src[*usedlen] == ':'
23861 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23862 {
23863 *usedlen += 2;
23864 if (c == '8')
23865 {
23866#ifdef WIN3264
23867 has_shortname = 1; /* Postpone this. */
23868#endif
23869 continue;
23870 }
23871 pbuf = NULL;
23872 /* Need full path first (use expand_env() to remove a "~/") */
23873 if (!has_fullname)
23874 {
23875 if (c == '.' && **fnamep == '~')
23876 p = pbuf = expand_env_save(*fnamep);
23877 else
23878 p = pbuf = FullName_save(*fnamep, FALSE);
23879 }
23880 else
23881 p = *fnamep;
23882
23883 has_fullname = 0;
23884
23885 if (p != NULL)
23886 {
23887 if (c == '.')
23888 {
23889 mch_dirname(dirname, MAXPATHL);
23890 s = shorten_fname(p, dirname);
23891 if (s != NULL)
23892 {
23893 *fnamep = s;
23894 if (pbuf != NULL)
23895 {
23896 vim_free(*bufp); /* free any allocated file name */
23897 *bufp = pbuf;
23898 pbuf = NULL;
23899 }
23900 }
23901 }
23902 else
23903 {
23904 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23905 /* Only replace it when it starts with '~' */
23906 if (*dirname == '~')
23907 {
23908 s = vim_strsave(dirname);
23909 if (s != NULL)
23910 {
23911 *fnamep = s;
23912 vim_free(*bufp);
23913 *bufp = s;
23914 }
23915 }
23916 }
23917 vim_free(pbuf);
23918 }
23919 }
23920
23921 tail = gettail(*fnamep);
23922 *fnamelen = (int)STRLEN(*fnamep);
23923
23924 /* ":h" - head, remove "/file_name", can be repeated */
23925 /* Don't remove the first "/" or "c:\" */
23926 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23927 {
23928 valid |= VALID_HEAD;
23929 *usedlen += 2;
23930 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023931 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023932 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 *fnamelen = (int)(tail - *fnamep);
23934#ifdef VMS
23935 if (*fnamelen > 0)
23936 *fnamelen += 1; /* the path separator is part of the path */
23937#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023938 if (*fnamelen == 0)
23939 {
23940 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23941 p = vim_strsave((char_u *)".");
23942 if (p == NULL)
23943 return -1;
23944 vim_free(*bufp);
23945 *bufp = *fnamep = tail = p;
23946 *fnamelen = 1;
23947 }
23948 else
23949 {
23950 while (tail > s && !after_pathsep(s, tail))
23951 mb_ptr_back(*fnamep, tail);
23952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 }
23954
23955 /* ":8" - shortname */
23956 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23957 {
23958 *usedlen += 2;
23959#ifdef WIN3264
23960 has_shortname = 1;
23961#endif
23962 }
23963
23964#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023965 /*
23966 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023967 */
23968 if (has_shortname)
23969 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023970 /* Copy the string if it is shortened by :h and when it wasn't copied
23971 * yet, because we are going to change it in place. Avoids changing
23972 * the buffer name for "%:8". */
23973 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 {
23975 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023976 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 return -1;
23978 vim_free(*bufp);
23979 *bufp = *fnamep = p;
23980 }
23981
23982 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023983 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 if (!has_fullname && !vim_isAbsName(*fnamep))
23985 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023986 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 return -1;
23988 }
23989 else
23990 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023991 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992
Bram Moolenaardc935552011-08-17 15:23:23 +020023993 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023995 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 return -1;
23997
23998 if (l == 0)
23999 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024000 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024002 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 return -1;
24004 }
24005 *fnamelen = l;
24006 }
24007 }
24008#endif /* WIN3264 */
24009
24010 /* ":t" - tail, just the basename */
24011 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24012 {
24013 *usedlen += 2;
24014 *fnamelen -= (int)(tail - *fnamep);
24015 *fnamep = tail;
24016 }
24017
24018 /* ":e" - extension, can be repeated */
24019 /* ":r" - root, without extension, can be repeated */
24020 while (src[*usedlen] == ':'
24021 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24022 {
24023 /* find a '.' in the tail:
24024 * - for second :e: before the current fname
24025 * - otherwise: The last '.'
24026 */
24027 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24028 s = *fnamep - 2;
24029 else
24030 s = *fnamep + *fnamelen - 1;
24031 for ( ; s > tail; --s)
24032 if (s[0] == '.')
24033 break;
24034 if (src[*usedlen + 1] == 'e') /* :e */
24035 {
24036 if (s > tail)
24037 {
24038 *fnamelen += (int)(*fnamep - (s + 1));
24039 *fnamep = s + 1;
24040#ifdef VMS
24041 /* cut version from the extension */
24042 s = *fnamep + *fnamelen - 1;
24043 for ( ; s > *fnamep; --s)
24044 if (s[0] == ';')
24045 break;
24046 if (s > *fnamep)
24047 *fnamelen = s - *fnamep;
24048#endif
24049 }
24050 else if (*fnamep <= tail)
24051 *fnamelen = 0;
24052 }
24053 else /* :r */
24054 {
24055 if (s > tail) /* remove one extension */
24056 *fnamelen = (int)(s - *fnamep);
24057 }
24058 *usedlen += 2;
24059 }
24060
24061 /* ":s?pat?foo?" - substitute */
24062 /* ":gs?pat?foo?" - global substitute */
24063 if (src[*usedlen] == ':'
24064 && (src[*usedlen + 1] == 's'
24065 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24066 {
24067 char_u *str;
24068 char_u *pat;
24069 char_u *sub;
24070 int sep;
24071 char_u *flags;
24072 int didit = FALSE;
24073
24074 flags = (char_u *)"";
24075 s = src + *usedlen + 2;
24076 if (src[*usedlen + 1] == 'g')
24077 {
24078 flags = (char_u *)"g";
24079 ++s;
24080 }
24081
24082 sep = *s++;
24083 if (sep)
24084 {
24085 /* find end of pattern */
24086 p = vim_strchr(s, sep);
24087 if (p != NULL)
24088 {
24089 pat = vim_strnsave(s, (int)(p - s));
24090 if (pat != NULL)
24091 {
24092 s = p + 1;
24093 /* find end of substitution */
24094 p = vim_strchr(s, sep);
24095 if (p != NULL)
24096 {
24097 sub = vim_strnsave(s, (int)(p - s));
24098 str = vim_strnsave(*fnamep, *fnamelen);
24099 if (sub != NULL && str != NULL)
24100 {
24101 *usedlen = (int)(p + 1 - src);
24102 s = do_string_sub(str, pat, sub, flags);
24103 if (s != NULL)
24104 {
24105 *fnamep = s;
24106 *fnamelen = (int)STRLEN(s);
24107 vim_free(*bufp);
24108 *bufp = s;
24109 didit = TRUE;
24110 }
24111 }
24112 vim_free(sub);
24113 vim_free(str);
24114 }
24115 vim_free(pat);
24116 }
24117 }
24118 /* after using ":s", repeat all the modifiers */
24119 if (didit)
24120 goto repeat;
24121 }
24122 }
24123
24124 return valid;
24125}
24126
24127/*
24128 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24129 * "flags" can be "g" to do a global substitute.
24130 * Returns an allocated string, NULL for error.
24131 */
24132 char_u *
24133do_string_sub(str, pat, sub, flags)
24134 char_u *str;
24135 char_u *pat;
24136 char_u *sub;
24137 char_u *flags;
24138{
24139 int sublen;
24140 regmatch_T regmatch;
24141 int i;
24142 int do_all;
24143 char_u *tail;
24144 garray_T ga;
24145 char_u *ret;
24146 char_u *save_cpo;
24147
24148 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24149 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024150 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151
24152 ga_init2(&ga, 1, 200);
24153
24154 do_all = (flags[0] == 'g');
24155
24156 regmatch.rm_ic = p_ic;
24157 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24158 if (regmatch.regprog != NULL)
24159 {
24160 tail = str;
24161 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24162 {
24163 /*
24164 * Get some space for a temporary buffer to do the substitution
24165 * into. It will contain:
24166 * - The text up to where the match is.
24167 * - The substituted text.
24168 * - The text after the match.
24169 */
24170 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24171 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24172 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24173 {
24174 ga_clear(&ga);
24175 break;
24176 }
24177
24178 /* copy the text up to where the match is */
24179 i = (int)(regmatch.startp[0] - tail);
24180 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24181 /* add the substituted text */
24182 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24183 + ga.ga_len + i, TRUE, TRUE, FALSE);
24184 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 /* avoid getting stuck on a match with an empty string */
24186 if (tail == regmatch.endp[0])
24187 {
24188 if (*tail == NUL)
24189 break;
24190 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24191 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 }
24193 else
24194 {
24195 tail = regmatch.endp[0];
24196 if (*tail == NUL)
24197 break;
24198 }
24199 if (!do_all)
24200 break;
24201 }
24202
24203 if (ga.ga_data != NULL)
24204 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24205
24206 vim_free(regmatch.regprog);
24207 }
24208
24209 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24210 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024211 if (p_cpo == empty_option)
24212 p_cpo = save_cpo;
24213 else
24214 /* Darn, evaluating {sub} expression changed the value. */
24215 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216
24217 return ret;
24218}
24219
24220#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */