blob: 6e8c26f4a76afefc3880390ad13ecfa363efa137 [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 listitem_T *listitem_alloc __ARGS((void));
428static void listitem_free __ARGS((listitem_T *item));
429static void listitem_remove __ARGS((list_T *l, listitem_T *item));
430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000435static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000438static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
440static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
441static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *list2string __ARGS((typval_T *tv, int copyID));
445static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000446static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000447static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
448static void set_ref_in_list __ARGS((list_T *l, int copyID));
449static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200450static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000451static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
453static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000454static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000456static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000458static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
459static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
462static int string2float __ARGS((char_u *text, float_T *value));
463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
465static int find_internal_func __ARGS((char_u *name));
466static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
467static 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 +0200468static 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 +0000469static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000470static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472#ifdef FEAT_FLOAT
473static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200474static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
519static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200530#ifdef FEAT_FLOAT
531static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
532#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000535static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000541#ifdef FEAT_FLOAT
542static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200544static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000546static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000555static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000556static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000557static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000563static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000571static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000572static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000573static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000574static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200577static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000578static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000586static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000600static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000606static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000618#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200619static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
621#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000626static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000643static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000644#ifdef FEAT_FLOAT
645static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000648static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000649static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000668static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000670static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000677static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000678static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000679static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000680static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200682static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000683static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000685static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#ifdef FEAT_FLOAT
688static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200689static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000692static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000693static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#ifdef FEAT_FLOAT
697static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
699#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000700static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200701static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702#ifdef HAVE_STRFTIME
703static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
704#endif
705static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200711static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200712static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000718static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200719static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000721static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000722static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000723static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000724static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000725static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000727static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200728#ifdef FEAT_FLOAT
729static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
731#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
736static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200739static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200740static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000750static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000753static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000755static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000756static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757static int get_env_len __ARGS((char_u **arg));
758static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000759static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000760static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
761#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
762#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
763 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000764static 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 +0000765static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000766static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
768static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static typval_T *alloc_tv __ARGS((void));
770static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static void init_tv __ARGS((typval_T *varp));
772static long get_tv_number __ARGS((typval_T *varp));
773static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000774static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static char_u *get_tv_string __ARGS((typval_T *varp));
776static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000777static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000779static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
781static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
782static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000783static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
784static 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 +0000785static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
786static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000787static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200788static int var_check_func_name __ARGS((char_u *name, int new_var));
789static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000790static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000791static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
793static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
794static int eval_fname_script __ARGS((char_u *p));
795static int eval_fname_sid __ARGS((char_u *p));
796static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static ufunc_T *find_func __ARGS((char_u *name));
798static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000799static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800#ifdef FEAT_PROFILE
801static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000802static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
803static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
804static int
805# ifdef __BORLANDC__
806 _RTLENTRYF
807# endif
808 prof_total_cmp __ARGS((const void *s1, const void *s2));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000815static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000816static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void func_free __ARGS((ufunc_T *fp));
819static void func_unref __ARGS((char_u *name));
820static void func_ref __ARGS((char_u *name));
821static 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 +0000822static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
823static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
826static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000827static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000828static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200831
832#ifdef EBCDIC
833static int compare_func_name __ARGS((const void *s1, const void *s2));
834static void sortFunctions __ARGS(());
835#endif
836
837
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000838/* Character used as separated in autoload function/variable names. */
839#define AUTOLOAD_CHAR '#'
840
Bram Moolenaar33570922005-01-25 22:26:29 +0000841/*
842 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 */
844 void
845eval_init()
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 int i;
848 struct vimvar *p;
849
850 init_var_dict(&globvardict, &globvars_var);
851 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200852 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000853 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000854 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000855
856 for (i = 0; i < VV_LEN; ++i)
857 {
858 p = &vimvars[i];
859 STRCPY(p->vv_di.di_key, p->vv_name);
860 if (p->vv_flags & VV_RO)
861 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
862 else if (p->vv_flags & VV_RO_SBX)
863 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
864 else
865 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000866
867 /* add to v: scope dict, unless the value is not always available */
868 if (p->vv_type != VAR_UNKNOWN)
869 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000871 /* add to compat scope dict */
872 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000874 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200875
876#ifdef EBCDIC
877 /*
878 * Sort the function table, to enable binary sort.
879 */
880 sortFunctions();
881#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000882}
883
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000884#if defined(EXITFREE) || defined(PROTO)
885 void
886eval_clear()
887{
888 int i;
889 struct vimvar *p;
890
891 for (i = 0; i < VV_LEN; ++i)
892 {
893 p = &vimvars[i];
894 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000895 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000896 vim_free(p->vv_str);
897 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000898 }
899 else if (p->vv_di.di_tv.v_type == VAR_LIST)
900 {
901 list_unref(p->vv_list);
902 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000903 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000904 }
905 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000906 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907 hash_clear(&compat_hashtab);
908
Bram Moolenaard9fba312005-06-26 22:34:35 +0000909 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200910 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001356 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001358 if (tv.vval.v_list->lv_len > 0)
1359 ga_append(&ga, NL);
1360 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 ga_append(&ga, NUL);
1362 retval = (char_u *)ga.ga_data;
1363 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364#ifdef FEAT_FLOAT
1365 else if (convert && tv.v_type == VAR_FLOAT)
1366 {
1367 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1368 retval = vim_strsave(numbuf);
1369 }
1370#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 else
1372 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375
1376 return retval;
1377}
1378
1379/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 * Call eval_to_string() without using current local variables and using
1381 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 */
1383 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *arg;
1386 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 char_u *retval;
1390 void *save_funccalp;
1391
1392 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 ++sandbox;
1395 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 --sandbox;
1399 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 restore_funccal(save_funccalp);
1401 return retval;
1402}
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * Top level evaluation function, returning a number.
1406 * Evaluates "expr" silently.
1407 * Returns -1 for an error.
1408 */
1409 int
1410eval_to_number(expr)
1411 char_u *expr;
1412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001415 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 ++emsg_off;
1418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = -1;
1421 else
1422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 --emsg_off;
1427
1428 return retval;
1429}
1430
Bram Moolenaara40058a2005-07-11 22:42:07 +00001431/*
1432 * Prepare v: variable "idx" to be used.
1433 * Save the current typeval in "save_tv".
1434 * When not used yet add the variable to the v: hashtable.
1435 */
1436 static void
1437prepare_vimvar(idx, save_tv)
1438 int idx;
1439 typval_T *save_tv;
1440{
1441 *save_tv = vimvars[idx].vv_tv;
1442 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1443 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1444}
1445
1446/*
1447 * Restore v: variable "idx" to typeval "save_tv".
1448 * When no longer defined, remove the variable from the v: hashtable.
1449 */
1450 static void
1451restore_vimvar(idx, save_tv)
1452 int idx;
1453 typval_T *save_tv;
1454{
1455 hashitem_T *hi;
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457 vimvars[idx].vv_tv = *save_tv;
1458 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1459 {
1460 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1461 if (HASHITEM_EMPTY(hi))
1462 EMSG2(_(e_intern2), "restore_vimvar()");
1463 else
1464 hash_remove(&vimvarht, hi);
1465 }
1466}
1467
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001468#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469/*
1470 * Evaluate an expression to a list with suggestions.
1471 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001472 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473 */
1474 list_T *
1475eval_spell_expr(badword, expr)
1476 char_u *badword;
1477 char_u *expr;
1478{
1479 typval_T save_val;
1480 typval_T rettv;
1481 list_T *list = NULL;
1482 char_u *p = skipwhite(expr);
1483
1484 /* Set "v:val" to the bad word. */
1485 prepare_vimvar(VV_VAL, &save_val);
1486 vimvars[VV_VAL].vv_type = VAR_STRING;
1487 vimvars[VV_VAL].vv_str = badword;
1488 if (p_verbose == 0)
1489 ++emsg_off;
1490
1491 if (eval1(&p, &rettv, TRUE) == OK)
1492 {
1493 if (rettv.v_type != VAR_LIST)
1494 clear_tv(&rettv);
1495 else
1496 list = rettv.vval.v_list;
1497 }
1498
1499 if (p_verbose == 0)
1500 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 restore_vimvar(VV_VAL, &save_val);
1502
1503 return list;
1504}
1505
1506/*
1507 * "list" is supposed to contain two items: a word and a number. Return the
1508 * word in "pp" and the number as the return value.
1509 * Return -1 if anything isn't right.
1510 * Used to get the good word and score from the eval_spell_expr() result.
1511 */
1512 int
1513get_spellword(list, pp)
1514 list_T *list;
1515 char_u **pp;
1516{
1517 listitem_T *li;
1518
1519 li = list->lv_first;
1520 if (li == NULL)
1521 return -1;
1522 *pp = get_tv_string(&li->li_tv);
1523
1524 li = li->li_next;
1525 if (li == NULL)
1526 return -1;
1527 return get_tv_number(&li->li_tv);
1528}
1529#endif
1530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 * Top level evaluation function.
1533 * Returns an allocated typval_T with the result.
1534 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535 */
1536 typval_T *
1537eval_expr(arg, nextcmd)
1538 char_u *arg;
1539 char_u **nextcmd;
1540{
1541 typval_T *tv;
1542
1543 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001544 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 {
1546 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 }
1549
1550 return tv;
1551}
1552
1553
Bram Moolenaar4f688582007-07-24 12:34:30 +00001554#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1555 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001557 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001558 * Uses argv[argc] for the function arguments. Only Number and String
1559 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001562 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001563call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 char_u *func;
1565 int argc;
1566 char_u **argv;
1567 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar33570922005-01-25 22:26:29 +00001570 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 long n;
1572 int len;
1573 int i;
1574 int doesrange;
1575 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001578 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 for (i = 0; i < argc; i++)
1583 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001584 /* Pass a NULL or empty argument as an empty string */
1585 if (argv[i] == NULL || *argv[i] == NUL)
1586 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001587 argvars[i].v_type = VAR_STRING;
1588 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 continue;
1590 }
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 /* Recognize a number argument, the others must be strings. */
1593 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1594 if (len != 0 && len == (int)STRLEN(argv[i]))
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_NUMBER;
1597 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_STRING;
1602 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 }
1605
1606 if (safe)
1607 {
1608 save_funccalp = save_funccal();
1609 ++sandbox;
1610 }
1611
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1613 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (safe)
1617 {
1618 --sandbox;
1619 restore_funccal(save_funccalp);
1620 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 vim_free(argvars);
1622
1623 if (ret == FAIL)
1624 clear_tv(rettv);
1625
1626 return ret;
1627}
1628
Bram Moolenaar4f688582007-07-24 12:34:30 +00001629# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001631 * Call vimL function "func" and return the result as a string.
1632 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 * Uses argv[argc] for the function arguments.
1634 */
1635 void *
1636call_func_retstr(func, argc, argv, safe)
1637 char_u *func;
1638 int argc;
1639 char_u **argv;
1640 int safe; /* use the sandbox */
1641{
1642 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001643 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644
1645 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1646 return NULL;
1647
1648 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 return retval;
1651}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001652# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653
Bram Moolenaar4f688582007-07-24 12:34:30 +00001654# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001656 * Call vimL function "func" and return the result as a number.
1657 * Returns -1 when calling the function fails.
1658 * Uses argv[argc] for the function arguments.
1659 */
1660 long
1661call_func_retnr(func, argc, argv, safe)
1662 char_u *func;
1663 int argc;
1664 char_u **argv;
1665 int safe; /* use the sandbox */
1666{
1667 typval_T rettv;
1668 long retval;
1669
1670 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1671 return -1;
1672
1673 retval = get_tv_number_chk(&rettv, NULL);
1674 clear_tv(&rettv);
1675 return retval;
1676}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001678
1679/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001682 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 */
1684 void *
1685call_func_retlist(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
1692
1693 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1694 return NULL;
1695
1696 if (rettv.v_type != VAR_LIST)
1697 {
1698 clear_tv(&rettv);
1699 return NULL;
1700 }
1701
1702 return rettv.vval.v_list;
1703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
1705
Bram Moolenaar4f688582007-07-24 12:34:30 +00001706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707/*
1708 * Save the current function call pointer, and set it to NULL.
1709 * Used when executing autocommands and for ":source".
1710 */
1711 void *
1712save_funccal()
1713{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 current_funccal = NULL;
1717 return (void *)fc;
1718}
1719
1720 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001721restore_funccal(vfc)
1722 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724 funccall_T *fc = (funccall_T *)vfc;
1725
1726 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
1728
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729#if defined(FEAT_PROFILE) || defined(PROTO)
1730/*
1731 * Prepare profiling for entering a child or something else that is not
1732 * counted for the script/function itself.
1733 * Should always be called in pair with prof_child_exit().
1734 */
1735 void
1736prof_child_enter(tm)
1737 proftime_T *tm; /* place to store waittime */
1738{
1739 funccall_T *fc = current_funccal;
1740
1741 if (fc != NULL && fc->func->uf_profiling)
1742 profile_start(&fc->prof_child);
1743 script_prof_save(tm);
1744}
1745
1746/*
1747 * Take care of time spent in a child.
1748 * Should always be called after prof_child_enter().
1749 */
1750 void
1751prof_child_exit(tm)
1752 proftime_T *tm; /* where waittime was stored */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 {
1758 profile_end(&fc->prof_child);
1759 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1760 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1761 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1762 }
1763 script_prof_restore(tm);
1764}
1765#endif
1766
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef FEAT_FOLDING
1769/*
1770 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1771 * it in "*cp". Doesn't give error messages.
1772 */
1773 int
1774eval_foldexpr(arg, cp)
1775 char_u *arg;
1776 int *cp;
1777{
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 int retval;
1780 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001781 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1782 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001785 if (use_sandbox)
1786 ++sandbox;
1787 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = 0;
1791 else
1792 {
1793 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (tv.v_type == VAR_NUMBER)
1795 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001796 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 retval = 0;
1798 else
1799 {
1800 /* If the result is a string, check if there is a non-digit before
1801 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (!VIM_ISDIGIT(*s) && *s != '-')
1804 *cp = *s++;
1805 retval = atol((char *)s);
1806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 --sandbox;
1812 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 return retval;
1815}
1816#endif
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 * ":let" list all variable values
1820 * ":let var1 var2" list variable values
1821 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 * ":let var += expr" assignment command.
1823 * ":let var -= expr" assignment command.
1824 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827 void
1828ex_let(eap)
1829 exarg_T *eap;
1830{
1831 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 int var_count = 0;
1836 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001838 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001839 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 argend = skip_var_list(arg, &var_count, &semicolon);
1842 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001844 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1845 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001849 /*
1850 * ":let" without "=": list variables
1851 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (*arg == '[')
1853 EMSG(_(e_invarg));
1854 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_glob_vars(&first);
1861 list_buf_vars(&first);
1862 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001865#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_script_vars(&first);
1867 list_func_vars(&first);
1868 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 eap->nextcmd = check_nextcmd(arg);
1871 }
1872 else
1873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 op[0] = '=';
1875 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001876 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 {
1878 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1879 op[0] = expr[-1]; /* +=, -= or .= */
1880 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (eap->skip)
1884 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (eap->skip)
1887 {
1888 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 --emsg_skip;
1891 }
1892 else if (i != FAIL)
1893 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 }
1899}
1900
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901/*
1902 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1903 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 * When "nextchars" is not NULL it points to a string with characters that
1905 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1906 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 * Returns OK or FAIL;
1908 */
1909 static int
1910ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1911 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 int copy; /* copy values from "tv", don't move */
1914 int semicolon; /* from skip_var_list() */
1915 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917{
1918 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 listitem_T *item;
1922 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923
1924 if (*arg != '[')
1925 {
1926 /*
1927 * ":let var = expr" or ":for var in list"
1928 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 return FAIL;
1931 return OK;
1932 }
1933
1934 /*
1935 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1936 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001937 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 {
1939 EMSG(_(e_listreq));
1940 return FAIL;
1941 }
1942
1943 i = list_len(l);
1944 if (semicolon == 0 && var_count < i)
1945 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001946 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 return FAIL;
1948 }
1949 if (var_count - semicolon > i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954
1955 item = l->lv_first;
1956 while (*arg != ']')
1957 {
1958 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 item = item->li_next;
1961 if (arg == NULL)
1962 return FAIL;
1963
1964 arg = skipwhite(arg);
1965 if (*arg == ';')
1966 {
1967 /* Put the rest of the list (may be empty) in the var after ';'.
1968 * Create a new list for this. */
1969 l = list_alloc();
1970 if (l == NULL)
1971 return FAIL;
1972 while (item != NULL)
1973 {
1974 list_append_tv(l, &item->li_tv);
1975 item = item->li_next;
1976 }
1977
1978 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001979 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 ltv.vval.v_list = l;
1981 l->lv_refcount = 1;
1982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1984 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 clear_tv(&ltv);
1986 if (arg == NULL)
1987 return FAIL;
1988 break;
1989 }
1990 else if (*arg != ',' && *arg != ']')
1991 {
1992 EMSG2(_(e_intern2), "ex_let_vars()");
1993 return FAIL;
1994 }
1995 }
1996
1997 return OK;
1998}
1999
2000/*
2001 * Skip over assignable variable "var" or list of variables "[var, var]".
2002 * Used for ":let varvar = expr" and ":for varvar in expr".
2003 * For "[var, var]" increment "*var_count" for each variable.
2004 * for "[var, var; var]" set "semicolon".
2005 * Return NULL for an error.
2006 */
2007 static char_u *
2008skip_var_list(arg, var_count, semicolon)
2009 char_u *arg;
2010 int *var_count;
2011 int *semicolon;
2012{
2013 char_u *p, *s;
2014
2015 if (*arg == '[')
2016 {
2017 /* "[var, var]": find the matching ']'. */
2018 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002019 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 {
2021 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2022 s = skip_var_one(p);
2023 if (s == p)
2024 {
2025 EMSG2(_(e_invarg2), p);
2026 return NULL;
2027 }
2028 ++*var_count;
2029
2030 p = skipwhite(s);
2031 if (*p == ']')
2032 break;
2033 else if (*p == ';')
2034 {
2035 if (*semicolon == 1)
2036 {
2037 EMSG(_("Double ; in list of variables"));
2038 return NULL;
2039 }
2040 *semicolon = 1;
2041 }
2042 else if (*p != ',')
2043 {
2044 EMSG2(_(e_invarg2), p);
2045 return NULL;
2046 }
2047 }
2048 return p + 1;
2049 }
2050 else
2051 return skip_var_one(arg);
2052}
2053
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002055 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002056 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 static char_u *
2059skip_var_one(arg)
2060 char_u *arg;
2061{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 if (*arg == '@' && arg[1] != NUL)
2063 return arg + 2;
2064 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2065 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066}
2067
Bram Moolenaara7043832005-01-21 11:56:39 +00002068/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 * List variables for hashtab "ht" with prefix "prefix".
2070 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078{
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashitem_T *hi;
2080 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 int todo;
2082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2085 {
2086 if (!HASHITEM_EMPTY(hi))
2087 {
2088 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 di = HI2DI(hi);
2090 if (empty || di->di_tv.v_type != VAR_STRING
2091 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 }
2094 }
2095}
2096
2097/*
2098 * List global variables.
2099 */
2100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_glob_vars(first)
2102 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105}
2106
2107/*
2108 * List buffer variables.
2109 */
2110 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111list_buf_vars(first)
2112 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002113{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114 char_u numbuf[NUMBUFLEN];
2115
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2117 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118
2119 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2121 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
2124/*
2125 * List window variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_win_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2132 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135#ifdef FEAT_WINDOWS
2136/*
2137 * List tab page variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_tab_vars(first)
2141 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002142{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2144 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145}
2146#endif
2147
Bram Moolenaara7043832005-01-21 11:56:39 +00002148/*
2149 * List Vim variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_vim_vars(first)
2153 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156}
2157
2158/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002159 * List script-local variables, if there is a script.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_script_vars(first)
2163 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164{
2165 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2167 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168}
2169
2170/*
2171 * List function variables, if there is a function.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_func_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_funccal != NULL)
2178 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 * List variables in "arg".
2184 */
2185 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 exarg_T *eap;
2188 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
2191 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 char_u *name_start;
2195 char_u *arg_subsc;
2196 char_u *tofree;
2197 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198
2199 while (!ends_excmd(*arg) && !got_int)
2200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002203 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2205 {
2206 emsg_severe = TRUE;
2207 EMSG(_(e_trailing));
2208 break;
2209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 /* get_name_len() takes care of expanding curly braces */
2214 name_start = name = arg;
2215 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2216 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* This is mainly to keep test 49 working: when expanding
2219 * curly braces fails overrule the exception error message. */
2220 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 emsg_severe = TRUE;
2223 EMSG2(_(e_invarg2), arg);
2224 break;
2225 }
2226 error = TRUE;
2227 }
2228 else
2229 {
2230 if (tofree != NULL)
2231 name = tofree;
2232 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 else
2235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* handle d.key, l[idx], f(expr) */
2237 arg_subsc = arg;
2238 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 'g': list_glob_vars(first); break;
2247 case 'b': list_buf_vars(first); break;
2248 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'v': list_vim_vars(first); break;
2253 case 's': list_script_vars(first); break;
2254 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 default:
2256 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 }
2259 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 {
2261 char_u numbuf[NUMBUFLEN];
2262 char_u *tf;
2263 int c;
2264 char_u *s;
2265
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002266 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 c = *arg;
2268 *arg = NUL;
2269 list_one_var_a((char_u *)"",
2270 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 tv.v_type,
2272 s == NULL ? (char_u *)"" : s,
2273 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 *arg = c;
2275 vim_free(tf);
2276 }
2277 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281
2282 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284
2285 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
2287
2288 return arg;
2289}
2290
2291/*
2292 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2293 * Returns a pointer to the char just after the var name.
2294 * Returns NULL if there is an error.
2295 */
2296 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002299 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002301 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303{
2304 int c1;
2305 char_u *name;
2306 char_u *p;
2307 char_u *arg_end = NULL;
2308 int len;
2309 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311
2312 /*
2313 * ":let $VAR = expr": Set environment variable.
2314 */
2315 if (*arg == '$')
2316 {
2317 /* Find the end of the name. */
2318 ++arg;
2319 name = arg;
2320 len = get_env_len(&arg);
2321 if (len == 0)
2322 EMSG2(_(e_invarg2), name - 1);
2323 else
2324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 if (op != NULL && (*op == '+' || *op == '-'))
2326 EMSG2(_(e_letwrong), op);
2327 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002330 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
2332 c1 = name[len];
2333 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334 p = get_tv_string_chk(tv);
2335 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 {
2337 int mustfree = FALSE;
2338 char_u *s = vim_getenv(name, &mustfree);
2339
2340 if (s != NULL)
2341 {
2342 p = tofree = concat_str(s, p);
2343 if (mustfree)
2344 vim_free(s);
2345 }
2346 }
2347 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 if (STRICMP(name, "HOME") == 0)
2351 init_homedir();
2352 else if (didset_vim && STRICMP(name, "VIM") == 0)
2353 didset_vim = FALSE;
2354 else if (didset_vimruntime
2355 && STRICMP(name, "VIMRUNTIME") == 0)
2356 didset_vimruntime = FALSE;
2357 arg_end = arg;
2358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362 }
2363 }
2364
2365 /*
2366 * ":let &option = expr": Set option value.
2367 * ":let &l:option = expr": Set local option value.
2368 * ":let &g:option = expr": Set global option value.
2369 */
2370 else if (*arg == '&')
2371 {
2372 /* Find the end of the name. */
2373 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 if (p == NULL || (endchars != NULL
2375 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 EMSG(_(e_letunexp));
2377 else
2378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 long n;
2380 int opt_type;
2381 long numval;
2382 char_u *stringval = NULL;
2383 char_u *s;
2384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 c1 = *p;
2386 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387
2388 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 s = get_tv_string_chk(tv); /* != NULL if number or string */
2390 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 opt_type = get_option_value(arg, &numval,
2393 &stringval, opt_flags);
2394 if ((opt_type == 1 && *op == '.')
2395 || (opt_type == 0 && *op != '.'))
2396 EMSG2(_(e_letwrong), op);
2397 else
2398 {
2399 if (opt_type == 1) /* number */
2400 {
2401 if (*op == '+')
2402 n = numval + n;
2403 else
2404 n = numval - n;
2405 }
2406 else if (opt_type == 0 && stringval != NULL) /* string */
2407 {
2408 s = concat_str(stringval, s);
2409 vim_free(stringval);
2410 stringval = s;
2411 }
2412 }
2413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 if (s != NULL)
2415 {
2416 set_option_value(arg, n, s, opt_flags);
2417 arg_end = p;
2418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 }
2422 }
2423
2424 /*
2425 * ":let @r = expr": Set register contents.
2426 */
2427 else if (*arg == '@')
2428 {
2429 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 if (op != NULL && (*op == '+' || *op == '-'))
2431 EMSG2(_(e_letwrong), op);
2432 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002437 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 char_u *s;
2439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 p = get_tv_string_chk(tv);
2441 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002443 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 if (s != NULL)
2445 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002446 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 arg_end = arg + 1;
2454 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
2457 }
2458
2459 /*
2460 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002465 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480
2481 else
2482 EMSG2(_(e_invarg2), arg);
2483
2484 return arg_end;
2485}
2486
2487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2489 */
2490 static int
2491check_changedtick(arg)
2492 char_u *arg;
2493{
2494 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2495 {
2496 EMSG2(_(e_readonlyvar), arg);
2497 return TRUE;
2498 }
2499 return FALSE;
2500}
2501
2502/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * Get an lval: variable, Dict item or List item that can be assigned a value
2504 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2505 * "name.key", "name.key[expr]" etc.
2506 * Indexing only works if "name" is an existing List or Dictionary.
2507 * "name" points to the start of the name.
2508 * If "rettv" is not NULL it points to the value to be assigned.
2509 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2510 * wrong; must end in space or cmd separator.
2511 *
2512 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002513 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 */
2516 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 typval_T *rettv;
2520 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 int unlet;
2522 int skip;
2523 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 char_u *expr_start, *expr_end;
2528 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 dictitem_T *v;
2530 typval_T var1;
2531 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540
2541 if (skip)
2542 {
2543 /* When skipping just find the end of the name. */
2544 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 }
2547
2548 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (expr_start != NULL)
2551 {
2552 /* Don't expand the name when we already know there is an error. */
2553 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2554 && *p != '[' && *p != '.')
2555 {
2556 EMSG(_(e_trailing));
2557 return NULL;
2558 }
2559
2560 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2561 if (lp->ll_exp_name == NULL)
2562 {
2563 /* Report an invalid expression in braces, unless the
2564 * expression evaluation has been cancelled due to an
2565 * aborting error, an interrupt, or an exception. */
2566 if (!aborting() && !quiet)
2567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002568 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 EMSG2(_(e_invarg2), name);
2570 return NULL;
2571 }
2572 }
2573 lp->ll_name = lp->ll_exp_name;
2574 }
2575 else
2576 lp->ll_name = name;
2577
2578 /* Without [idx] or .key we are done. */
2579 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2580 return p;
2581
2582 cc = *p;
2583 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002584 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (v == NULL && !quiet)
2586 EMSG2(_(e_undefvar), lp->ll_name);
2587 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 if (v == NULL)
2589 return NULL;
2590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 /*
2592 * Loop until no more [idx] or .key is following.
2593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2598 && !(lp->ll_tv->v_type == VAR_DICT
2599 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_("E689: Can only index a List or Dictionary"));
2603 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E708: [:] must come last"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 len = -1;
2613 if (*p == '.')
2614 {
2615 key = p + 1;
2616 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2617 ;
2618 if (len == 0)
2619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!quiet)
2621 EMSG(_(e_emptykey));
2622 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 }
2624 p = key + len;
2625 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 else
2627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 if (*p == ':')
2631 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 empty1 = FALSE;
2635 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 if (get_tv_string_chk(&var1) == NULL)
2638 {
2639 /* not a number or string */
2640 clear_tv(&var1);
2641 return NULL;
2642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 }
2644
2645 /* Optionally get the second index [ :expr]. */
2646 if (*p == ':')
2647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 if (!empty1)
2653 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (rettv != NULL && (rettv->v_type != VAR_LIST
2657 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (!empty1)
2662 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = skipwhite(p + 1);
2666 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 else
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 if (!empty1)
2674 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002677 if (get_tv_string_chk(&var2) == NULL)
2678 {
2679 /* not a number or string */
2680 if (!empty1)
2681 clear_tv(&var1);
2682 clear_tv(&var2);
2683 return NULL;
2684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (!quiet)
2694 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (!empty1)
2696 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Skip to past ']'. */
2703 ++p;
2704 }
2705
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
2708 if (len == -1)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (*key == NUL)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 lp->ll_list = NULL;
2721 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002722 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002723
2724 /* When assigning to g: check that a function and variable name is
2725 * valid. */
2726 if (rettv != NULL && lp->ll_dict == &globvardict)
2727 {
2728 if (rettv->v_type == VAR_FUNC
2729 && var_check_func_name(key, lp->ll_di == NULL))
2730 return NULL;
2731 if (!valid_varname(key))
2732 return NULL;
2733 }
2734
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002737 /* Can't add "v:" variable. */
2738 if (lp->ll_dict == &vimvardict)
2739 {
2740 EMSG2(_(e_illvar), name);
2741 return NULL;
2742 }
2743
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002744 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002748 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (len == -1)
2750 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
2753 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (len == -1)
2758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 p = NULL;
2761 break;
2762 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 /* existing variable, need to check if it can be changed */
2764 else if (var_check_ro(lp->ll_di->di_flags, name))
2765 return NULL;
2766
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (len == -1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771 else
2772 {
2773 /*
2774 * Get the number and item for the only or first index of the List.
2775 */
2776 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 else
2779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var1);
2782 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002783 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_list = lp->ll_tv->vval.v_list;
2785 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2786 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002788 if (lp->ll_n1 < 0)
2789 {
2790 lp->ll_n1 = 0;
2791 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2792 }
2793 }
2794 if (lp->ll_li == NULL)
2795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002798 if (!quiet)
2799 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802
2803 /*
2804 * May need to find the item or absolute index for the second
2805 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 * When no index given: "lp->ll_empty2" is TRUE.
2807 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002811 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002817 {
2818 if (!quiet)
2819 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002821 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2826 if (lp->ll_n1 < 0)
2827 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2828 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 {
2830 if (!quiet)
2831 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002834 }
2835
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002838 }
2839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return p;
2841}
2842
2843/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 */
2846 static void
2847clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002848 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849{
2850 vim_free(lp->ll_exp_name);
2851 vim_free(lp->ll_newkey);
2852}
2853
2854/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002855 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 */
2859 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002860set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002865 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866{
2867 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 listitem_T *ri;
2869 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870
2871 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 cc = *endp;
2876 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 if (op != NULL && *op != '=')
2878 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002881 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002882 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002883 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884 {
2885 if (tv_op(&tv, rettv, op) == OK)
2886 set_var(lp->ll_name, &tv, FALSE);
2887 clear_tv(&tv);
2888 }
2889 }
2890 else
2891 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 else if (tv_check_lock(lp->ll_newkey == NULL
2896 ? lp->ll_tv->v_lock
2897 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2898 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 else if (lp->ll_range)
2900 {
2901 /*
2902 * Assign the List values to the list items.
2903 */
2904 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906 if (op != NULL && *op != '=')
2907 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2908 else
2909 {
2910 clear_tv(&lp->ll_li->li_tv);
2911 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 ri = ri->li_next;
2914 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2915 break;
2916 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002919 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ri = NULL;
2922 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 lp->ll_li = lp->ll_li->li_next;
2926 ++lp->ll_n1;
2927 }
2928 if (ri != NULL)
2929 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 else if (lp->ll_empty2
2931 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 : lp->ll_n1 != lp->ll_n2)
2933 EMSG(_("E711: List value has not enough items"));
2934 }
2935 else
2936 {
2937 /*
2938 * Assign to a List or Dictionary item.
2939 */
2940 if (lp->ll_newkey != NULL)
2941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 if (op != NULL && *op != '=')
2943 {
2944 EMSG2(_(e_letwrong), op);
2945 return;
2946 }
2947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002949 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 if (di == NULL)
2951 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002952 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2953 {
2954 vim_free(di);
2955 return;
2956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959 else if (op != NULL && *op != '=')
2960 {
2961 tv_op(lp->ll_tv, rettv, op);
2962 return;
2963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002964 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /*
2968 * Assign the value to the variable or list item.
2969 */
2970 if (copy)
2971 copy_tv(rettv, lp->ll_tv);
2972 else
2973 {
2974 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002975 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 }
2978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979}
2980
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2983 * Returns OK or FAIL.
2984 */
2985 static int
2986tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 typval_T *tv1;
2988 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 char_u *op;
2990{
2991 long n;
2992 char_u numbuf[NUMBUFLEN];
2993 char_u *s;
2994
2995 /* Can't do anything with a Funcref or a Dict on the right. */
2996 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2997 {
2998 switch (tv1->v_type)
2999 {
3000 case VAR_DICT:
3001 case VAR_FUNC:
3002 break;
3003
3004 case VAR_LIST:
3005 if (*op != '+' || tv2->v_type != VAR_LIST)
3006 break;
3007 /* List += List */
3008 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3009 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3010 return OK;
3011
3012 case VAR_NUMBER:
3013 case VAR_STRING:
3014 if (tv2->v_type == VAR_LIST)
3015 break;
3016 if (*op == '+' || *op == '-')
3017 {
3018 /* nr += nr or nr -= nr*/
3019 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003020#ifdef FEAT_FLOAT
3021 if (tv2->v_type == VAR_FLOAT)
3022 {
3023 float_T f = n;
3024
3025 if (*op == '+')
3026 f += tv2->vval.v_float;
3027 else
3028 f -= tv2->vval.v_float;
3029 clear_tv(tv1);
3030 tv1->v_type = VAR_FLOAT;
3031 tv1->vval.v_float = f;
3032 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034#endif
3035 {
3036 if (*op == '+')
3037 n += get_tv_number(tv2);
3038 else
3039 n -= get_tv_number(tv2);
3040 clear_tv(tv1);
3041 tv1->v_type = VAR_NUMBER;
3042 tv1->vval.v_number = n;
3043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 }
3045 else
3046 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 if (tv2->v_type == VAR_FLOAT)
3048 break;
3049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 /* str .= str */
3051 s = get_tv_string(tv1);
3052 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3053 clear_tv(tv1);
3054 tv1->v_type = VAR_STRING;
3055 tv1->vval.v_string = s;
3056 }
3057 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058
3059#ifdef FEAT_FLOAT
3060 case VAR_FLOAT:
3061 {
3062 float_T f;
3063
3064 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3065 && tv2->v_type != VAR_NUMBER
3066 && tv2->v_type != VAR_STRING))
3067 break;
3068 if (tv2->v_type == VAR_FLOAT)
3069 f = tv2->vval.v_float;
3070 else
3071 f = get_tv_number(tv2);
3072 if (*op == '+')
3073 tv1->vval.v_float += f;
3074 else
3075 tv1->vval.v_float -= f;
3076 }
3077 return OK;
3078#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 }
3080 }
3081
3082 EMSG2(_(e_letwrong), op);
3083 return FAIL;
3084}
3085
3086/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003087 * Add a watcher to a list.
3088 */
3089 static void
3090list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003091 list_T *l;
3092 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003093{
3094 lw->lw_next = l->lv_watch;
3095 l->lv_watch = lw;
3096}
3097
3098/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003099 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003100 * No warning when it isn't found...
3101 */
3102 static void
3103list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003104 list_T *l;
3105 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106{
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108
3109 lwp = &l->lv_watch;
3110 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3111 {
3112 if (lw == lwrem)
3113 {
3114 *lwp = lw->lw_next;
3115 break;
3116 }
3117 lwp = &lw->lw_next;
3118 }
3119}
3120
3121/*
3122 * Just before removing an item from a list: advance watchers to the next
3123 * item.
3124 */
3125 static void
3126list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 list_T *l;
3128 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003129{
Bram Moolenaar33570922005-01-25 22:26:29 +00003130 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131
3132 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3133 if (lw->lw_item == item)
3134 lw->lw_item = item->li_next;
3135}
3136
3137/*
3138 * Evaluate the expression used in a ":for var in expr" command.
3139 * "arg" points to "var".
3140 * Set "*errp" to TRUE for an error, FALSE otherwise;
3141 * Return a pointer that holds the info. Null when there is an error.
3142 */
3143 void *
3144eval_for_line(arg, errp, nextcmdp, skip)
3145 char_u *arg;
3146 int *errp;
3147 char_u **nextcmdp;
3148 int skip;
3149{
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 typval_T tv;
3153 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154
3155 *errp = TRUE; /* default: there is an error */
3156
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158 if (fi == NULL)
3159 return NULL;
3160
3161 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3162 if (expr == NULL)
3163 return fi;
3164
3165 expr = skipwhite(expr);
3166 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3167 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003168 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 return fi;
3170 }
3171
3172 if (skip)
3173 ++emsg_skip;
3174 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3175 {
3176 *errp = FALSE;
3177 if (!skip)
3178 {
3179 l = tv.vval.v_list;
3180 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003181 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003183 clear_tv(&tv);
3184 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 else
3186 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003187 /* No need to increment the refcount, it's already set for the
3188 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 fi->fi_list = l;
3190 list_add_watch(l, &fi->fi_lw);
3191 fi->fi_lw.lw_item = l->lv_first;
3192 }
3193 }
3194 }
3195 if (skip)
3196 --emsg_skip;
3197
3198 return fi;
3199}
3200
3201/*
3202 * Use the first item in a ":for" list. Advance to the next.
3203 * Assign the values to the variable (list). "arg" points to the first one.
3204 * Return TRUE when a valid item was found, FALSE when at end of list or
3205 * something wrong.
3206 */
3207 int
3208next_for_item(fi_void, arg)
3209 void *fi_void;
3210 char_u *arg;
3211{
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215
3216 item = fi->fi_lw.lw_item;
3217 if (item == NULL)
3218 result = FALSE;
3219 else
3220 {
3221 fi->fi_lw.lw_item = item->li_next;
3222 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3223 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3224 }
3225 return result;
3226}
3227
3228/*
3229 * Free the structure used to store info used by ":for".
3230 */
3231 void
3232free_for_info(fi_void)
3233 void *fi_void;
3234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003237 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003238 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003240 list_unref(fi->fi_list);
3241 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 vim_free(fi);
3243}
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3246
3247 void
3248set_context_for_expression(xp, arg, cmdidx)
3249 expand_T *xp;
3250 char_u *arg;
3251 cmdidx_T cmdidx;
3252{
3253 int got_eq = FALSE;
3254 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 if (cmdidx == CMD_let)
3258 {
3259 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003260 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 {
3262 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003263 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 {
3265 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 if (vim_iswhite(*p))
3268 break;
3269 }
3270 return;
3271 }
3272 }
3273 else
3274 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3275 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 while ((xp->xp_pattern = vim_strpbrk(arg,
3277 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3278 {
3279 c = *xp->xp_pattern;
3280 if (c == '&')
3281 {
3282 c = xp->xp_pattern[1];
3283 if (c == '&')
3284 {
3285 ++xp->xp_pattern;
3286 xp->xp_context = cmdidx != CMD_let || got_eq
3287 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3288 }
3289 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003292 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3293 xp->xp_pattern += 2;
3294
3295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297 else if (c == '$')
3298 {
3299 /* environment variable */
3300 xp->xp_context = EXPAND_ENV_VARS;
3301 }
3302 else if (c == '=')
3303 {
3304 got_eq = TRUE;
3305 xp->xp_context = EXPAND_EXPRESSION;
3306 }
3307 else if (c == '<'
3308 && xp->xp_context == EXPAND_FUNCTIONS
3309 && vim_strchr(xp->xp_pattern, '(') == NULL)
3310 {
3311 /* Function name can start with "<SNR>" */
3312 break;
3313 }
3314 else if (cmdidx != CMD_let || got_eq)
3315 {
3316 if (c == '"') /* string */
3317 {
3318 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3319 if (c == '\\' && xp->xp_pattern[1] != NUL)
3320 ++xp->xp_pattern;
3321 xp->xp_context = EXPAND_NOTHING;
3322 }
3323 else if (c == '\'') /* literal string */
3324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3327 /* skip */ ;
3328 xp->xp_context = EXPAND_NOTHING;
3329 }
3330 else if (c == '|')
3331 {
3332 if (xp->xp_pattern[1] == '|')
3333 {
3334 ++xp->xp_pattern;
3335 xp->xp_context = EXPAND_EXPRESSION;
3336 }
3337 else
3338 xp->xp_context = EXPAND_COMMANDS;
3339 }
3340 else
3341 xp->xp_context = EXPAND_EXPRESSION;
3342 }
3343 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 /* Doesn't look like something valid, expand as an expression
3345 * anyway. */
3346 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 arg = xp->xp_pattern;
3348 if (*arg != NUL)
3349 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3350 /* skip */ ;
3351 }
3352 xp->xp_pattern = arg;
3353}
3354
3355#endif /* FEAT_CMDL_COMPL */
3356
3357/*
3358 * ":1,25call func(arg1, arg2)" function call.
3359 */
3360 void
3361ex_call(eap)
3362 exarg_T *eap;
3363{
3364 char_u *arg = eap->arg;
3365 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003367 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003369 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 linenr_T lnum;
3371 int doesrange;
3372 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003375 if (eap->skip)
3376 {
3377 /* trans_function_name() doesn't work well when skipping, use eval0()
3378 * instead to skip to any following command, e.g. for:
3379 * :if 0 | call dict.foo().bar() | endif */
3380 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3381 return;
3382 }
3383
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003384 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003385 if (fudi.fd_newkey != NULL)
3386 {
3387 /* Still need to give an error message for missing key. */
3388 EMSG2(_(e_dictkey), fudi.fd_newkey);
3389 vim_free(fudi.fd_newkey);
3390 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003391 if (tofree == NULL)
3392 return;
3393
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003394 /* Increase refcount on dictionary, it could get deleted when evaluating
3395 * the arguments. */
3396 if (fudi.fd_dict != NULL)
3397 ++fudi.fd_dict->dv_refcount;
3398
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003399 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003400 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003401 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
Bram Moolenaar532c7802005-01-27 14:44:31 +00003403 /* Skip white space to allow ":call func ()". Not good, but required for
3404 * backward compatibility. */
3405 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 if (*startarg != '(')
3409 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003410 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 goto end;
3412 }
3413
3414 /*
3415 * When skipping, evaluate the function once, to find the end of the
3416 * arguments.
3417 * When the function takes a range, this is discovered after the first
3418 * call, and the loop is broken.
3419 */
3420 if (eap->skip)
3421 {
3422 ++emsg_skip;
3423 lnum = eap->line2; /* do it once, also with an invalid range */
3424 }
3425 else
3426 lnum = eap->line1;
3427 for ( ; lnum <= eap->line2; ++lnum)
3428 {
3429 if (!eap->skip && eap->addr_count > 0)
3430 {
3431 curwin->w_cursor.lnum = lnum;
3432 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003433#ifdef FEAT_VIRTUALEDIT
3434 curwin->w_cursor.coladd = 0;
3435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003438 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003439 eap->line1, eap->line2, &doesrange,
3440 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 {
3442 failed = TRUE;
3443 break;
3444 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003445
3446 /* Handle a function returning a Funcref, Dictionary or List. */
3447 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3448 {
3449 failed = TRUE;
3450 break;
3451 }
3452
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003453 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (doesrange || eap->skip)
3455 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003456
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003458 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003459 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003460 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (aborting())
3462 break;
3463 }
3464 if (eap->skip)
3465 --emsg_skip;
3466
3467 if (!failed)
3468 {
3469 /* Check for trailing illegal characters and a following command. */
3470 if (!ends_excmd(*arg))
3471 {
3472 emsg_severe = TRUE;
3473 EMSG(_(e_trailing));
3474 }
3475 else
3476 eap->nextcmd = check_nextcmd(arg);
3477 }
3478
3479end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003480 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482}
3483
3484/*
3485 * ":unlet[!] var1 ... " command.
3486 */
3487 void
3488ex_unlet(eap)
3489 exarg_T *eap;
3490{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003491 ex_unletlock(eap, eap->arg, 0);
3492}
3493
3494/*
3495 * ":lockvar" and ":unlockvar" commands
3496 */
3497 void
3498ex_lockvar(eap)
3499 exarg_T *eap;
3500{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003502 int deep = 2;
3503
3504 if (eap->forceit)
3505 deep = -1;
3506 else if (vim_isdigit(*arg))
3507 {
3508 deep = getdigits(&arg);
3509 arg = skipwhite(arg);
3510 }
3511
3512 ex_unletlock(eap, arg, deep);
3513}
3514
3515/*
3516 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3517 */
3518 static void
3519ex_unletlock(eap, argstart, deep)
3520 exarg_T *eap;
3521 char_u *argstart;
3522 int deep;
3523{
3524 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003527 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 do
3530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003531 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003532 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3533 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003534 if (lv.ll_name == NULL)
3535 error = TRUE; /* error but continue parsing */
3536 if (name_end == NULL || (!vim_iswhite(*name_end)
3537 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 if (name_end != NULL)
3540 {
3541 emsg_severe = TRUE;
3542 EMSG(_(e_trailing));
3543 }
3544 if (!(eap->skip || error))
3545 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 break;
3547 }
3548
3549 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 {
3551 if (eap->cmdidx == CMD_unlet)
3552 {
3553 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3554 error = TRUE;
3555 }
3556 else
3557 {
3558 if (do_lock_var(&lv, name_end, deep,
3559 eap->cmdidx == CMD_lockvar) == FAIL)
3560 error = TRUE;
3561 }
3562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 if (!eap->skip)
3565 clear_lval(&lv);
3566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 arg = skipwhite(name_end);
3568 } while (!ends_excmd(*arg));
3569
3570 eap->nextcmd = check_nextcmd(arg);
3571}
3572
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003576 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 int forceit;
3578{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003579 int ret = OK;
3580 int cc;
3581
3582 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003584 cc = *name_end;
3585 *name_end = NUL;
3586
3587 /* Normal name or expanded name. */
3588 if (check_changedtick(lp->ll_name))
3589 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003592 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3595 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 else if (lp->ll_range)
3597 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599
3600 /* Delete a range of List items. */
3601 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3602 {
3603 li = lp->ll_li->li_next;
3604 listitem_remove(lp->ll_list, lp->ll_li);
3605 lp->ll_li = li;
3606 ++lp->ll_n1;
3607 }
3608 }
3609 else
3610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 /* unlet a List item. */
3613 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003616 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 }
3618
3619 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620}
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622/*
3623 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
3626 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003627do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003629 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
Bram Moolenaar33570922005-01-25 22:26:29 +00003631 hashtab_T *ht;
3632 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003633 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003634 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar33570922005-01-25 22:26:29 +00003636 ht = find_var_ht(name, &varname);
3637 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003639 hi = hash_find(ht, varname);
3640 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003641 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003642 di = HI2DI(hi);
3643 if (var_check_fixed(di->di_flags, name)
3644 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645 return FAIL;
3646 delete_var(ht, hi);
3647 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650 if (forceit)
3651 return OK;
3652 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return FAIL;
3654}
3655
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656/*
3657 * Lock or unlock variable indicated by "lp".
3658 * "deep" is the levels to go (-1 for unlimited);
3659 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3660 */
3661 static int
3662do_lock_var(lp, name_end, deep, lock)
3663 lval_T *lp;
3664 char_u *name_end;
3665 int deep;
3666 int lock;
3667{
3668 int ret = OK;
3669 int cc;
3670 dictitem_T *di;
3671
3672 if (deep == 0) /* nothing to do */
3673 return OK;
3674
3675 if (lp->ll_tv == NULL)
3676 {
3677 cc = *name_end;
3678 *name_end = NUL;
3679
3680 /* Normal name or expanded name. */
3681 if (check_changedtick(lp->ll_name))
3682 ret = FAIL;
3683 else
3684 {
3685 di = find_var(lp->ll_name, NULL);
3686 if (di == NULL)
3687 ret = FAIL;
3688 else
3689 {
3690 if (lock)
3691 di->di_flags |= DI_FLAGS_LOCK;
3692 else
3693 di->di_flags &= ~DI_FLAGS_LOCK;
3694 item_lock(&di->di_tv, deep, lock);
3695 }
3696 }
3697 *name_end = cc;
3698 }
3699 else if (lp->ll_range)
3700 {
3701 listitem_T *li = lp->ll_li;
3702
3703 /* (un)lock a range of List items. */
3704 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3705 {
3706 item_lock(&li->li_tv, deep, lock);
3707 li = li->li_next;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else if (lp->ll_list != NULL)
3712 /* (un)lock a List item. */
3713 item_lock(&lp->ll_li->li_tv, deep, lock);
3714 else
3715 /* un(lock) a Dictionary item. */
3716 item_lock(&lp->ll_di->di_tv, deep, lock);
3717
3718 return ret;
3719}
3720
3721/*
3722 * Lock or unlock an item. "deep" is nr of levels to go.
3723 */
3724 static void
3725item_lock(tv, deep, lock)
3726 typval_T *tv;
3727 int deep;
3728 int lock;
3729{
3730 static int recurse = 0;
3731 list_T *l;
3732 listitem_T *li;
3733 dict_T *d;
3734 hashitem_T *hi;
3735 int todo;
3736
3737 if (recurse >= DICT_MAXNEST)
3738 {
3739 EMSG(_("E743: variable nested too deep for (un)lock"));
3740 return;
3741 }
3742 if (deep == 0)
3743 return;
3744 ++recurse;
3745
3746 /* lock/unlock the item itself */
3747 if (lock)
3748 tv->v_lock |= VAR_LOCKED;
3749 else
3750 tv->v_lock &= ~VAR_LOCKED;
3751
3752 switch (tv->v_type)
3753 {
3754 case VAR_LIST:
3755 if ((l = tv->vval.v_list) != NULL)
3756 {
3757 if (lock)
3758 l->lv_lock |= VAR_LOCKED;
3759 else
3760 l->lv_lock &= ~VAR_LOCKED;
3761 if (deep < 0 || deep > 1)
3762 /* recursive: lock/unlock the items the List contains */
3763 for (li = l->lv_first; li != NULL; li = li->li_next)
3764 item_lock(&li->li_tv, deep - 1, lock);
3765 }
3766 break;
3767 case VAR_DICT:
3768 if ((d = tv->vval.v_dict) != NULL)
3769 {
3770 if (lock)
3771 d->dv_lock |= VAR_LOCKED;
3772 else
3773 d->dv_lock &= ~VAR_LOCKED;
3774 if (deep < 0 || deep > 1)
3775 {
3776 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003777 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3779 {
3780 if (!HASHITEM_EMPTY(hi))
3781 {
3782 --todo;
3783 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3784 }
3785 }
3786 }
3787 }
3788 }
3789 --recurse;
3790}
3791
Bram Moolenaara40058a2005-07-11 22:42:07 +00003792/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003793 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3794 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003795 */
3796 static int
3797tv_islocked(tv)
3798 typval_T *tv;
3799{
3800 return (tv->v_lock & VAR_LOCKED)
3801 || (tv->v_type == VAR_LIST
3802 && tv->vval.v_list != NULL
3803 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3804 || (tv->v_type == VAR_DICT
3805 && tv->vval.v_dict != NULL
3806 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3807}
3808
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3810/*
3811 * Delete all "menutrans_" variables.
3812 */
3813 void
3814del_menutrans_vars()
3815{
Bram Moolenaar33570922005-01-25 22:26:29 +00003816 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003817 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
Bram Moolenaar33570922005-01-25 22:26:29 +00003819 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003820 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003821 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003822 {
3823 if (!HASHITEM_EMPTY(hi))
3824 {
3825 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3827 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003828 }
3829 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831}
3832#endif
3833
3834#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3835
3836/*
3837 * Local string buffer for the next two functions to store a variable name
3838 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3839 * get_user_var_name().
3840 */
3841
3842static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3843
3844static char_u *varnamebuf = NULL;
3845static int varnamebuflen = 0;
3846
3847/*
3848 * Function to concatenate a prefix and a variable name.
3849 */
3850 static char_u *
3851cat_prefix_varname(prefix, name)
3852 int prefix;
3853 char_u *name;
3854{
3855 int len;
3856
3857 len = (int)STRLEN(name) + 3;
3858 if (len > varnamebuflen)
3859 {
3860 vim_free(varnamebuf);
3861 len += 10; /* some additional space */
3862 varnamebuf = alloc(len);
3863 if (varnamebuf == NULL)
3864 {
3865 varnamebuflen = 0;
3866 return NULL;
3867 }
3868 varnamebuflen = len;
3869 }
3870 *varnamebuf = prefix;
3871 varnamebuf[1] = ':';
3872 STRCPY(varnamebuf + 2, name);
3873 return varnamebuf;
3874}
3875
3876/*
3877 * Function given to ExpandGeneric() to obtain the list of user defined
3878 * (global/buffer/window/built-in) variable names.
3879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 char_u *
3881get_user_var_name(xp, idx)
3882 expand_T *xp;
3883 int idx;
3884{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003885 static long_u gdone;
3886 static long_u bdone;
3887 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003888#ifdef FEAT_WINDOWS
3889 static long_u tdone;
3890#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003891 static int vidx;
3892 static hashitem_T *hi;
3893 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
3895 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003896 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003897 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003898#ifdef FEAT_WINDOWS
3899 tdone = 0;
3900#endif
3901 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003902
3903 /* Global variables */
3904 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003906 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003908 else
3909 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003910 while (HASHITEM_EMPTY(hi))
3911 ++hi;
3912 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3913 return cat_prefix_varname('g', hi->hi_key);
3914 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003916
3917 /* b: variables */
3918 ht = &curbuf->b_vars.dv_hashtab;
3919 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003923 else
3924 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003925 while (HASHITEM_EMPTY(hi))
3926 ++hi;
3927 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 return (char_u *)"b:changedtick";
3933 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003934
3935 /* w: variables */
3936 ht = &curwin->w_vars.dv_hashtab;
3937 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003939 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hi = ht->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 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003947
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003948#ifdef FEAT_WINDOWS
3949 /* t: variables */
3950 ht = &curtab->tp_vars.dv_hashtab;
3951 if (tdone < ht->ht_used)
3952 {
3953 if (tdone++ == 0)
3954 hi = ht->ht_array;
3955 else
3956 ++hi;
3957 while (HASHITEM_EMPTY(hi))
3958 ++hi;
3959 return cat_prefix_varname('t', hi->hi_key);
3960 }
3961#endif
3962
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 /* v: variables */
3964 if (vidx < VV_LEN)
3965 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
3967 vim_free(varnamebuf);
3968 varnamebuf = NULL;
3969 varnamebuflen = 0;
3970 return NULL;
3971}
3972
3973#endif /* FEAT_CMDL_COMPL */
3974
3975/*
3976 * types for expressions.
3977 */
3978typedef enum
3979{
3980 TYPE_UNKNOWN = 0
3981 , TYPE_EQUAL /* == */
3982 , TYPE_NEQUAL /* != */
3983 , TYPE_GREATER /* > */
3984 , TYPE_GEQUAL /* >= */
3985 , TYPE_SMALLER /* < */
3986 , TYPE_SEQUAL /* <= */
3987 , TYPE_MATCH /* =~ */
3988 , TYPE_NOMATCH /* !~ */
3989} exptype_T;
3990
3991/*
3992 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003993 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3995 */
3996
3997/*
3998 * Handle zero level expression.
3999 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004000 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004001 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 * Return OK or FAIL.
4003 */
4004 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 char_u **nextcmd;
4009 int evaluate;
4010{
4011 int ret;
4012 char_u *p;
4013
4014 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (ret == FAIL || !ends_excmd(*p))
4017 {
4018 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 /*
4021 * Report the invalid expression unless the expression evaluation has
4022 * been cancelled due to an aborting error, an interrupt, or an
4023 * exception.
4024 */
4025 if (!aborting())
4026 EMSG2(_(e_invexpr2), arg);
4027 ret = FAIL;
4028 }
4029 if (nextcmd != NULL)
4030 *nextcmd = check_nextcmd(p);
4031
4032 return ret;
4033}
4034
4035/*
4036 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004037 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 *
4039 * "arg" must point to the first non-white of the expression.
4040 * "arg" is advanced to the next non-white after the recognized expression.
4041 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004042 * Note: "rettv.v_lock" is not set.
4043 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 * Return OK or FAIL.
4045 */
4046 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 int evaluate;
4051{
4052 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 /*
4056 * Get the first variable.
4057 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return FAIL;
4060
4061 if ((*arg)[0] == '?')
4062 {
4063 result = FALSE;
4064 if (evaluate)
4065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004066 int error = FALSE;
4067
4068 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004071 if (error)
4072 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074
4075 /*
4076 * Get the second variable.
4077 */
4078 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 return FAIL;
4081
4082 /*
4083 * Check for the ":".
4084 */
4085 if ((*arg)[0] != ':')
4086 {
4087 EMSG(_("E109: Missing ':' after '?'"));
4088 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 return FAIL;
4091 }
4092
4093 /*
4094 * Get the third variable.
4095 */
4096 *arg = skipwhite(*arg + 1);
4097 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4098 {
4099 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return FAIL;
4102 }
4103 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106
4107 return OK;
4108}
4109
4110/*
4111 * Handle first level expression:
4112 * expr2 || expr2 || expr2 logical OR
4113 *
4114 * "arg" must point to the first non-white of the expression.
4115 * "arg" is advanced to the next non-white after the recognized expression.
4116 *
4117 * Return OK or FAIL.
4118 */
4119 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 int evaluate;
4124{
Bram Moolenaar33570922005-01-25 22:26:29 +00004125 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 long result;
4127 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
4130 /*
4131 * Get the first variable.
4132 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FAIL;
4135
4136 /*
4137 * Repeat until there is no following "||".
4138 */
4139 first = TRUE;
4140 result = FALSE;
4141 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4142 {
4143 if (evaluate && first)
4144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004145 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004148 if (error)
4149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 first = FALSE;
4151 }
4152
4153 /*
4154 * Get the second variable.
4155 */
4156 *arg = skipwhite(*arg + 2);
4157 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4158 return FAIL;
4159
4160 /*
4161 * Compute the result.
4162 */
4163 if (evaluate && !result)
4164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (error)
4169 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 }
4171 if (evaluate)
4172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 rettv->v_type = VAR_NUMBER;
4174 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176 }
4177
4178 return OK;
4179}
4180
4181/*
4182 * Handle second level expression:
4183 * expr3 && expr3 && expr3 logical AND
4184 *
4185 * "arg" must point to the first non-white of the expression.
4186 * "arg" is advanced to the next non-white after the recognized expression.
4187 *
4188 * Return OK or FAIL.
4189 */
4190 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 int evaluate;
4195{
Bram Moolenaar33570922005-01-25 22:26:29 +00004196 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 long result;
4198 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
4201 /*
4202 * Get the first variable.
4203 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206
4207 /*
4208 * Repeat until there is no following "&&".
4209 */
4210 first = TRUE;
4211 result = TRUE;
4212 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4213 {
4214 if (evaluate && first)
4215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004219 if (error)
4220 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 first = FALSE;
4222 }
4223
4224 /*
4225 * Get the second variable.
4226 */
4227 *arg = skipwhite(*arg + 2);
4228 if (eval4(arg, &var2, evaluate && result) == FAIL)
4229 return FAIL;
4230
4231 /*
4232 * Compute the result.
4233 */
4234 if (evaluate && result)
4235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (error)
4240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242 if (evaluate)
4243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 rettv->v_type = VAR_NUMBER;
4245 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 }
4248
4249 return OK;
4250}
4251
4252/*
4253 * Handle third level expression:
4254 * var1 == var2
4255 * var1 =~ var2
4256 * var1 != var2
4257 * var1 !~ var2
4258 * var1 > var2
4259 * var1 >= var2
4260 * var1 < var2
4261 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004262 * var1 is var2
4263 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 *
4265 * "arg" must point to the first non-white of the expression.
4266 * "arg" is advanced to the next non-white after the recognized expression.
4267 *
4268 * Return OK or FAIL.
4269 */
4270 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int evaluate;
4275{
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 char_u *p;
4278 int i;
4279 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004280 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 int len = 2;
4282 long n1, n2;
4283 char_u *s1, *s2;
4284 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4285 regmatch_T regmatch;
4286 int ic;
4287 char_u *save_cpo;
4288
4289 /*
4290 * Get the first variable.
4291 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 return FAIL;
4294
4295 p = *arg;
4296 switch (p[0])
4297 {
4298 case '=': if (p[1] == '=')
4299 type = TYPE_EQUAL;
4300 else if (p[1] == '~')
4301 type = TYPE_MATCH;
4302 break;
4303 case '!': if (p[1] == '=')
4304 type = TYPE_NEQUAL;
4305 else if (p[1] == '~')
4306 type = TYPE_NOMATCH;
4307 break;
4308 case '>': if (p[1] != '=')
4309 {
4310 type = TYPE_GREATER;
4311 len = 1;
4312 }
4313 else
4314 type = TYPE_GEQUAL;
4315 break;
4316 case '<': if (p[1] != '=')
4317 {
4318 type = TYPE_SMALLER;
4319 len = 1;
4320 }
4321 else
4322 type = TYPE_SEQUAL;
4323 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004324 case 'i': if (p[1] == 's')
4325 {
4326 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4327 len = 5;
4328 if (!vim_isIDc(p[len]))
4329 {
4330 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4331 type_is = TRUE;
4332 }
4333 }
4334 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336
4337 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004338 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 */
4340 if (type != TYPE_UNKNOWN)
4341 {
4342 /* extra question mark appended: ignore case */
4343 if (p[len] == '?')
4344 {
4345 ic = TRUE;
4346 ++len;
4347 }
4348 /* extra '#' appended: match case */
4349 else if (p[len] == '#')
4350 {
4351 ic = FALSE;
4352 ++len;
4353 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004354 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 else
4356 ic = p_ic;
4357
4358 /*
4359 * Get the second variable.
4360 */
4361 *arg = skipwhite(p + len);
4362 if (eval5(arg, &var2, evaluate) == FAIL)
4363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 return FAIL;
4366 }
4367
4368 if (evaluate)
4369 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004370 if (type_is && rettv->v_type != var2.v_type)
4371 {
4372 /* For "is" a different type always means FALSE, for "notis"
4373 * it means TRUE. */
4374 n1 = (type == TYPE_NEQUAL);
4375 }
4376 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4377 {
4378 if (type_is)
4379 {
4380 n1 = (rettv->v_type == var2.v_type
4381 && rettv->vval.v_list == var2.vval.v_list);
4382 if (type == TYPE_NEQUAL)
4383 n1 = !n1;
4384 }
4385 else if (rettv->v_type != var2.v_type
4386 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4387 {
4388 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004389 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004391 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 clear_tv(rettv);
4393 clear_tv(&var2);
4394 return FAIL;
4395 }
4396 else
4397 {
4398 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004399 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4400 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004401 if (type == TYPE_NEQUAL)
4402 n1 = !n1;
4403 }
4404 }
4405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004406 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4407 {
4408 if (type_is)
4409 {
4410 n1 = (rettv->v_type == var2.v_type
4411 && rettv->vval.v_dict == var2.vval.v_dict);
4412 if (type == TYPE_NEQUAL)
4413 n1 = !n1;
4414 }
4415 else if (rettv->v_type != var2.v_type
4416 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4417 {
4418 if (rettv->v_type != var2.v_type)
4419 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4420 else
4421 EMSG(_("E736: Invalid operation for Dictionary"));
4422 clear_tv(rettv);
4423 clear_tv(&var2);
4424 return FAIL;
4425 }
4426 else
4427 {
4428 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004429 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4430 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004431 if (type == TYPE_NEQUAL)
4432 n1 = !n1;
4433 }
4434 }
4435
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4437 {
4438 if (rettv->v_type != var2.v_type
4439 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4440 {
4441 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004442 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004443 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004444 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 clear_tv(rettv);
4446 clear_tv(&var2);
4447 return FAIL;
4448 }
4449 else
4450 {
4451 /* Compare two Funcrefs for being equal or unequal. */
4452 if (rettv->vval.v_string == NULL
4453 || var2.vval.v_string == NULL)
4454 n1 = FALSE;
4455 else
4456 n1 = STRCMP(rettv->vval.v_string,
4457 var2.vval.v_string) == 0;
4458 if (type == TYPE_NEQUAL)
4459 n1 = !n1;
4460 }
4461 }
4462
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004463#ifdef FEAT_FLOAT
4464 /*
4465 * If one of the two variables is a float, compare as a float.
4466 * When using "=~" or "!~", always compare as string.
4467 */
4468 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4469 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4470 {
4471 float_T f1, f2;
4472
4473 if (rettv->v_type == VAR_FLOAT)
4474 f1 = rettv->vval.v_float;
4475 else
4476 f1 = get_tv_number(rettv);
4477 if (var2.v_type == VAR_FLOAT)
4478 f2 = var2.vval.v_float;
4479 else
4480 f2 = get_tv_number(&var2);
4481 n1 = FALSE;
4482 switch (type)
4483 {
4484 case TYPE_EQUAL: n1 = (f1 == f2); break;
4485 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4486 case TYPE_GREATER: n1 = (f1 > f2); break;
4487 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4488 case TYPE_SMALLER: n1 = (f1 < f2); break;
4489 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4490 case TYPE_UNKNOWN:
4491 case TYPE_MATCH:
4492 case TYPE_NOMATCH: break; /* avoid gcc warning */
4493 }
4494 }
4495#endif
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 /*
4498 * If one of the two variables is a number, compare as a number.
4499 * When using "=~" or "!~", always compare as string.
4500 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004501 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504 n1 = get_tv_number(rettv);
4505 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 switch (type)
4507 {
4508 case TYPE_EQUAL: n1 = (n1 == n2); break;
4509 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4510 case TYPE_GREATER: n1 = (n1 > n2); break;
4511 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4512 case TYPE_SMALLER: n1 = (n1 < n2); break;
4513 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4514 case TYPE_UNKNOWN:
4515 case TYPE_MATCH:
4516 case TYPE_NOMATCH: break; /* avoid gcc warning */
4517 }
4518 }
4519 else
4520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 s1 = get_tv_string_buf(rettv, buf1);
4522 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4524 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4525 else
4526 i = 0;
4527 n1 = FALSE;
4528 switch (type)
4529 {
4530 case TYPE_EQUAL: n1 = (i == 0); break;
4531 case TYPE_NEQUAL: n1 = (i != 0); break;
4532 case TYPE_GREATER: n1 = (i > 0); break;
4533 case TYPE_GEQUAL: n1 = (i >= 0); break;
4534 case TYPE_SMALLER: n1 = (i < 0); break;
4535 case TYPE_SEQUAL: n1 = (i <= 0); break;
4536
4537 case TYPE_MATCH:
4538 case TYPE_NOMATCH:
4539 /* avoid 'l' flag in 'cpoptions' */
4540 save_cpo = p_cpo;
4541 p_cpo = (char_u *)"";
4542 regmatch.regprog = vim_regcomp(s2,
4543 RE_MAGIC + RE_STRING);
4544 regmatch.rm_ic = ic;
4545 if (regmatch.regprog != NULL)
4546 {
4547 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4548 vim_free(regmatch.regprog);
4549 if (type == TYPE_NOMATCH)
4550 n1 = !n1;
4551 }
4552 p_cpo = save_cpo;
4553 break;
4554
4555 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4556 }
4557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558 clear_tv(rettv);
4559 clear_tv(&var2);
4560 rettv->v_type = VAR_NUMBER;
4561 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563 }
4564
4565 return OK;
4566}
4567
4568/*
4569 * Handle fourth level expression:
4570 * + number addition
4571 * - number subtraction
4572 * . string concatenation
4573 *
4574 * "arg" must point to the first non-white of the expression.
4575 * "arg" is advanced to the next non-white after the recognized expression.
4576 *
4577 * Return OK or FAIL.
4578 */
4579 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 int evaluate;
4584{
Bram Moolenaar33570922005-01-25 22:26:29 +00004585 typval_T var2;
4586 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 int op;
4588 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004589#ifdef FEAT_FLOAT
4590 float_T f1 = 0, f2 = 0;
4591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 char_u *s1, *s2;
4593 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4594 char_u *p;
4595
4596 /*
4597 * Get the first variable.
4598 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004599 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 return FAIL;
4601
4602 /*
4603 * Repeat computing, until no '+', '-' or '.' is following.
4604 */
4605 for (;;)
4606 {
4607 op = **arg;
4608 if (op != '+' && op != '-' && op != '.')
4609 break;
4610
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611 if ((op != '+' || rettv->v_type != VAR_LIST)
4612#ifdef FEAT_FLOAT
4613 && (op == '.' || rettv->v_type != VAR_FLOAT)
4614#endif
4615 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004616 {
4617 /* For "list + ...", an illegal use of the first operand as
4618 * a number cannot be determined before evaluating the 2nd
4619 * operand: if this is also a list, all is ok.
4620 * For "something . ...", "something - ..." or "non-list + ...",
4621 * we know that the first operand needs to be a string or number
4622 * without evaluating the 2nd operand. So check before to avoid
4623 * side effects after an error. */
4624 if (evaluate && get_tv_string_chk(rettv) == NULL)
4625 {
4626 clear_tv(rettv);
4627 return FAIL;
4628 }
4629 }
4630
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 /*
4632 * Get the second variable.
4633 */
4634 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004635 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 return FAIL;
4639 }
4640
4641 if (evaluate)
4642 {
4643 /*
4644 * Compute the result.
4645 */
4646 if (op == '.')
4647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004648 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4649 s2 = get_tv_string_buf_chk(&var2, buf2);
4650 if (s2 == NULL) /* type error ? */
4651 {
4652 clear_tv(rettv);
4653 clear_tv(&var2);
4654 return FAIL;
4655 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004656 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004657 clear_tv(rettv);
4658 rettv->v_type = VAR_STRING;
4659 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004661 else if (op == '+' && rettv->v_type == VAR_LIST
4662 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004663 {
4664 /* concatenate Lists */
4665 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4666 &var3) == FAIL)
4667 {
4668 clear_tv(rettv);
4669 clear_tv(&var2);
4670 return FAIL;
4671 }
4672 clear_tv(rettv);
4673 *rettv = var3;
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 else
4676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004677 int error = FALSE;
4678
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004679#ifdef FEAT_FLOAT
4680 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004682 f1 = rettv->vval.v_float;
4683 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004684 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004685 else
4686#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004687 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004688 n1 = get_tv_number_chk(rettv, &error);
4689 if (error)
4690 {
4691 /* This can only happen for "list + non-list". For
4692 * "non-list + ..." or "something - ...", we returned
4693 * before evaluating the 2nd operand. */
4694 clear_tv(rettv);
4695 return FAIL;
4696 }
4697#ifdef FEAT_FLOAT
4698 if (var2.v_type == VAR_FLOAT)
4699 f1 = n1;
4700#endif
4701 }
4702#ifdef FEAT_FLOAT
4703 if (var2.v_type == VAR_FLOAT)
4704 {
4705 f2 = var2.vval.v_float;
4706 n2 = 0;
4707 }
4708 else
4709#endif
4710 {
4711 n2 = get_tv_number_chk(&var2, &error);
4712 if (error)
4713 {
4714 clear_tv(rettv);
4715 clear_tv(&var2);
4716 return FAIL;
4717 }
4718#ifdef FEAT_FLOAT
4719 if (rettv->v_type == VAR_FLOAT)
4720 f2 = n2;
4721#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004723 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724
4725#ifdef FEAT_FLOAT
4726 /* If there is a float on either side the result is a float. */
4727 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4728 {
4729 if (op == '+')
4730 f1 = f1 + f2;
4731 else
4732 f1 = f1 - f2;
4733 rettv->v_type = VAR_FLOAT;
4734 rettv->vval.v_float = f1;
4735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004737#endif
4738 {
4739 if (op == '+')
4740 n1 = n1 + n2;
4741 else
4742 n1 = n1 - n2;
4743 rettv->v_type = VAR_NUMBER;
4744 rettv->vval.v_number = n1;
4745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004747 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749 }
4750 return OK;
4751}
4752
4753/*
4754 * Handle fifth level expression:
4755 * * number multiplication
4756 * / number division
4757 * % number modulo
4758 *
4759 * "arg" must point to the first non-white of the expression.
4760 * "arg" is advanced to the next non-white after the recognized expression.
4761 *
4762 * Return OK or FAIL.
4763 */
4764 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004765eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004769 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
Bram Moolenaar33570922005-01-25 22:26:29 +00004771 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 int op;
4773 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774#ifdef FEAT_FLOAT
4775 int use_float = FALSE;
4776 float_T f1 = 0, f2;
4777#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 /*
4781 * Get the first variable.
4782 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004783 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 return FAIL;
4785
4786 /*
4787 * Repeat computing, until no '*', '/' or '%' is following.
4788 */
4789 for (;;)
4790 {
4791 op = **arg;
4792 if (op != '*' && op != '/' && op != '%')
4793 break;
4794
4795 if (evaluate)
4796 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797#ifdef FEAT_FLOAT
4798 if (rettv->v_type == VAR_FLOAT)
4799 {
4800 f1 = rettv->vval.v_float;
4801 use_float = TRUE;
4802 n1 = 0;
4803 }
4804 else
4805#endif
4806 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004807 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004808 if (error)
4809 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811 else
4812 n1 = 0;
4813
4814 /*
4815 * Get the second variable.
4816 */
4817 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004818 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return FAIL;
4820
4821 if (evaluate)
4822 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004823#ifdef FEAT_FLOAT
4824 if (var2.v_type == VAR_FLOAT)
4825 {
4826 if (!use_float)
4827 {
4828 f1 = n1;
4829 use_float = TRUE;
4830 }
4831 f2 = var2.vval.v_float;
4832 n2 = 0;
4833 }
4834 else
4835#endif
4836 {
4837 n2 = get_tv_number_chk(&var2, &error);
4838 clear_tv(&var2);
4839 if (error)
4840 return FAIL;
4841#ifdef FEAT_FLOAT
4842 if (use_float)
4843 f2 = n2;
4844#endif
4845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 /*
4848 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004849 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004851#ifdef FEAT_FLOAT
4852 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004854 if (op == '*')
4855 f1 = f1 * f2;
4856 else if (op == '/')
4857 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004858# ifdef VMS
4859 /* VMS crashes on divide by zero, work around it */
4860 if (f2 == 0.0)
4861 {
4862 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004863 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004864 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004865 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004866 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004867 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004868 }
4869 else
4870 f1 = f1 / f2;
4871# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872 /* We rely on the floating point library to handle divide
4873 * by zero to result in "inf" and not a crash. */
4874 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004875# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004879 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 return FAIL;
4881 }
4882 rettv->v_type = VAR_FLOAT;
4883 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 if (op == '*')
4889 n1 = n1 * n2;
4890 else if (op == '/')
4891 {
4892 if (n2 == 0) /* give an error message? */
4893 {
4894 if (n1 == 0)
4895 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4896 else if (n1 < 0)
4897 n1 = -0x7fffffffL;
4898 else
4899 n1 = 0x7fffffffL;
4900 }
4901 else
4902 n1 = n1 / n2;
4903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 {
4906 if (n2 == 0) /* give an error message? */
4907 n1 = 0;
4908 else
4909 n1 = n1 % n2;
4910 }
4911 rettv->v_type = VAR_NUMBER;
4912 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 }
4916
4917 return OK;
4918}
4919
4920/*
4921 * Handle sixth level expression:
4922 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004923 * "string" string constant
4924 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 * &option-name option value
4926 * @r register contents
4927 * identifier variable value
4928 * function() function call
4929 * $VAR environment variable
4930 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004931 * [expr, expr] List
4932 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 *
4934 * Also handle:
4935 * ! in front logical NOT
4936 * - in front unary minus
4937 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 * trailing [] subscript in String or List
4939 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 *
4941 * "arg" must point to the first non-white of the expression.
4942 * "arg" is advanced to the next non-white after the recognized expression.
4943 *
4944 * Return OK or FAIL.
4945 */
4946 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004947eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004951 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 long n;
4954 int len;
4955 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 char_u *start_leader, *end_leader;
4957 int ret = OK;
4958 char_u *alias;
4959
4960 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004961 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004962 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966 /*
4967 * Skip '!' and '-' characters. They are handled later.
4968 */
4969 start_leader = *arg;
4970 while (**arg == '!' || **arg == '-' || **arg == '+')
4971 *arg = skipwhite(*arg + 1);
4972 end_leader = *arg;
4973
4974 switch (**arg)
4975 {
4976 /*
4977 * Number constant.
4978 */
4979 case '0':
4980 case '1':
4981 case '2':
4982 case '3':
4983 case '4':
4984 case '5':
4985 case '6':
4986 case '7':
4987 case '8':
4988 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 {
4990#ifdef FEAT_FLOAT
4991 char_u *p = skipdigits(*arg + 1);
4992 int get_float = FALSE;
4993
4994 /* We accept a float when the format matches
4995 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004996 * strict to avoid backwards compatibility problems.
4997 * Don't look for a float after the "." operator, so that
4998 * ":let vers = 1.2.3" doesn't fail. */
4999 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 get_float = TRUE;
5002 p = skipdigits(p + 2);
5003 if (*p == 'e' || *p == 'E')
5004 {
5005 ++p;
5006 if (*p == '-' || *p == '+')
5007 ++p;
5008 if (!vim_isdigit(*p))
5009 get_float = FALSE;
5010 else
5011 p = skipdigits(p + 1);
5012 }
5013 if (ASCII_ISALPHA(*p) || *p == '.')
5014 get_float = FALSE;
5015 }
5016 if (get_float)
5017 {
5018 float_T f;
5019
5020 *arg += string2float(*arg, &f);
5021 if (evaluate)
5022 {
5023 rettv->v_type = VAR_FLOAT;
5024 rettv->vval.v_float = f;
5025 }
5026 }
5027 else
5028#endif
5029 {
5030 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5031 *arg += len;
5032 if (evaluate)
5033 {
5034 rettv->v_type = VAR_NUMBER;
5035 rettv->vval.v_number = n;
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
5041 /*
5042 * String constant: "string".
5043 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005044 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 break;
5046
5047 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005050 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005051 break;
5052
5053 /*
5054 * List: [expr, expr]
5055 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 break;
5058
5059 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 * Dictionary: {key: val, key: val}
5061 */
5062 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5063 break;
5064
5065 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005066 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005068 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 break;
5070
5071 /*
5072 * Environment variable: $VAR.
5073 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 break;
5076
5077 /*
5078 * Register contents: @r.
5079 */
5080 case '@': ++*arg;
5081 if (evaluate)
5082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005084 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 if (**arg != NUL)
5087 ++*arg;
5088 break;
5089
5090 /*
5091 * nested expression: (expression).
5092 */
5093 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (**arg == ')')
5096 ++*arg;
5097 else if (ret == OK)
5098 {
5099 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 ret = FAIL;
5102 }
5103 break;
5104
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 break;
5107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108
5109 if (ret == NOTDONE)
5110 {
5111 /*
5112 * Must be a variable or function name.
5113 * Can also be a curly-braces kind of name: {expr}.
5114 */
5115 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005116 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 if (alias != NULL)
5118 s = alias;
5119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005120 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 ret = FAIL;
5122 else
5123 {
5124 if (**arg == '(') /* recursive! */
5125 {
5126 /* If "s" is the name of a variable of type VAR_FUNC
5127 * use its contents. */
5128 s = deref_func_name(s, &len);
5129
5130 /* Invoke the function. */
5131 ret = get_func_tv(s, len, rettv, arg,
5132 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005133 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 /* Stop the expression evaluation when immediately
5135 * aborting on error, or when an interrupt occurred or
5136 * an exception was thrown but not caught. */
5137 if (aborting())
5138 {
5139 if (ret == OK)
5140 clear_tv(rettv);
5141 ret = FAIL;
5142 }
5143 }
5144 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005145 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005146 else
5147 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005149 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 }
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 *arg = skipwhite(*arg);
5153
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005154 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5155 * expr(expr). */
5156 if (ret == OK)
5157 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
5159 /*
5160 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5161 */
5162 if (ret == OK && evaluate && end_leader > start_leader)
5163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165 int val = 0;
5166#ifdef FEAT_FLOAT
5167 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005168
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005169 if (rettv->v_type == VAR_FLOAT)
5170 f = rettv->vval.v_float;
5171 else
5172#endif
5173 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005174 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005176 clear_tv(rettv);
5177 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 else
5180 {
5181 while (end_leader > start_leader)
5182 {
5183 --end_leader;
5184 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005185 {
5186#ifdef FEAT_FLOAT
5187 if (rettv->v_type == VAR_FLOAT)
5188 f = !f;
5189 else
5190#endif
5191 val = !val;
5192 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005194 {
5195#ifdef FEAT_FLOAT
5196 if (rettv->v_type == VAR_FLOAT)
5197 f = -f;
5198 else
5199#endif
5200 val = -val;
5201 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005203#ifdef FEAT_FLOAT
5204 if (rettv->v_type == VAR_FLOAT)
5205 {
5206 clear_tv(rettv);
5207 rettv->vval.v_float = f;
5208 }
5209 else
5210#endif
5211 {
5212 clear_tv(rettv);
5213 rettv->v_type = VAR_NUMBER;
5214 rettv->vval.v_number = val;
5215 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218
5219 return ret;
5220}
5221
5222/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005223 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5224 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5226 */
5227 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005230 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005232 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233{
5234 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005235 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005236 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 long len = -1;
5238 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005242 if (rettv->v_type == VAR_FUNC
5243#ifdef FEAT_FLOAT
5244 || rettv->v_type == VAR_FLOAT
5245#endif
5246 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005248 if (verbose)
5249 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 return FAIL;
5251 }
5252
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 /*
5256 * dict.name
5257 */
5258 key = *arg + 1;
5259 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5260 ;
5261 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 }
5265 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 /*
5268 * something[idx]
5269 *
5270 * Get the (first) variable from inside the [].
5271 */
5272 *arg = skipwhite(*arg + 1);
5273 if (**arg == ':')
5274 empty1 = TRUE;
5275 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5276 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5278 {
5279 /* not a number or string */
5280 clear_tv(&var1);
5281 return FAIL;
5282 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283
5284 /*
5285 * Get the second variable from inside the [:].
5286 */
5287 if (**arg == ':')
5288 {
5289 range = TRUE;
5290 *arg = skipwhite(*arg + 1);
5291 if (**arg == ']')
5292 empty2 = TRUE;
5293 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 if (!empty1)
5296 clear_tv(&var1);
5297 return FAIL;
5298 }
5299 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5300 {
5301 /* not a number or string */
5302 if (!empty1)
5303 clear_tv(&var1);
5304 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 return FAIL;
5306 }
5307 }
5308
5309 /* Check for the ']'. */
5310 if (**arg != ']')
5311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005312 if (verbose)
5313 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314 clear_tv(&var1);
5315 if (range)
5316 clear_tv(&var2);
5317 return FAIL;
5318 }
5319 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 }
5321
5322 if (evaluate)
5323 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324 n1 = 0;
5325 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005327 n1 = get_tv_number(&var1);
5328 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 }
5330 if (range)
5331 {
5332 if (empty2)
5333 n2 = -1;
5334 else
5335 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005336 n2 = get_tv_number(&var2);
5337 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 }
5339 }
5340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005341 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 {
5343 case VAR_NUMBER:
5344 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005345 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 len = (long)STRLEN(s);
5347 if (range)
5348 {
5349 /* The resulting variable is a substring. If the indexes
5350 * are out of range the result is empty. */
5351 if (n1 < 0)
5352 {
5353 n1 = len + n1;
5354 if (n1 < 0)
5355 n1 = 0;
5356 }
5357 if (n2 < 0)
5358 n2 = len + n2;
5359 else if (n2 >= len)
5360 n2 = len;
5361 if (n1 >= len || n2 < 0 || n1 > n2)
5362 s = NULL;
5363 else
5364 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5365 }
5366 else
5367 {
5368 /* The resulting variable is a string of a single
5369 * character. If the index is too big or negative the
5370 * result is empty. */
5371 if (n1 >= len || n1 < 0)
5372 s = NULL;
5373 else
5374 s = vim_strnsave(s + n1, 1);
5375 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 clear_tv(rettv);
5377 rettv->v_type = VAR_STRING;
5378 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 break;
5380
5381 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 if (n1 < 0)
5384 n1 = len + n1;
5385 if (!empty1 && (n1 < 0 || n1 >= len))
5386 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005387 /* For a range we allow invalid values and return an empty
5388 * list. A list index out of range is an error. */
5389 if (!range)
5390 {
5391 if (verbose)
5392 EMSGN(_(e_listidx), n1);
5393 return FAIL;
5394 }
5395 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 if (range)
5398 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 list_T *l;
5400 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401
5402 if (n2 < 0)
5403 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005404 else if (n2 >= len)
5405 n2 = len - 1;
5406 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005407 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 l = list_alloc();
5409 if (l == NULL)
5410 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 n1 <= n2; ++n1)
5413 {
5414 if (list_append_tv(l, &item->li_tv) == FAIL)
5415 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005416 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 return FAIL;
5418 }
5419 item = item->li_next;
5420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005421 clear_tv(rettv);
5422 rettv->v_type = VAR_LIST;
5423 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005424 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 }
5426 else
5427 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005428 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 clear_tv(rettv);
5430 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 }
5432 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433
5434 case VAR_DICT:
5435 if (range)
5436 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005437 if (verbose)
5438 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 if (len == -1)
5440 clear_tv(&var1);
5441 return FAIL;
5442 }
5443 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005444 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445
5446 if (len == -1)
5447 {
5448 key = get_tv_string(&var1);
5449 if (*key == NUL)
5450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451 if (verbose)
5452 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 clear_tv(&var1);
5454 return FAIL;
5455 }
5456 }
5457
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005458 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005460 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005461 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462 if (len == -1)
5463 clear_tv(&var1);
5464 if (item == NULL)
5465 return FAIL;
5466
5467 copy_tv(&item->di_tv, &var1);
5468 clear_tv(rettv);
5469 *rettv = var1;
5470 }
5471 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 }
5473 }
5474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 return OK;
5476}
5477
5478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 * Get an option value.
5480 * "arg" points to the '&' or '+' before the option name.
5481 * "arg" is advanced to character after the option name.
5482 * Return OK or FAIL.
5483 */
5484 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 int evaluate;
5489{
5490 char_u *option_end;
5491 long numval;
5492 char_u *stringval;
5493 int opt_type;
5494 int c;
5495 int working = (**arg == '+'); /* has("+option") */
5496 int ret = OK;
5497 int opt_flags;
5498
5499 /*
5500 * Isolate the option name and find its value.
5501 */
5502 option_end = find_option_end(arg, &opt_flags);
5503 if (option_end == NULL)
5504 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 EMSG2(_("E112: Option name missing: %s"), *arg);
5507 return FAIL;
5508 }
5509
5510 if (!evaluate)
5511 {
5512 *arg = option_end;
5513 return OK;
5514 }
5515
5516 c = *option_end;
5517 *option_end = NUL;
5518 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
5521 if (opt_type == -3) /* invalid name */
5522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 EMSG2(_("E113: Unknown option: %s"), *arg);
5525 ret = FAIL;
5526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
5529 if (opt_type == -2) /* hidden string option */
5530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 rettv->v_type = VAR_STRING;
5532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 else if (opt_type == -1) /* hidden number option */
5535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005536 rettv->v_type = VAR_NUMBER;
5537 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539 else if (opt_type == 1) /* number option */
5540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 rettv->v_type = VAR_NUMBER;
5542 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 }
5544 else /* string option */
5545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 rettv->v_type = VAR_STRING;
5547 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549 }
5550 else if (working && (opt_type == -2 || opt_type == -1))
5551 ret = FAIL;
5552
5553 *option_end = c; /* put back for error messages */
5554 *arg = option_end;
5555
5556 return ret;
5557}
5558
5559/*
5560 * Allocate a variable for a string constant.
5561 * Return OK or FAIL.
5562 */
5563 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 int evaluate;
5568{
5569 char_u *p;
5570 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 int extra = 0;
5572
5573 /*
5574 * Find the end of the string, skipping backslashed characters.
5575 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005576 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
5578 if (*p == '\\' && p[1] != NUL)
5579 {
5580 ++p;
5581 /* A "\<x>" form occupies at least 4 characters, and produces up
5582 * to 6 characters: reserve space for 2 extra */
5583 if (*p == '<')
5584 extra += 2;
5585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587
5588 if (*p != '"')
5589 {
5590 EMSG2(_("E114: Missing quote: %s"), *arg);
5591 return FAIL;
5592 }
5593
5594 /* If only parsing, set *arg and return here */
5595 if (!evaluate)
5596 {
5597 *arg = p + 1;
5598 return OK;
5599 }
5600
5601 /*
5602 * Copy the string into allocated memory, handling backslashed
5603 * characters.
5604 */
5605 name = alloc((unsigned)(p - *arg + extra));
5606 if (name == NULL)
5607 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 rettv->v_type = VAR_STRING;
5609 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
5613 if (*p == '\\')
5614 {
5615 switch (*++p)
5616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005617 case 'b': *name++ = BS; ++p; break;
5618 case 'e': *name++ = ESC; ++p; break;
5619 case 'f': *name++ = FF; ++p; break;
5620 case 'n': *name++ = NL; ++p; break;
5621 case 'r': *name++ = CAR; ++p; break;
5622 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 case 'X': /* hex: "\x1", "\x12" */
5625 case 'x':
5626 case 'u': /* Unicode: "\u0023" */
5627 case 'U':
5628 if (vim_isxdigit(p[1]))
5629 {
5630 int n, nr;
5631 int c = toupper(*p);
5632
5633 if (c == 'X')
5634 n = 2;
5635 else
5636 n = 4;
5637 nr = 0;
5638 while (--n >= 0 && vim_isxdigit(p[1]))
5639 {
5640 ++p;
5641 nr = (nr << 4) + hex2nr(*p);
5642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644#ifdef FEAT_MBYTE
5645 /* For "\u" store the number according to
5646 * 'encoding'. */
5647 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 else
5650#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 break;
5654
5655 /* octal: "\1", "\12", "\123" */
5656 case '0':
5657 case '1':
5658 case '2':
5659 case '3':
5660 case '4':
5661 case '5':
5662 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 case '7': *name = *p++ - '0';
5664 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 *name = (*name << 3) + *p++ - '0';
5667 if (*p >= '0' && *p <= '7')
5668 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 break;
5672
5673 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (extra != 0)
5676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 break;
5679 }
5680 /* FALLTHROUGH */
5681
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 break;
5684 }
5685 }
5686 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 *arg = p + 1;
5692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 return OK;
5694}
5695
5696/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 * Return OK or FAIL.
5699 */
5700 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 int evaluate;
5705{
5706 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005708 int reduce = 0;
5709
5710 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 */
5713 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005718 break;
5719 ++reduce;
5720 ++p;
5721 }
5722 }
5723
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005725 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 return FAIL;
5728 }
5729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005731 if (!evaluate)
5732 {
5733 *arg = p + 1;
5734 return OK;
5735 }
5736
5737 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 */
5740 str = alloc((unsigned)((p - *arg) - reduce));
5741 if (str == NULL)
5742 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 rettv->v_type = VAR_STRING;
5744 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 break;
5752 ++p;
5753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757 *arg = p + 1;
5758
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 return OK;
5760}
5761
5762/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763 * Allocate a variable for a List and fill it from "*arg".
5764 * Return OK or FAIL.
5765 */
5766 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005767get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005769 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 int evaluate;
5771{
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 list_T *l = NULL;
5773 typval_T tv;
5774 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775
5776 if (evaluate)
5777 {
5778 l = list_alloc();
5779 if (l == NULL)
5780 return FAIL;
5781 }
5782
5783 *arg = skipwhite(*arg + 1);
5784 while (**arg != ']' && **arg != NUL)
5785 {
5786 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5787 goto failret;
5788 if (evaluate)
5789 {
5790 item = listitem_alloc();
5791 if (item != NULL)
5792 {
5793 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005794 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 list_append(l, item);
5796 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005797 else
5798 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 }
5800
5801 if (**arg == ']')
5802 break;
5803 if (**arg != ',')
5804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 goto failret;
5807 }
5808 *arg = skipwhite(*arg + 1);
5809 }
5810
5811 if (**arg != ']')
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814failret:
5815 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005816 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 return FAIL;
5818 }
5819
5820 *arg = skipwhite(*arg + 1);
5821 if (evaluate)
5822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005823 rettv->v_type = VAR_LIST;
5824 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 ++l->lv_refcount;
5826 }
5827
5828 return OK;
5829}
5830
5831/*
5832 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005833 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005835 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836list_alloc()
5837{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005838 list_T *l;
5839
5840 l = (list_T *)alloc_clear(sizeof(list_T));
5841 if (l != NULL)
5842 {
5843 /* Prepend the list to the list of lists for garbage collection. */
5844 if (first_list != NULL)
5845 first_list->lv_used_prev = l;
5846 l->lv_used_prev = NULL;
5847 l->lv_used_next = first_list;
5848 first_list = l;
5849 }
5850 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851}
5852
5853/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005854 * Allocate an empty list for a return value.
5855 * Returns OK or FAIL.
5856 */
5857 static int
5858rettv_list_alloc(rettv)
5859 typval_T *rettv;
5860{
5861 list_T *l = list_alloc();
5862
5863 if (l == NULL)
5864 return FAIL;
5865
5866 rettv->vval.v_list = l;
5867 rettv->v_type = VAR_LIST;
5868 ++l->lv_refcount;
5869 return OK;
5870}
5871
5872/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 * Unreference a list: decrement the reference count and free it when it
5874 * becomes zero.
5875 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005876 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005880 if (l != NULL && --l->lv_refcount <= 0)
5881 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882}
5883
5884/*
5885 * Free a list, including all items it points to.
5886 * Ignores the reference count.
5887 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005888 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005889list_free(l, recurse)
5890 list_T *l;
5891 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892{
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005895 /* Remove the list from the list of lists for garbage collection. */
5896 if (l->lv_used_prev == NULL)
5897 first_list = l->lv_used_next;
5898 else
5899 l->lv_used_prev->lv_used_next = l->lv_used_next;
5900 if (l->lv_used_next != NULL)
5901 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5902
Bram Moolenaard9fba312005-06-26 22:34:35 +00005903 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005905 /* Remove the item before deleting it. */
5906 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005907 if (recurse || (item->li_tv.v_type != VAR_LIST
5908 && item->li_tv.v_type != VAR_DICT))
5909 clear_tv(&item->li_tv);
5910 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 }
5912 vim_free(l);
5913}
5914
5915/*
5916 * Allocate a list item.
5917 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919listitem_alloc()
5920{
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922}
5923
5924/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005925 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 */
5927 static void
5928listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005931 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 vim_free(item);
5933}
5934
5935/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005936 * Remove a list item from a List and free it. Also clears the value.
5937 */
5938 static void
5939listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 list_T *l;
5941 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005942{
5943 list_remove(l, item, item);
5944 listitem_free(item);
5945}
5946
5947/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 * Get the number of items in a list.
5949 */
5950 static long
5951list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 if (l == NULL)
5955 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005956 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957}
5958
5959/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005960 * Return TRUE when two lists have exactly the same values.
5961 */
5962 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005963list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 list_T *l1;
5965 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005966 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005967 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005970
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005971 if (l1 == NULL || l2 == NULL)
5972 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005973 if (l1 == l2)
5974 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005975 if (list_len(l1) != list_len(l2))
5976 return FALSE;
5977
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978 for (item1 = l1->lv_first, item2 = l2->lv_first;
5979 item1 != NULL && item2 != NULL;
5980 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005981 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982 return FALSE;
5983 return item1 == NULL && item2 == NULL;
5984}
5985
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005986#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5987 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005988/*
5989 * Return the dictitem that an entry in a hashtable points to.
5990 */
5991 dictitem_T *
5992dict_lookup(hi)
5993 hashitem_T *hi;
5994{
5995 return HI2DI(hi);
5996}
5997#endif
5998
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006000 * Return TRUE when two dictionaries have exactly the same key/values.
6001 */
6002 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 dict_T *d1;
6005 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006006 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006007 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006008{
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 hashitem_T *hi;
6010 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006011 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006012
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006013 if (d1 == NULL || d2 == NULL)
6014 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006015 if (d1 == d2)
6016 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006017 if (dict_len(d1) != dict_len(d2))
6018 return FALSE;
6019
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006020 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006022 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006023 if (!HASHITEM_EMPTY(hi))
6024 {
6025 item2 = dict_find(d2, hi->hi_key, -1);
6026 if (item2 == NULL)
6027 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006028 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006029 return FALSE;
6030 --todo;
6031 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006032 }
6033 return TRUE;
6034}
6035
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036static int tv_equal_recurse_limit;
6037
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006038/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006039 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006040 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006041 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006042 */
6043 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 typval_T *tv1;
6046 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006047 int ic; /* ignore case */
6048 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006049{
6050 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006051 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006052 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006053 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006054
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006055 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006056 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006058 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006059 * recursiveness to a limit. We guess they are equal then.
6060 * A fixed limit has the problem of still taking an awful long time.
6061 * Reduce the limit every time running into it. That should work fine for
6062 * deeply linked structures that are not recursively linked and catch
6063 * recursiveness quickly. */
6064 if (!recursive)
6065 tv_equal_recurse_limit = 1000;
6066 if (recursive_cnt >= tv_equal_recurse_limit)
6067 {
6068 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006069 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006070 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006071
6072 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006073 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006074 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006075 ++recursive_cnt;
6076 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6077 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006078 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006079
6080 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 ++recursive_cnt;
6082 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6083 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006084 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006085
6086 case VAR_FUNC:
6087 return (tv1->vval.v_string != NULL
6088 && tv2->vval.v_string != NULL
6089 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6090
6091 case VAR_NUMBER:
6092 return tv1->vval.v_number == tv2->vval.v_number;
6093
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006094#ifdef FEAT_FLOAT
6095 case VAR_FLOAT:
6096 return tv1->vval.v_float == tv2->vval.v_float;
6097#endif
6098
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006099 case VAR_STRING:
6100 s1 = get_tv_string_buf(tv1, buf1);
6101 s2 = get_tv_string_buf(tv2, buf2);
6102 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104
6105 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 return TRUE;
6107}
6108
6109/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110 * Locate item with index "n" in list "l" and return it.
6111 * A negative index is counted from the end; -1 is the last item.
6112 * Returns NULL when "n" is out of range.
6113 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006114 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006116 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 long n;
6118{
Bram Moolenaar33570922005-01-25 22:26:29 +00006119 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 long idx;
6121
6122 if (l == NULL)
6123 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006124
6125 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006126 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006127 n = l->lv_len + n;
6128
6129 /* Check for index out of range. */
6130 if (n < 0 || n >= l->lv_len)
6131 return NULL;
6132
6133 /* When there is a cached index may start search from there. */
6134 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006135 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006136 if (n < l->lv_idx / 2)
6137 {
6138 /* closest to the start of the list */
6139 item = l->lv_first;
6140 idx = 0;
6141 }
6142 else if (n > (l->lv_idx + l->lv_len) / 2)
6143 {
6144 /* closest to the end of the list */
6145 item = l->lv_last;
6146 idx = l->lv_len - 1;
6147 }
6148 else
6149 {
6150 /* closest to the cached index */
6151 item = l->lv_idx_item;
6152 idx = l->lv_idx;
6153 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 }
6155 else
6156 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006157 if (n < l->lv_len / 2)
6158 {
6159 /* closest to the start of the list */
6160 item = l->lv_first;
6161 idx = 0;
6162 }
6163 else
6164 {
6165 /* closest to the end of the list */
6166 item = l->lv_last;
6167 idx = l->lv_len - 1;
6168 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006170
6171 while (n > idx)
6172 {
6173 /* search forward */
6174 item = item->li_next;
6175 ++idx;
6176 }
6177 while (n < idx)
6178 {
6179 /* search backward */
6180 item = item->li_prev;
6181 --idx;
6182 }
6183
6184 /* cache the used index */
6185 l->lv_idx = idx;
6186 l->lv_idx_item = item;
6187
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 return item;
6189}
6190
6191/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006192 * Get list item "l[idx]" as a number.
6193 */
6194 static long
6195list_find_nr(l, idx, errorp)
6196 list_T *l;
6197 long idx;
6198 int *errorp; /* set to TRUE when something wrong */
6199{
6200 listitem_T *li;
6201
6202 li = list_find(l, idx);
6203 if (li == NULL)
6204 {
6205 if (errorp != NULL)
6206 *errorp = TRUE;
6207 return -1L;
6208 }
6209 return get_tv_number_chk(&li->li_tv, errorp);
6210}
6211
6212/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006213 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6214 */
6215 char_u *
6216list_find_str(l, idx)
6217 list_T *l;
6218 long idx;
6219{
6220 listitem_T *li;
6221
6222 li = list_find(l, idx - 1);
6223 if (li == NULL)
6224 {
6225 EMSGN(_(e_listidx), idx);
6226 return NULL;
6227 }
6228 return get_tv_string(&li->li_tv);
6229}
6230
6231/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006232 * Locate "item" list "l" and return its index.
6233 * Returns -1 when "item" is not in the list.
6234 */
6235 static long
6236list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 list_T *l;
6238 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006239{
6240 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006242
6243 if (l == NULL)
6244 return -1;
6245 idx = 0;
6246 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6247 ++idx;
6248 if (li == NULL)
6249 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006250 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006251}
6252
6253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 * Append item "item" to the end of list "l".
6255 */
6256 static void
6257list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 list_T *l;
6259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260{
6261 if (l->lv_last == NULL)
6262 {
6263 /* empty list */
6264 l->lv_first = item;
6265 l->lv_last = item;
6266 item->li_prev = NULL;
6267 }
6268 else
6269 {
6270 l->lv_last->li_next = item;
6271 item->li_prev = l->lv_last;
6272 l->lv_last = item;
6273 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006274 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 item->li_next = NULL;
6276}
6277
6278/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006280 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006282 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 list_T *l;
6285 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006287 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288
Bram Moolenaar05159a02005-02-26 23:04:13 +00006289 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006291 copy_tv(tv, &li->li_tv);
6292 list_append(l, li);
6293 return OK;
6294}
6295
6296/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006297 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006298 * Return FAIL when out of memory.
6299 */
6300 int
6301list_append_dict(list, dict)
6302 list_T *list;
6303 dict_T *dict;
6304{
6305 listitem_T *li = listitem_alloc();
6306
6307 if (li == NULL)
6308 return FAIL;
6309 li->li_tv.v_type = VAR_DICT;
6310 li->li_tv.v_lock = 0;
6311 li->li_tv.vval.v_dict = dict;
6312 list_append(list, li);
6313 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 return OK;
6315}
6316
6317/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006318 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006319 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006320 * Returns FAIL when out of memory.
6321 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006322 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006323list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006324 list_T *l;
6325 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006326 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006327{
6328 listitem_T *li = listitem_alloc();
6329
6330 if (li == NULL)
6331 return FAIL;
6332 list_append(l, li);
6333 li->li_tv.v_type = VAR_STRING;
6334 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006335 if (str == NULL)
6336 li->li_tv.vval.v_string = NULL;
6337 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006338 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006339 return FAIL;
6340 return OK;
6341}
6342
6343/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006344 * Append "n" to list "l".
6345 * Returns FAIL when out of memory.
6346 */
6347 static int
6348list_append_number(l, n)
6349 list_T *l;
6350 varnumber_T n;
6351{
6352 listitem_T *li;
6353
6354 li = listitem_alloc();
6355 if (li == NULL)
6356 return FAIL;
6357 li->li_tv.v_type = VAR_NUMBER;
6358 li->li_tv.v_lock = 0;
6359 li->li_tv.vval.v_number = n;
6360 list_append(l, li);
6361 return OK;
6362}
6363
6364/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006366 * If "item" is NULL append at the end.
6367 * Return FAIL when out of memory.
6368 */
6369 static int
6370list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 list_T *l;
6372 typval_T *tv;
6373 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374{
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006376
6377 if (ni == NULL)
6378 return FAIL;
6379 copy_tv(tv, &ni->li_tv);
6380 if (item == NULL)
6381 /* Append new item at end of list. */
6382 list_append(l, ni);
6383 else
6384 {
6385 /* Insert new item before existing item. */
6386 ni->li_prev = item->li_prev;
6387 ni->li_next = item;
6388 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006389 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006390 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006391 ++l->lv_idx;
6392 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006393 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006394 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006396 l->lv_idx_item = NULL;
6397 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006398 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006399 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006400 }
6401 return OK;
6402}
6403
6404/*
6405 * Extend "l1" with "l2".
6406 * If "bef" is NULL append at the end, otherwise insert before this item.
6407 * Returns FAIL when out of memory.
6408 */
6409 static int
6410list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 list_T *l1;
6412 list_T *l2;
6413 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414{
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006416 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006418 /* We also quit the loop when we have inserted the original item count of
6419 * the list, avoid a hang when we extend a list with itself. */
6420 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6422 return FAIL;
6423 return OK;
6424}
6425
6426/*
6427 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6428 * Return FAIL when out of memory.
6429 */
6430 static int
6431list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006432 list_T *l1;
6433 list_T *l2;
6434 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435{
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006438 if (l1 == NULL || l2 == NULL)
6439 return FAIL;
6440
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006442 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 if (l == NULL)
6444 return FAIL;
6445 tv->v_type = VAR_LIST;
6446 tv->vval.v_list = l;
6447
6448 /* append all items from the second list */
6449 return list_extend(l, l2, NULL);
6450}
6451
6452/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006453 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006455 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456 * Returns NULL when out of memory.
6457 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006460 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006462 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463{
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 list_T *copy;
6465 listitem_T *item;
6466 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467
6468 if (orig == NULL)
6469 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006470
6471 copy = list_alloc();
6472 if (copy != NULL)
6473 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006474 if (copyID != 0)
6475 {
6476 /* Do this before adding the items, because one of the items may
6477 * refer back to this list. */
6478 orig->lv_copyID = copyID;
6479 orig->lv_copylist = copy;
6480 }
6481 for (item = orig->lv_first; item != NULL && !got_int;
6482 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 {
6484 ni = listitem_alloc();
6485 if (ni == NULL)
6486 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006487 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 {
6489 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6490 {
6491 vim_free(ni);
6492 break;
6493 }
6494 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006496 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 list_append(copy, ni);
6498 }
6499 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 if (item != NULL)
6501 {
6502 list_unref(copy);
6503 copy = NULL;
6504 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006505 }
6506
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507 return copy;
6508}
6509
6510/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006512 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006514 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006515list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 list_T *l;
6517 listitem_T *item;
6518 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519{
Bram Moolenaar33570922005-01-25 22:26:29 +00006520 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 /* notify watchers */
6523 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 list_fix_watch(l, ip);
6527 if (ip == item2)
6528 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530
6531 if (item2->li_next == NULL)
6532 l->lv_last = item->li_prev;
6533 else
6534 item2->li_next->li_prev = item->li_prev;
6535 if (item->li_prev == NULL)
6536 l->lv_first = item2->li_next;
6537 else
6538 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006539 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540}
6541
6542/*
6543 * Return an allocated string with the string representation of a list.
6544 * May return NULL.
6545 */
6546 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006547list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006549 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550{
6551 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552
6553 if (tv->vval.v_list == NULL)
6554 return NULL;
6555 ga_init2(&ga, (int)sizeof(char), 80);
6556 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006557 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 {
6559 vim_free(ga.ga_data);
6560 return NULL;
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 ga_append(&ga, ']');
6563 ga_append(&ga, NUL);
6564 return (char_u *)ga.ga_data;
6565}
6566
6567/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006568 * Join list "l" into a string in "*gap", using separator "sep".
6569 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006570 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006571 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006572 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006573list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006574 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006576 char_u *sep;
6577 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006579{
6580 int first = TRUE;
6581 char_u *tofree;
6582 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006584 char_u *s;
6585
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006587 {
6588 if (first)
6589 first = FALSE;
6590 else
6591 ga_concat(gap, sep);
6592
6593 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006594 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006595 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006596 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006597 if (s != NULL)
6598 ga_concat(gap, s);
6599 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 if (s == NULL)
6601 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006602 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006603 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006605}
6606
6607/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006608 * Garbage collection for lists and dictionaries.
6609 *
6610 * We use reference counts to be able to free most items right away when they
6611 * are no longer used. But for composite items it's possible that it becomes
6612 * unused while the reference count is > 0: When there is a recursive
6613 * reference. Example:
6614 * :let l = [1, 2, 3]
6615 * :let d = {9: l}
6616 * :let l[1] = d
6617 *
6618 * Since this is quite unusual we handle this with garbage collection: every
6619 * once in a while find out which lists and dicts are not referenced from any
6620 * variable.
6621 *
6622 * Here is a good reference text about garbage collection (refers to Python
6623 * but it applies to all reference-counting mechanisms):
6624 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006625 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006626
6627/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006628 * Do garbage collection for lists and dicts.
6629 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006630 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006631 int
6632garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006633{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006634 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006635 buf_T *buf;
6636 win_T *wp;
6637 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006638 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006639 int did_free;
6640 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006641#ifdef FEAT_WINDOWS
6642 tabpage_T *tp;
6643#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006644
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006645 /* Only do this once. */
6646 want_garbage_collect = FALSE;
6647 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006648 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006649
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006650 /* We advance by two because we add one for items referenced through
6651 * previous_funccal. */
6652 current_copyID += COPYID_INC;
6653 copyID = current_copyID;
6654
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006655 /*
6656 * 1. Go through all accessible variables and mark all lists and dicts
6657 * with copyID.
6658 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006659
6660 /* Don't free variables in the previous_funccal list unless they are only
6661 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006662 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006663 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6664 {
6665 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6666 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6667 }
6668
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006669 /* script-local variables */
6670 for (i = 1; i <= ga_scripts.ga_len; ++i)
6671 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6672
6673 /* buffer-local variables */
6674 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6675 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6676
6677 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006678 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006679 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6680
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006681#ifdef FEAT_WINDOWS
6682 /* tabpage-local variables */
6683 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6684 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6685#endif
6686
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006687 /* global variables */
6688 set_ref_in_ht(&globvarht, copyID);
6689
6690 /* function-local variables */
6691 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6692 {
6693 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6694 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6695 }
6696
Bram Moolenaard812df62008-11-09 12:46:09 +00006697 /* v: vars */
6698 set_ref_in_ht(&vimvarht, copyID);
6699
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006700 /*
6701 * 2. Free lists and dictionaries that are not referenced.
6702 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006703 did_free = free_unref_items(copyID);
6704
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006705 /*
6706 * 3. Check if any funccal can be freed now.
6707 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006708 for (pfc = &previous_funccal; *pfc != NULL; )
6709 {
6710 if (can_free_funccal(*pfc, copyID))
6711 {
6712 fc = *pfc;
6713 *pfc = fc->caller;
6714 free_funccal(fc, TRUE);
6715 did_free = TRUE;
6716 did_free_funccal = TRUE;
6717 }
6718 else
6719 pfc = &(*pfc)->caller;
6720 }
6721 if (did_free_funccal)
6722 /* When a funccal was freed some more items might be garbage
6723 * collected, so run again. */
6724 (void)garbage_collect();
6725
6726 return did_free;
6727}
6728
6729/*
6730 * Free lists and dictionaries that are no longer referenced.
6731 */
6732 static int
6733free_unref_items(copyID)
6734 int copyID;
6735{
6736 dict_T *dd;
6737 list_T *ll;
6738 int did_free = FALSE;
6739
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006740 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006741 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742 */
6743 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006744 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006746 /* Free the Dictionary and ordinary items it contains, but don't
6747 * recurse into Lists and Dictionaries, they will be in the list
6748 * of dicts or list of lists. */
6749 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006750 did_free = TRUE;
6751
6752 /* restart, next dict may also have been freed */
6753 dd = first_dict;
6754 }
6755 else
6756 dd = dd->dv_used_next;
6757
6758 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006759 * Go through the list of lists and free items without the copyID.
6760 * But don't free a list that has a watcher (used in a for loop), these
6761 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006762 */
6763 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006764 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6765 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006767 /* Free the List and ordinary items it contains, but don't recurse
6768 * into Lists and Dictionaries, they will be in the list of dicts
6769 * or list of lists. */
6770 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006771 did_free = TRUE;
6772
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006773 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 ll = first_list;
6775 }
6776 else
6777 ll = ll->lv_used_next;
6778
6779 return did_free;
6780}
6781
6782/*
6783 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6784 */
6785 static void
6786set_ref_in_ht(ht, copyID)
6787 hashtab_T *ht;
6788 int copyID;
6789{
6790 int todo;
6791 hashitem_T *hi;
6792
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006793 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794 for (hi = ht->ht_array; todo > 0; ++hi)
6795 if (!HASHITEM_EMPTY(hi))
6796 {
6797 --todo;
6798 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6799 }
6800}
6801
6802/*
6803 * Mark all lists and dicts referenced through list "l" with "copyID".
6804 */
6805 static void
6806set_ref_in_list(l, copyID)
6807 list_T *l;
6808 int copyID;
6809{
6810 listitem_T *li;
6811
6812 for (li = l->lv_first; li != NULL; li = li->li_next)
6813 set_ref_in_item(&li->li_tv, copyID);
6814}
6815
6816/*
6817 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6818 */
6819 static void
6820set_ref_in_item(tv, copyID)
6821 typval_T *tv;
6822 int copyID;
6823{
6824 dict_T *dd;
6825 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006826
6827 switch (tv->v_type)
6828 {
6829 case VAR_DICT:
6830 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006831 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 /* Didn't see this dict yet. */
6834 dd->dv_copyID = copyID;
6835 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006838
6839 case VAR_LIST:
6840 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006841 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 /* Didn't see this list yet. */
6844 ll->lv_copyID = copyID;
6845 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006848 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006849 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850}
6851
6852/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006853 * Allocate an empty header for a dictionary.
6854 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006855 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006856dict_alloc()
6857{
Bram Moolenaar33570922005-01-25 22:26:29 +00006858 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006859
Bram Moolenaar33570922005-01-25 22:26:29 +00006860 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006861 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006862 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006863 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 if (first_dict != NULL)
6865 first_dict->dv_used_prev = d;
6866 d->dv_used_next = first_dict;
6867 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006868 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869
Bram Moolenaar33570922005-01-25 22:26:29 +00006870 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006871 d->dv_lock = 0;
6872 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006873 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006874 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006875 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006876}
6877
6878/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006879 * Allocate an empty dict for a return value.
6880 * Returns OK or FAIL.
6881 */
6882 static int
6883rettv_dict_alloc(rettv)
6884 typval_T *rettv;
6885{
6886 dict_T *d = dict_alloc();
6887
6888 if (d == NULL)
6889 return FAIL;
6890
6891 rettv->vval.v_dict = d;
6892 rettv->v_type = VAR_DICT;
6893 ++d->dv_refcount;
6894 return OK;
6895}
6896
6897
6898/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006899 * Unreference a Dictionary: decrement the reference count and free it when it
6900 * becomes zero.
6901 */
Bram Moolenaar82139082011-09-14 16:52:09 +02006902 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006903dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006904 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006905{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006906 if (d != NULL && --d->dv_refcount <= 0)
6907 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006908}
6909
6910/*
6911 * Free a Dictionary, including all items it contains.
6912 * Ignores the reference count.
6913 */
6914 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006915dict_free(d, recurse)
6916 dict_T *d;
6917 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006918{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006919 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006920 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006921 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006922
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 /* Remove the dict from the list of dicts for garbage collection. */
6924 if (d->dv_used_prev == NULL)
6925 first_dict = d->dv_used_next;
6926 else
6927 d->dv_used_prev->dv_used_next = d->dv_used_next;
6928 if (d->dv_used_next != NULL)
6929 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6930
6931 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006932 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006933 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006934 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006935 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006936 if (!HASHITEM_EMPTY(hi))
6937 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006938 /* Remove the item before deleting it, just in case there is
6939 * something recursive causing trouble. */
6940 di = HI2DI(hi);
6941 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006942 if (recurse || (di->di_tv.v_type != VAR_LIST
6943 && di->di_tv.v_type != VAR_DICT))
6944 clear_tv(&di->di_tv);
6945 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006946 --todo;
6947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006950 vim_free(d);
6951}
6952
6953/*
6954 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006955 * The "key" is copied to the new item.
6956 * Note that the value of the item "di_tv" still needs to be initialized!
6957 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006958 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006959 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006960dictitem_alloc(key)
6961 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006962{
Bram Moolenaar33570922005-01-25 22:26:29 +00006963 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006964
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006965 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006966 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006967 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006968 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006969 di->di_flags = 0;
6970 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006971 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972}
6973
6974/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975 * Make a copy of a Dictionary item.
6976 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006978dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006979 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006980{
Bram Moolenaar33570922005-01-25 22:26:29 +00006981 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006982
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006983 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6984 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006985 if (di != NULL)
6986 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006987 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 copy_tv(&org->di_tv, &di->di_tv);
6990 }
6991 return di;
6992}
6993
6994/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006995 * Remove item "item" from Dictionary "dict" and free it.
6996 */
6997 static void
6998dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006999 dict_T *dict;
7000 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007001{
Bram Moolenaar33570922005-01-25 22:26:29 +00007002 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007003
Bram Moolenaar33570922005-01-25 22:26:29 +00007004 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007005 if (HASHITEM_EMPTY(hi))
7006 EMSG2(_(e_intern2), "dictitem_remove()");
7007 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007009 dictitem_free(item);
7010}
7011
7012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013 * Free a dict item. Also clears the value.
7014 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007015 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007016dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007017 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007018{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019 clear_tv(&item->di_tv);
7020 vim_free(item);
7021}
7022
7023/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7025 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007026 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007027 * Returns NULL when out of memory.
7028 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007030dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007032 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007033 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007034{
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 dict_T *copy;
7036 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007037 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039
7040 if (orig == NULL)
7041 return NULL;
7042
7043 copy = dict_alloc();
7044 if (copy != NULL)
7045 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007046 if (copyID != 0)
7047 {
7048 orig->dv_copyID = copyID;
7049 orig->dv_copydict = copy;
7050 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007051 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007052 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007053 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007055 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007056 --todo;
7057
7058 di = dictitem_alloc(hi->hi_key);
7059 if (di == NULL)
7060 break;
7061 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007062 {
7063 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7064 copyID) == FAIL)
7065 {
7066 vim_free(di);
7067 break;
7068 }
7069 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007070 else
7071 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7072 if (dict_add(copy, di) == FAIL)
7073 {
7074 dictitem_free(di);
7075 break;
7076 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007077 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007078 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007079
Bram Moolenaare9a41262005-01-15 22:18:47 +00007080 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007081 if (todo > 0)
7082 {
7083 dict_unref(copy);
7084 copy = NULL;
7085 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007086 }
7087
7088 return copy;
7089}
7090
7091/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007093 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007095 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 dict_T *d;
7098 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099{
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101}
7102
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007104 * Add a number or string entry to dictionary "d".
7105 * When "str" is NULL use number "nr", otherwise use "str".
7106 * Returns FAIL when out of memory and when key already exists.
7107 */
7108 int
7109dict_add_nr_str(d, key, nr, str)
7110 dict_T *d;
7111 char *key;
7112 long nr;
7113 char_u *str;
7114{
7115 dictitem_T *item;
7116
7117 item = dictitem_alloc((char_u *)key);
7118 if (item == NULL)
7119 return FAIL;
7120 item->di_tv.v_lock = 0;
7121 if (str == NULL)
7122 {
7123 item->di_tv.v_type = VAR_NUMBER;
7124 item->di_tv.vval.v_number = nr;
7125 }
7126 else
7127 {
7128 item->di_tv.v_type = VAR_STRING;
7129 item->di_tv.vval.v_string = vim_strsave(str);
7130 }
7131 if (dict_add(d, item) == FAIL)
7132 {
7133 dictitem_free(item);
7134 return FAIL;
7135 }
7136 return OK;
7137}
7138
7139/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007140 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007141 * Returns FAIL when out of memory and when key already exists.
7142 */
7143 int
7144dict_add_list(d, key, list)
7145 dict_T *d;
7146 char *key;
7147 list_T *list;
7148{
7149 dictitem_T *item;
7150
7151 item = dictitem_alloc((char_u *)key);
7152 if (item == NULL)
7153 return FAIL;
7154 item->di_tv.v_lock = 0;
7155 item->di_tv.v_type = VAR_LIST;
7156 item->di_tv.vval.v_list = list;
7157 if (dict_add(d, item) == FAIL)
7158 {
7159 dictitem_free(item);
7160 return FAIL;
7161 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007162 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007163 return OK;
7164}
7165
7166/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 * Get the number of items in a Dictionary.
7168 */
7169 static long
7170dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007172{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 if (d == NULL)
7174 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007175 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176}
7177
7178/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 * Find item "key[len]" in Dictionary "d".
7180 * If "len" is negative use strlen(key).
7181 * Returns NULL when not found.
7182 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007183 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186 char_u *key;
7187 int len;
7188{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007189#define AKEYLEN 200
7190 char_u buf[AKEYLEN];
7191 char_u *akey;
7192 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 if (len < 0)
7196 akey = key;
7197 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007198 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 tofree = akey = vim_strnsave(key, len);
7200 if (akey == NULL)
7201 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007202 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 else
7204 {
7205 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007206 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 akey = buf;
7208 }
7209
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211 vim_free(tofree);
7212 if (HASHITEM_EMPTY(hi))
7213 return NULL;
7214 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215}
7216
7217/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007218 * Get a string item from a dictionary.
7219 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007220 * Returns NULL if the entry doesn't exist or out of memory.
7221 */
7222 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007223get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007224 dict_T *d;
7225 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007226 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007227{
7228 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007229 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007230
7231 di = dict_find(d, key, -1);
7232 if (di == NULL)
7233 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007234 s = get_tv_string(&di->di_tv);
7235 if (save && s != NULL)
7236 s = vim_strsave(s);
7237 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007238}
7239
7240/*
7241 * Get a number item from a dictionary.
7242 * Returns 0 if the entry doesn't exist or out of memory.
7243 */
7244 long
7245get_dict_number(d, key)
7246 dict_T *d;
7247 char_u *key;
7248{
7249 dictitem_T *di;
7250
7251 di = dict_find(d, key, -1);
7252 if (di == NULL)
7253 return 0;
7254 return get_tv_number(&di->di_tv);
7255}
7256
7257/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 * Return an allocated string with the string representation of a Dictionary.
7259 * May return NULL.
7260 */
7261 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007262dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007263 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007264 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265{
7266 garray_T ga;
7267 int first = TRUE;
7268 char_u *tofree;
7269 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 return NULL;
7277 ga_init2(&ga, (int)sizeof(char), 80);
7278 ga_append(&ga, '{');
7279
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007280 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007281 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 --todo;
7286
7287 if (first)
7288 first = FALSE;
7289 else
7290 ga_concat(&ga, (char_u *)", ");
7291
7292 tofree = string_quote(hi->hi_key, FALSE);
7293 if (tofree != NULL)
7294 {
7295 ga_concat(&ga, tofree);
7296 vim_free(tofree);
7297 }
7298 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007299 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 if (s != NULL)
7301 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007303 if (s == NULL)
7304 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007307 if (todo > 0)
7308 {
7309 vim_free(ga.ga_data);
7310 return NULL;
7311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312
7313 ga_append(&ga, '}');
7314 ga_append(&ga, NUL);
7315 return (char_u *)ga.ga_data;
7316}
7317
7318/*
7319 * Allocate a variable for a Dictionary and fill it from "*arg".
7320 * Return OK or FAIL. Returns NOTDONE for {expr}.
7321 */
7322 static int
7323get_dict_tv(arg, rettv, evaluate)
7324 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 int evaluate;
7327{
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 dict_T *d = NULL;
7329 typval_T tvkey;
7330 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007331 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335
7336 /*
7337 * First check if it's not a curly-braces thing: {expr}.
7338 * Must do this without evaluating, otherwise a function may be called
7339 * twice. Unfortunately this means we need to call eval1() twice for the
7340 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 if (*start != '}')
7344 {
7345 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7346 return FAIL;
7347 if (*start == '}')
7348 return NOTDONE;
7349 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350
7351 if (evaluate)
7352 {
7353 d = dict_alloc();
7354 if (d == NULL)
7355 return FAIL;
7356 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 tvkey.v_type = VAR_UNKNOWN;
7358 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359
7360 *arg = skipwhite(*arg + 1);
7361 while (**arg != '}' && **arg != NUL)
7362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364 goto failret;
7365 if (**arg != ':')
7366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007367 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369 goto failret;
7370 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007371 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007373 key = get_tv_string_buf_chk(&tvkey, buf);
7374 if (key == NULL || *key == NUL)
7375 {
7376 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7377 if (key != NULL)
7378 EMSG(_(e_emptykey));
7379 clear_tv(&tvkey);
7380 goto failret;
7381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383
7384 *arg = skipwhite(*arg + 1);
7385 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7386 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007387 if (evaluate)
7388 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 goto failret;
7390 }
7391 if (evaluate)
7392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 if (item != NULL)
7395 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007396 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 clear_tv(&tv);
7399 goto failret;
7400 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 item = dictitem_alloc(key);
7402 clear_tv(&tvkey);
7403 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007406 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007407 if (dict_add(d, item) == FAIL)
7408 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 }
7410 }
7411
7412 if (**arg == '}')
7413 break;
7414 if (**arg != ',')
7415 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007416 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 goto failret;
7418 }
7419 *arg = skipwhite(*arg + 1);
7420 }
7421
7422 if (**arg != '}')
7423 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007424 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425failret:
7426 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007427 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 return FAIL;
7429 }
7430
7431 *arg = skipwhite(*arg + 1);
7432 if (evaluate)
7433 {
7434 rettv->v_type = VAR_DICT;
7435 rettv->vval.v_dict = d;
7436 ++d->dv_refcount;
7437 }
7438
7439 return OK;
7440}
7441
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007443 * Return a string with the string representation of a variable.
7444 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007445 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007446 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007447 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007448 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007449 */
7450 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007453 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007454 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 static int recurse = 0;
7458 char_u *r = NULL;
7459
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007461 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007462 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007463 *tofree = NULL;
7464 return NULL;
7465 }
7466 ++recurse;
7467
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007468 switch (tv->v_type)
7469 {
7470 case VAR_FUNC:
7471 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 r = tv->vval.v_string;
7473 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007475 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007476 if (tv->vval.v_list == NULL)
7477 {
7478 *tofree = NULL;
7479 r = NULL;
7480 }
7481 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7482 {
7483 *tofree = NULL;
7484 r = (char_u *)"[...]";
7485 }
7486 else
7487 {
7488 tv->vval.v_list->lv_copyID = copyID;
7489 *tofree = list2string(tv, copyID);
7490 r = *tofree;
7491 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007492 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007493
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007495 if (tv->vval.v_dict == NULL)
7496 {
7497 *tofree = NULL;
7498 r = NULL;
7499 }
7500 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7501 {
7502 *tofree = NULL;
7503 r = (char_u *)"{...}";
7504 }
7505 else
7506 {
7507 tv->vval.v_dict->dv_copyID = copyID;
7508 *tofree = dict2string(tv, copyID);
7509 r = *tofree;
7510 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007513 case VAR_STRING:
7514 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515 *tofree = NULL;
7516 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007517 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007518
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007519#ifdef FEAT_FLOAT
7520 case VAR_FLOAT:
7521 *tofree = NULL;
7522 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7523 r = numbuf;
7524 break;
7525#endif
7526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007527 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007528 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007530 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531
7532 --recurse;
7533 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534}
7535
7536/*
7537 * Return a string with the string representation of a variable.
7538 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7539 * "numbuf" is used for a number.
7540 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007541 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007542 */
7543 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007544tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007546 char_u **tofree;
7547 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007548 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007549{
7550 switch (tv->v_type)
7551 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007552 case VAR_FUNC:
7553 *tofree = string_quote(tv->vval.v_string, TRUE);
7554 return *tofree;
7555 case VAR_STRING:
7556 *tofree = string_quote(tv->vval.v_string, FALSE);
7557 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007558#ifdef FEAT_FLOAT
7559 case VAR_FLOAT:
7560 *tofree = NULL;
7561 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7562 return numbuf;
7563#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007564 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007565 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007568 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007569 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007570 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007571 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007572}
7573
7574/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 * Return string "str" in ' quotes, doubling ' characters.
7576 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007578 */
7579 static char_u *
7580string_quote(str, function)
7581 char_u *str;
7582 int function;
7583{
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007585 char_u *p, *r, *s;
7586
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 len = (function ? 13 : 3);
7588 if (str != NULL)
7589 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007590 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 for (p = str; *p != NUL; mb_ptr_adv(p))
7592 if (*p == '\'')
7593 ++len;
7594 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007595 s = r = alloc(len);
7596 if (r != NULL)
7597 {
7598 if (function)
7599 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007601 r += 10;
7602 }
7603 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 if (str != NULL)
7606 for (p = str; *p != NUL; )
7607 {
7608 if (*p == '\'')
7609 *r++ = '\'';
7610 MB_COPY_CHAR(p, r);
7611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007613 if (function)
7614 *r++ = ')';
7615 *r++ = NUL;
7616 }
7617 return s;
7618}
7619
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007620#ifdef FEAT_FLOAT
7621/*
7622 * Convert the string "text" to a floating point number.
7623 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7624 * this always uses a decimal point.
7625 * Returns the length of the text that was consumed.
7626 */
7627 static int
7628string2float(text, value)
7629 char_u *text;
7630 float_T *value; /* result stored here */
7631{
7632 char *s = (char *)text;
7633 float_T f;
7634
7635 f = strtod(s, &s);
7636 *value = f;
7637 return (int)((char_u *)s - text);
7638}
7639#endif
7640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 * Get the value of an environment variable.
7643 * "arg" is pointing to the '$'. It is advanced to after the name.
7644 * If the environment variable was not set, silently assume it is empty.
7645 * Always return OK.
7646 */
7647 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007648get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 int evaluate;
7652{
7653 char_u *string = NULL;
7654 int len;
7655 int cc;
7656 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007657 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
7659 ++*arg;
7660 name = *arg;
7661 len = get_env_len(arg);
7662 if (evaluate)
7663 {
7664 if (len != 0)
7665 {
7666 cc = name[len];
7667 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007668 /* first try vim_getenv(), fast for normal environment vars */
7669 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007671 {
7672 if (!mustfree)
7673 string = vim_strsave(string);
7674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 else
7676 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007677 if (mustfree)
7678 vim_free(string);
7679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 /* next try expanding things like $VIM and ${HOME} */
7681 string = expand_env_save(name - 1);
7682 if (string != NULL && *string == '$')
7683 {
7684 vim_free(string);
7685 string = NULL;
7686 }
7687 }
7688 name[len] = cc;
7689 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007690 rettv->v_type = VAR_STRING;
7691 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693
7694 return OK;
7695}
7696
7697/*
7698 * Array with names and number of arguments of all internal functions
7699 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7700 */
7701static struct fst
7702{
7703 char *f_name; /* function name */
7704 char f_min_argc; /* minimal number of arguments */
7705 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007707 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708} functions[] =
7709{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007710#ifdef FEAT_FLOAT
7711 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007712 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007713#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007714 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 {"append", 2, 2, f_append},
7716 {"argc", 0, 0, f_argc},
7717 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007718 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007719#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007720 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007721 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007722 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007725 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {"bufexists", 1, 1, f_bufexists},
7727 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7728 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7729 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7730 {"buflisted", 1, 1, f_buflisted},
7731 {"bufloaded", 1, 1, f_bufloaded},
7732 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007733 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {"bufwinnr", 1, 1, f_bufwinnr},
7735 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007736 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007737 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007738#ifdef FEAT_FLOAT
7739 {"ceil", 1, 1, f_ceil},
7740#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007741 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {"char2nr", 1, 1, f_char2nr},
7743 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007744 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007746#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007747 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007748 {"complete_add", 1, 1, f_complete_add},
7749 {"complete_check", 0, 0, f_complete_check},
7750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007753#ifdef FEAT_FLOAT
7754 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007755 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007756#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007757 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007759 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007760 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {"delete", 1, 1, f_delete},
7762 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007763 {"diff_filler", 1, 1, f_diff_filler},
7764 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007765 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007767 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"eventhandler", 0, 0, f_eventhandler},
7769 {"executable", 1, 1, f_executable},
7770 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007771#ifdef FEAT_FLOAT
7772 {"exp", 1, 1, f_exp},
7773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007775 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007776 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7778 {"filereadable", 1, 1, f_filereadable},
7779 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007780 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007781 {"finddir", 1, 3, f_finddir},
7782 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007783#ifdef FEAT_FLOAT
7784 {"float2nr", 1, 1, f_float2nr},
7785 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007786 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007787#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007788 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {"fnamemodify", 2, 2, f_fnamemodify},
7790 {"foldclosed", 1, 1, f_foldclosed},
7791 {"foldclosedend", 1, 1, f_foldclosedend},
7792 {"foldlevel", 1, 1, f_foldlevel},
7793 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007794 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007796 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007797 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007798 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007799 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {"getbufvar", 2, 2, f_getbufvar},
7801 {"getchar", 0, 1, f_getchar},
7802 {"getcharmod", 0, 0, f_getcharmod},
7803 {"getcmdline", 0, 0, f_getcmdline},
7804 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007805 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007807 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007808 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 {"getfsize", 1, 1, f_getfsize},
7810 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007811 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007812 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007813 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007814 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007815 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007816 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007817 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007818 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007820 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007821 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {"getwinposx", 0, 0, f_getwinposx},
7823 {"getwinposy", 0, 0, f_getwinposy},
7824 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007825 {"glob", 1, 2, f_glob},
7826 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007829 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007830 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7832 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7833 {"histadd", 2, 2, f_histadd},
7834 {"histdel", 1, 2, f_histdel},
7835 {"histget", 1, 2, f_histget},
7836 {"histnr", 1, 1, f_histnr},
7837 {"hlID", 1, 1, f_hlID},
7838 {"hlexists", 1, 1, f_hlexists},
7839 {"hostname", 0, 0, f_hostname},
7840 {"iconv", 3, 3, f_iconv},
7841 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007842 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007843 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007845 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"inputrestore", 0, 0, f_inputrestore},
7847 {"inputsave", 0, 0, f_inputsave},
7848 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007849 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007851 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007852 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007853 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007854 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007856 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {"libcall", 3, 3, f_libcall},
7858 {"libcallnr", 3, 3, f_libcallnr},
7859 {"line", 1, 1, f_line},
7860 {"line2byte", 1, 1, f_line2byte},
7861 {"lispindent", 1, 1, f_lispindent},
7862 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007863#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007864 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007865 {"log10", 1, 1, f_log10},
7866#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007867 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007868 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007869 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007870 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007871 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007872 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007873 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007874 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007875 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007876 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007877 {"max", 1, 1, f_max},
7878 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007879#ifdef vim_mkdir
7880 {"mkdir", 1, 3, f_mkdir},
7881#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007883#ifdef FEAT_MZSCHEME
7884 {"mzeval", 1, 1, f_mzeval},
7885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"nextnonblank", 1, 1, f_nextnonblank},
7887 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007888 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889#ifdef FEAT_FLOAT
7890 {"pow", 2, 2, f_pow},
7891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007893 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007894 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007896 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007897 {"reltime", 0, 2, f_reltime},
7898 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"remote_expr", 2, 3, f_remote_expr},
7900 {"remote_foreground", 1, 1, f_remote_foreground},
7901 {"remote_peek", 1, 2, f_remote_peek},
7902 {"remote_read", 1, 1, f_remote_read},
7903 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007904 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007906 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007908 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909#ifdef FEAT_FLOAT
7910 {"round", 1, 1, f_round},
7911#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007912 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007913 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007914 {"searchpair", 3, 7, f_searchpair},
7915 {"searchpairpos", 3, 7, f_searchpairpos},
7916 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {"server2client", 2, 2, f_server2client},
7918 {"serverlist", 0, 0, f_serverlist},
7919 {"setbufvar", 3, 3, f_setbufvar},
7920 {"setcmdpos", 1, 1, f_setcmdpos},
7921 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007922 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007923 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007924 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007925 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007927 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007928 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007930 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007932#ifdef FEAT_FLOAT
7933 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007934 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02007936 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007937 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007938 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007939 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007940 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007941#ifdef FEAT_FLOAT
7942 {"sqrt", 1, 1, f_sqrt},
7943 {"str2float", 1, 1, f_str2float},
7944#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007945 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007946 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007947 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948#ifdef HAVE_STRFTIME
7949 {"strftime", 1, 2, f_strftime},
7950#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007952 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"strlen", 1, 1, f_strlen},
7954 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007955 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007957 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {"submatch", 1, 1, f_submatch},
7959 {"substitute", 4, 4, f_substitute},
7960 {"synID", 3, 3, f_synID},
7961 {"synIDattr", 2, 3, f_synIDattr},
7962 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007963 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007964 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007965 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007966 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007967 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007968 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007969 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007970 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007971#ifdef FEAT_FLOAT
7972 {"tan", 1, 1, f_tan},
7973 {"tanh", 1, 1, f_tanh},
7974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007976 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"tolower", 1, 1, f_tolower},
7978 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007979 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007980#ifdef FEAT_FLOAT
7981 {"trunc", 1, 1, f_trunc},
7982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007984 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007985 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007986 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"virtcol", 1, 1, f_virtcol},
7988 {"visualmode", 0, 1, f_visualmode},
7989 {"winbufnr", 1, 1, f_winbufnr},
7990 {"wincol", 0, 0, f_wincol},
7991 {"winheight", 1, 1, f_winheight},
7992 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007993 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007995 {"winrestview", 1, 1, f_winrestview},
7996 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007998 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999};
8000
8001#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8002
8003/*
8004 * Function given to ExpandGeneric() to obtain the list of internal
8005 * or user defined function names.
8006 */
8007 char_u *
8008get_function_name(xp, idx)
8009 expand_T *xp;
8010 int idx;
8011{
8012 static int intidx = -1;
8013 char_u *name;
8014
8015 if (idx == 0)
8016 intidx = -1;
8017 if (intidx < 0)
8018 {
8019 name = get_user_func_name(xp, idx);
8020 if (name != NULL)
8021 return name;
8022 }
8023 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8024 {
8025 STRCPY(IObuff, functions[intidx].f_name);
8026 STRCAT(IObuff, "(");
8027 if (functions[intidx].f_max_argc == 0)
8028 STRCAT(IObuff, ")");
8029 return IObuff;
8030 }
8031
8032 return NULL;
8033}
8034
8035/*
8036 * Function given to ExpandGeneric() to obtain the list of internal or
8037 * user defined variable or function names.
8038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 char_u *
8040get_expr_name(xp, idx)
8041 expand_T *xp;
8042 int idx;
8043{
8044 static int intidx = -1;
8045 char_u *name;
8046
8047 if (idx == 0)
8048 intidx = -1;
8049 if (intidx < 0)
8050 {
8051 name = get_function_name(xp, idx);
8052 if (name != NULL)
8053 return name;
8054 }
8055 return get_user_var_name(xp, ++intidx);
8056}
8057
8058#endif /* FEAT_CMDL_COMPL */
8059
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008060#if defined(EBCDIC) || defined(PROTO)
8061/*
8062 * Compare struct fst by function name.
8063 */
8064 static int
8065compare_func_name(s1, s2)
8066 const void *s1;
8067 const void *s2;
8068{
8069 struct fst *p1 = (struct fst *)s1;
8070 struct fst *p2 = (struct fst *)s2;
8071
8072 return STRCMP(p1->f_name, p2->f_name);
8073}
8074
8075/*
8076 * Sort the function table by function name.
8077 * The sorting of the table above is ASCII dependant.
8078 * On machines using EBCDIC we have to sort it.
8079 */
8080 static void
8081sortFunctions()
8082{
8083 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8084
8085 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8086}
8087#endif
8088
8089
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090/*
8091 * Find internal function in table above.
8092 * Return index, or -1 if not found
8093 */
8094 static int
8095find_internal_func(name)
8096 char_u *name; /* name of the function */
8097{
8098 int first = 0;
8099 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8100 int cmp;
8101 int x;
8102
8103 /*
8104 * Find the function name in the table. Binary search.
8105 */
8106 while (first <= last)
8107 {
8108 x = first + ((unsigned)(last - first) >> 1);
8109 cmp = STRCMP(name, functions[x].f_name);
8110 if (cmp < 0)
8111 last = x - 1;
8112 else if (cmp > 0)
8113 first = x + 1;
8114 else
8115 return x;
8116 }
8117 return -1;
8118}
8119
8120/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008121 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8122 * name it contains, otherwise return "name".
8123 */
8124 static char_u *
8125deref_func_name(name, lenp)
8126 char_u *name;
8127 int *lenp;
8128{
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 int cc;
8131
8132 cc = name[*lenp];
8133 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008134 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008136 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008137 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008139 {
8140 *lenp = 0;
8141 return (char_u *)""; /* just in case */
8142 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008143 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008144 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 }
8146
8147 return name;
8148}
8149
8150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 * Allocate a variable for the result of a function.
8152 * Return OK or FAIL.
8153 */
8154 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008155get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8156 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 char_u *name; /* name of the function */
8158 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 char_u **arg; /* argument, pointing to the '(' */
8161 linenr_T firstline; /* first line of range */
8162 linenr_T lastline; /* last line of range */
8163 int *doesrange; /* return: function handled range */
8164 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008165 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
8167 char_u *argp;
8168 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008169 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 int argcount = 0; /* number of arguments found */
8171
8172 /*
8173 * Get the arguments.
8174 */
8175 argp = *arg;
8176 while (argcount < MAX_FUNC_ARGS)
8177 {
8178 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8179 if (*argp == ')' || *argp == ',' || *argp == NUL)
8180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8182 {
8183 ret = FAIL;
8184 break;
8185 }
8186 ++argcount;
8187 if (*argp != ',')
8188 break;
8189 }
8190 if (*argp == ')')
8191 ++argp;
8192 else
8193 ret = FAIL;
8194
8195 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008196 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008197 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008199 {
8200 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008201 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008202 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008203 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205
8206 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008207 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208
8209 *arg = skipwhite(argp);
8210 return ret;
8211}
8212
8213
8214/*
8215 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008216 * Return OK when the function can't be called, FAIL otherwise.
8217 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 */
8219 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008220call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008221 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008222 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008226 typval_T *argvars; /* vars for arguments, must have "argcount"
8227 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 linenr_T firstline; /* first line of range */
8229 linenr_T lastline; /* last line of range */
8230 int *doesrange; /* return: function handled range */
8231 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008232 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235#define ERROR_UNKNOWN 0
8236#define ERROR_TOOMANY 1
8237#define ERROR_TOOFEW 2
8238#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008239#define ERROR_DICT 4
8240#define ERROR_NONE 5
8241#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 int error = ERROR_NONE;
8243 int i;
8244 int llen;
8245 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246#define FLEN_FIXED 40
8247 char_u fname_buf[FLEN_FIXED + 1];
8248 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008249 char_u *name;
8250
8251 /* Make a copy of the name, if it comes from a funcref variable it could
8252 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008253 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008254 if (name == NULL)
8255 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256
8257 /*
8258 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8259 * Change <SNR>123_name() to K_SNR 123_name().
8260 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 llen = eval_fname_script(name);
8263 if (llen > 0)
8264 {
8265 fname_buf[0] = K_SPECIAL;
8266 fname_buf[1] = KS_EXTRA;
8267 fname_buf[2] = (int)KE_SNR;
8268 i = 3;
8269 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8270 {
8271 if (current_SID <= 0)
8272 error = ERROR_SCRIPT;
8273 else
8274 {
8275 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8276 i = (int)STRLEN(fname_buf);
8277 }
8278 }
8279 if (i + STRLEN(name + llen) < FLEN_FIXED)
8280 {
8281 STRCPY(fname_buf + i, name + llen);
8282 fname = fname_buf;
8283 }
8284 else
8285 {
8286 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8287 if (fname == NULL)
8288 error = ERROR_OTHER;
8289 else
8290 {
8291 mch_memmove(fname, fname_buf, (size_t)i);
8292 STRCPY(fname + i, name + llen);
8293 }
8294 }
8295 }
8296 else
8297 fname = name;
8298
8299 *doesrange = FALSE;
8300
8301
8302 /* execute the function if no errors detected and executing */
8303 if (evaluate && error == ERROR_NONE)
8304 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008305 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8306 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 error = ERROR_UNKNOWN;
8308
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008309 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {
8311 /*
8312 * User defined function.
8313 */
8314 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008315
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008317 /* Trigger FuncUndefined event, may load the function. */
8318 if (fp == NULL
8319 && apply_autocmds(EVENT_FUNCUNDEFINED,
8320 fname, fname, TRUE, NULL)
8321 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008323 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 fp = find_func(fname);
8325 }
8326#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008327 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008328 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008329 {
8330 /* loaded a package, search for the function again */
8331 fp = find_func(fname);
8332 }
8333
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 if (fp != NULL)
8335 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008336 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008338 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008340 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008342 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008343 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 else
8345 {
8346 /*
8347 * Call the user function.
8348 * Save and restore search patterns, script variables and
8349 * redo buffer.
8350 */
8351 save_search_patterns();
8352 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008353 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008354 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008355 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008356 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8357 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8358 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008359 /* Function was unreferenced while being used, free it
8360 * now. */
8361 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 restoreRedobuff();
8363 restore_search_patterns();
8364 error = ERROR_NONE;
8365 }
8366 }
8367 }
8368 else
8369 {
8370 /*
8371 * Find the function name in the table, call its implementation.
8372 */
8373 i = find_internal_func(fname);
8374 if (i >= 0)
8375 {
8376 if (argcount < functions[i].f_min_argc)
8377 error = ERROR_TOOFEW;
8378 else if (argcount > functions[i].f_max_argc)
8379 error = ERROR_TOOMANY;
8380 else
8381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008382 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008383 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 error = ERROR_NONE;
8385 }
8386 }
8387 }
8388 /*
8389 * The function call (or "FuncUndefined" autocommand sequence) might
8390 * have been aborted by an error, an interrupt, or an explicitly thrown
8391 * exception that has not been caught so far. This situation can be
8392 * tested for by calling aborting(). For an error in an internal
8393 * function or for the "E132" error in call_user_func(), however, the
8394 * throw point at which the "force_abort" flag (temporarily reset by
8395 * emsg()) is normally updated has not been reached yet. We need to
8396 * update that flag first to make aborting() reliable.
8397 */
8398 update_force_abort();
8399 }
8400 if (error == ERROR_NONE)
8401 ret = OK;
8402
8403 /*
8404 * Report an error unless the argument evaluation or function call has been
8405 * cancelled due to an aborting error, an interrupt, or an exception.
8406 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008407 if (!aborting())
8408 {
8409 switch (error)
8410 {
8411 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008412 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008413 break;
8414 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008415 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 break;
8417 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008418 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008419 name);
8420 break;
8421 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008422 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008423 name);
8424 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008425 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008426 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 name);
8428 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008429 }
8430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (fname != name && fname != fname_buf)
8433 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008434 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
8436 return ret;
8437}
8438
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008439/*
8440 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008441 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008442 */
8443 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008444emsg_funcname(ermsg, name)
8445 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008446 char_u *name;
8447{
8448 char_u *p;
8449
8450 if (*name == K_SPECIAL)
8451 p = concat_str((char_u *)"<SNR>", name + 3);
8452 else
8453 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008454 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008455 if (p != name)
8456 vim_free(p);
8457}
8458
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008459/*
8460 * Return TRUE for a non-zero Number and a non-empty String.
8461 */
8462 static int
8463non_zero_arg(argvars)
8464 typval_T *argvars;
8465{
8466 return ((argvars[0].v_type == VAR_NUMBER
8467 && argvars[0].vval.v_number != 0)
8468 || (argvars[0].v_type == VAR_STRING
8469 && argvars[0].vval.v_string != NULL
8470 && *argvars[0].vval.v_string != NUL));
8471}
8472
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473/*********************************************
8474 * Implementation of the built-in functions
8475 */
8476
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008477#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008478static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8479
8480/*
8481 * Get the float value of "argvars[0]" into "f".
8482 * Returns FAIL when the argument is not a Number or Float.
8483 */
8484 static int
8485get_float_arg(argvars, f)
8486 typval_T *argvars;
8487 float_T *f;
8488{
8489 if (argvars[0].v_type == VAR_FLOAT)
8490 {
8491 *f = argvars[0].vval.v_float;
8492 return OK;
8493 }
8494 if (argvars[0].v_type == VAR_NUMBER)
8495 {
8496 *f = (float_T)argvars[0].vval.v_number;
8497 return OK;
8498 }
8499 EMSG(_("E808: Number or Float required"));
8500 return FAIL;
8501}
8502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008503/*
8504 * "abs(expr)" function
8505 */
8506 static void
8507f_abs(argvars, rettv)
8508 typval_T *argvars;
8509 typval_T *rettv;
8510{
8511 if (argvars[0].v_type == VAR_FLOAT)
8512 {
8513 rettv->v_type = VAR_FLOAT;
8514 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8515 }
8516 else
8517 {
8518 varnumber_T n;
8519 int error = FALSE;
8520
8521 n = get_tv_number_chk(&argvars[0], &error);
8522 if (error)
8523 rettv->vval.v_number = -1;
8524 else if (n > 0)
8525 rettv->vval.v_number = n;
8526 else
8527 rettv->vval.v_number = -n;
8528 }
8529}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008530
8531/*
8532 * "acos()" function
8533 */
8534 static void
8535f_acos(argvars, rettv)
8536 typval_T *argvars;
8537 typval_T *rettv;
8538{
8539 float_T f;
8540
8541 rettv->v_type = VAR_FLOAT;
8542 if (get_float_arg(argvars, &f) == OK)
8543 rettv->vval.v_float = acos(f);
8544 else
8545 rettv->vval.v_float = 0.0;
8546}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008547#endif
8548
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008550 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 */
8552 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008553f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008554 typval_T *argvars;
8555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556{
Bram Moolenaar33570922005-01-25 22:26:29 +00008557 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008562 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008563 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008564 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008566 }
8567 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008568 EMSG(_(e_listreq));
8569}
8570
8571/*
8572 * "append(lnum, string/list)" function
8573 */
8574 static void
8575f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 typval_T *argvars;
8577 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008578{
8579 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008580 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008581 list_T *l = NULL;
8582 listitem_T *li = NULL;
8583 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008584 long added = 0;
8585
Bram Moolenaar0d660222005-01-07 21:51:51 +00008586 lnum = get_tv_lnum(argvars);
8587 if (lnum >= 0
8588 && lnum <= curbuf->b_ml.ml_line_count
8589 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008590 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008591 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008592 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008593 l = argvars[1].vval.v_list;
8594 if (l == NULL)
8595 return;
8596 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008598 for (;;)
8599 {
8600 if (l == NULL)
8601 tv = &argvars[1]; /* append a string */
8602 else if (li == NULL)
8603 break; /* end of list */
8604 else
8605 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008606 line = get_tv_string_chk(tv);
8607 if (line == NULL) /* type error */
8608 {
8609 rettv->vval.v_number = 1; /* Failed */
8610 break;
8611 }
8612 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008613 ++added;
8614 if (l == NULL)
8615 break;
8616 li = li->li_next;
8617 }
8618
8619 appended_lines_mark(lnum, added);
8620 if (curwin->w_cursor.lnum > lnum)
8621 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008623 else
8624 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625}
8626
8627/*
8628 * "argc()" function
8629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008632 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008635 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636}
8637
8638/*
8639 * "argidx()" function
8640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008643 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647}
8648
8649/*
8650 * "argv(nr)" function
8651 */
8652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008653f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008654 typval_T *argvars;
8655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
8657 int idx;
8658
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008659 if (argvars[0].v_type != VAR_UNKNOWN)
8660 {
8661 idx = get_tv_number_chk(&argvars[0], NULL);
8662 if (idx >= 0 && idx < ARGCOUNT)
8663 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8664 else
8665 rettv->vval.v_string = NULL;
8666 rettv->v_type = VAR_STRING;
8667 }
8668 else if (rettv_list_alloc(rettv) == OK)
8669 for (idx = 0; idx < ARGCOUNT; ++idx)
8670 list_append_string(rettv->vval.v_list,
8671 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672}
8673
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008674#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008675/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008676 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008677 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008678 static void
8679f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008680 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008681 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008682{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008683 float_T f;
8684
8685 rettv->v_type = VAR_FLOAT;
8686 if (get_float_arg(argvars, &f) == OK)
8687 rettv->vval.v_float = asin(f);
8688 else
8689 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008690}
8691
8692/*
8693 * "atan()" function
8694 */
8695 static void
8696f_atan(argvars, rettv)
8697 typval_T *argvars;
8698 typval_T *rettv;
8699{
8700 float_T f;
8701
8702 rettv->v_type = VAR_FLOAT;
8703 if (get_float_arg(argvars, &f) == OK)
8704 rettv->vval.v_float = atan(f);
8705 else
8706 rettv->vval.v_float = 0.0;
8707}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008708
8709/*
8710 * "atan2()" function
8711 */
8712 static void
8713f_atan2(argvars, rettv)
8714 typval_T *argvars;
8715 typval_T *rettv;
8716{
8717 float_T fx, fy;
8718
8719 rettv->v_type = VAR_FLOAT;
8720 if (get_float_arg(argvars, &fx) == OK
8721 && get_float_arg(&argvars[1], &fy) == OK)
8722 rettv->vval.v_float = atan2(fx, fy);
8723 else
8724 rettv->vval.v_float = 0.0;
8725}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008726#endif
8727
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728/*
8729 * "browse(save, title, initdir, default)" function
8730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008732f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008733 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735{
8736#ifdef FEAT_BROWSE
8737 int save;
8738 char_u *title;
8739 char_u *initdir;
8740 char_u *defname;
8741 char_u buf[NUMBUFLEN];
8742 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008743 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 save = get_tv_number_chk(&argvars[0], &error);
8746 title = get_tv_string_chk(&argvars[1]);
8747 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8748 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008750 if (error || title == NULL || initdir == NULL || defname == NULL)
8751 rettv->vval.v_string = NULL;
8752 else
8753 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008754 do_browse(save ? BROWSE_SAVE : 0,
8755 title, defname, NULL, initdir, NULL, curbuf);
8756#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008758#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008760}
8761
8762/*
8763 * "browsedir(title, initdir)" function
8764 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008767 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008769{
8770#ifdef FEAT_BROWSE
8771 char_u *title;
8772 char_u *initdir;
8773 char_u buf[NUMBUFLEN];
8774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 title = get_tv_string_chk(&argvars[0]);
8776 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008778 if (title == NULL || initdir == NULL)
8779 rettv->vval.v_string = NULL;
8780 else
8781 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008782 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787}
8788
Bram Moolenaar33570922005-01-25 22:26:29 +00008789static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008790
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791/*
8792 * Find a buffer by number or exact name.
8793 */
8794 static buf_T *
8795find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008796 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
8798 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008800 if (avar->v_type == VAR_NUMBER)
8801 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008802 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008804 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008805 if (buf == NULL)
8806 {
8807 /* No full path name match, try a match with a URL or a "nofile"
8808 * buffer, these don't use the full path. */
8809 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8810 if (buf->b_fname != NULL
8811 && (path_with_url(buf->b_fname)
8812#ifdef FEAT_QUICKFIX
8813 || bt_nofile(buf)
8814#endif
8815 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008816 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008817 break;
8818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
8820 return buf;
8821}
8822
8823/*
8824 * "bufexists(expr)" function
8825 */
8826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008828 typval_T *argvars;
8829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834/*
8835 * "buflisted(expr)" function
8836 */
8837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 typval_T *argvars;
8840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 buf_T *buf;
8843
8844 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846}
8847
8848/*
8849 * "bufloaded(expr)" function
8850 */
8851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 typval_T *argvars;
8854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 buf_T *buf;
8857
8858 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860}
8861
Bram Moolenaar33570922005-01-25 22:26:29 +00008862static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008863
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864/*
8865 * Get buffer by number or pattern.
8866 */
8867 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008869 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 int save_magic;
8873 char_u *save_cpo;
8874 buf_T *buf;
8875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876 if (tv->v_type == VAR_NUMBER)
8877 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008878 if (tv->v_type != VAR_STRING)
8879 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 if (name == NULL || *name == NUL)
8881 return curbuf;
8882 if (name[0] == '$' && name[1] == NUL)
8883 return lastbuf;
8884
8885 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8886 save_magic = p_magic;
8887 p_magic = TRUE;
8888 save_cpo = p_cpo;
8889 p_cpo = (char_u *)"";
8890
8891 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8892 TRUE, FALSE));
8893
8894 p_magic = save_magic;
8895 p_cpo = save_cpo;
8896
8897 /* If not found, try expanding the name, like done for bufexists(). */
8898 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
8901 return buf;
8902}
8903
8904/*
8905 * "bufname(expr)" function
8906 */
8907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008908f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008909 typval_T *argvars;
8910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911{
8912 buf_T *buf;
8913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008914 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 buf = get_buf_tv(&argvars[0]);
8917 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 --emsg_off;
8923}
8924
8925/*
8926 * "bufnr(expr)" function
8927 */
8928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008929f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008930 typval_T *argvars;
8931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932{
8933 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008934 int error = FALSE;
8935 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008939 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008940 --emsg_off;
8941
8942 /* If the buffer isn't found and the second argument is not zero create a
8943 * new buffer. */
8944 if (buf == NULL
8945 && argvars[1].v_type != VAR_UNKNOWN
8946 && get_tv_number_chk(&argvars[1], &error) != 0
8947 && !error
8948 && (name = get_tv_string_chk(&argvars[0])) != NULL
8949 && !error)
8950 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8951
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956}
8957
8958/*
8959 * "bufwinnr(nr)" function
8960 */
8961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *argvars;
8964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
8966#ifdef FEAT_WINDOWS
8967 win_T *wp;
8968 int winnr = 0;
8969#endif
8970 buf_T *buf;
8971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975#ifdef FEAT_WINDOWS
8976 for (wp = firstwin; wp; wp = wp->w_next)
8977 {
8978 ++winnr;
8979 if (wp->w_buffer == buf)
8980 break;
8981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#endif
8986 --emsg_off;
8987}
8988
8989/*
8990 * "byte2line(byte)" function
8991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008994 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999#else
9000 long boff = 0;
9001
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 (linenr_T)0, &boff);
9008#endif
9009}
9010
9011/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009012 * "byteidx()" function
9013 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009016 typval_T *argvars;
9017 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009018{
9019#ifdef FEAT_MBYTE
9020 char_u *t;
9021#endif
9022 char_u *str;
9023 long idx;
9024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 str = get_tv_string_chk(&argvars[0]);
9026 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009028 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009029 return;
9030
9031#ifdef FEAT_MBYTE
9032 t = str;
9033 for ( ; idx > 0; idx--)
9034 {
9035 if (*t == NUL) /* EOL reached */
9036 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009037 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009038 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009039 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009040#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009041 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009043#endif
9044}
9045
9046/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009047 * "call(func, arglist)" function
9048 */
9049 static void
9050f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009053{
9054 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009055 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009056 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009057 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009058 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009060
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009061 if (argvars[1].v_type != VAR_LIST)
9062 {
9063 EMSG(_(e_listreq));
9064 return;
9065 }
9066 if (argvars[1].vval.v_list == NULL)
9067 return;
9068
9069 if (argvars[0].v_type == VAR_FUNC)
9070 func = argvars[0].vval.v_string;
9071 else
9072 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009073 if (*func == NUL)
9074 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009075
Bram Moolenaare9a41262005-01-15 22:18:47 +00009076 if (argvars[2].v_type != VAR_UNKNOWN)
9077 {
9078 if (argvars[2].v_type != VAR_DICT)
9079 {
9080 EMSG(_(e_dictreq));
9081 return;
9082 }
9083 selfdict = argvars[2].vval.v_dict;
9084 }
9085
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009086 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9087 item = item->li_next)
9088 {
9089 if (argc == MAX_FUNC_ARGS)
9090 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009091 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009092 break;
9093 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009094 /* Make a copy of each argument. This is needed to be able to set
9095 * v_lock to VAR_FIXED in the copy without changing the original list.
9096 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009097 copy_tv(&item->li_tv, &argv[argc++]);
9098 }
9099
9100 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009101 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009102 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9103 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009104
9105 /* Free the arguments. */
9106 while (argc > 0)
9107 clear_tv(&argv[--argc]);
9108}
9109
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009110#ifdef FEAT_FLOAT
9111/*
9112 * "ceil({float})" function
9113 */
9114 static void
9115f_ceil(argvars, rettv)
9116 typval_T *argvars;
9117 typval_T *rettv;
9118{
9119 float_T f;
9120
9121 rettv->v_type = VAR_FLOAT;
9122 if (get_float_arg(argvars, &f) == OK)
9123 rettv->vval.v_float = ceil(f);
9124 else
9125 rettv->vval.v_float = 0.0;
9126}
9127#endif
9128
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009129/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009130 * "changenr()" function
9131 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009132 static void
9133f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009134 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009135 typval_T *rettv;
9136{
9137 rettv->vval.v_number = curbuf->b_u_seq_cur;
9138}
9139
9140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 * "char2nr(string)" function
9142 */
9143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009145 typval_T *argvars;
9146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147{
9148#ifdef FEAT_MBYTE
9149 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 else
9152#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154}
9155
9156/*
9157 * "cindent(lnum)" function
9158 */
9159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009161 typval_T *argvars;
9162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
9164#ifdef FEAT_CINDENT
9165 pos_T pos;
9166 linenr_T lnum;
9167
9168 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9171 {
9172 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 curwin->w_cursor = pos;
9175 }
9176 else
9177#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179}
9180
9181/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009182 * "clearmatches()" function
9183 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009184 static void
9185f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009186 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009187 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009188{
9189#ifdef FEAT_SEARCH_EXTRA
9190 clear_matches(curwin);
9191#endif
9192}
9193
9194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 * "col(string)" function
9196 */
9197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009199 typval_T *argvars;
9200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
9202 colnr_T col = 0;
9203 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009204 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009206 fp = var2fpos(&argvars[0], FALSE, &fnum);
9207 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
9209 if (fp->col == MAXCOL)
9210 {
9211 /* '> can be MAXCOL, get the length of the line then */
9212 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009213 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 else
9215 col = MAXCOL;
9216 }
9217 else
9218 {
9219 col = fp->col + 1;
9220#ifdef FEAT_VIRTUALEDIT
9221 /* col(".") when the cursor is on the NUL at the end of the line
9222 * because of "coladd" can be seen as an extra column. */
9223 if (virtual_active() && fp == &curwin->w_cursor)
9224 {
9225 char_u *p = ml_get_cursor();
9226
9227 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9228 curwin->w_virtcol - curwin->w_cursor.coladd))
9229 {
9230# ifdef FEAT_MBYTE
9231 int l;
9232
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009233 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 col += l;
9235# else
9236 if (*p != NUL && p[1] == NUL)
9237 ++col;
9238# endif
9239 }
9240 }
9241#endif
9242 }
9243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009244 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245}
9246
Bram Moolenaar572cb562005-08-05 21:35:02 +00009247#if defined(FEAT_INS_EXPAND)
9248/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009249 * "complete()" function
9250 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009251 static void
9252f_complete(argvars, rettv)
9253 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009254 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009255{
9256 int startcol;
9257
9258 if ((State & INSERT) == 0)
9259 {
9260 EMSG(_("E785: complete() can only be used in Insert mode"));
9261 return;
9262 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009263
9264 /* Check for undo allowed here, because if something was already inserted
9265 * the line was already saved for undo and this check isn't done. */
9266 if (!undo_allowed())
9267 return;
9268
Bram Moolenaarade00832006-03-10 21:46:58 +00009269 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9270 {
9271 EMSG(_(e_invarg));
9272 return;
9273 }
9274
9275 startcol = get_tv_number_chk(&argvars[0], NULL);
9276 if (startcol <= 0)
9277 return;
9278
9279 set_completion(startcol - 1, argvars[1].vval.v_list);
9280}
9281
9282/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009283 * "complete_add()" function
9284 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009285 static void
9286f_complete_add(argvars, rettv)
9287 typval_T *argvars;
9288 typval_T *rettv;
9289{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009290 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009291}
9292
9293/*
9294 * "complete_check()" function
9295 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009296 static void
9297f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009298 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009299 typval_T *rettv;
9300{
9301 int saved = RedrawingDisabled;
9302
9303 RedrawingDisabled = 0;
9304 ins_compl_check_keys(0);
9305 rettv->vval.v_number = compl_interrupted;
9306 RedrawingDisabled = saved;
9307}
9308#endif
9309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310/*
9311 * "confirm(message, buttons[, default [, type]])" function
9312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009315 typval_T *argvars UNUSED;
9316 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
9318#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9319 char_u *message;
9320 char_u *buttons = NULL;
9321 char_u buf[NUMBUFLEN];
9322 char_u buf2[NUMBUFLEN];
9323 int def = 1;
9324 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 char_u *typestr;
9326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009328 message = get_tv_string_chk(&argvars[0]);
9329 if (message == NULL)
9330 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009331 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009333 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9334 if (buttons == NULL)
9335 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009336 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009338 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009339 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9342 if (typestr == NULL)
9343 error = TRUE;
9344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 switch (TOUPPER_ASC(*typestr))
9347 {
9348 case 'E': type = VIM_ERROR; break;
9349 case 'Q': type = VIM_QUESTION; break;
9350 case 'I': type = VIM_INFO; break;
9351 case 'W': type = VIM_WARNING; break;
9352 case 'G': type = VIM_GENERIC; break;
9353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 }
9355 }
9356 }
9357 }
9358
9359 if (buttons == NULL || *buttons == NUL)
9360 buttons = (char_u *)_("&Ok");
9361
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009362 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009363 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009364 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365#endif
9366}
9367
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009368/*
9369 * "copy()" function
9370 */
9371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *argvars;
9374 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009375{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009376 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009379#ifdef FEAT_FLOAT
9380/*
9381 * "cos()" function
9382 */
9383 static void
9384f_cos(argvars, rettv)
9385 typval_T *argvars;
9386 typval_T *rettv;
9387{
9388 float_T f;
9389
9390 rettv->v_type = VAR_FLOAT;
9391 if (get_float_arg(argvars, &f) == OK)
9392 rettv->vval.v_float = cos(f);
9393 else
9394 rettv->vval.v_float = 0.0;
9395}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009396
9397/*
9398 * "cosh()" function
9399 */
9400 static void
9401f_cosh(argvars, rettv)
9402 typval_T *argvars;
9403 typval_T *rettv;
9404{
9405 float_T f;
9406
9407 rettv->v_type = VAR_FLOAT;
9408 if (get_float_arg(argvars, &f) == OK)
9409 rettv->vval.v_float = cosh(f);
9410 else
9411 rettv->vval.v_float = 0.0;
9412}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009413#endif
9414
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009416 * "count()" function
9417 */
9418 static void
9419f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 typval_T *argvars;
9421 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009422{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009423 long n = 0;
9424 int ic = FALSE;
9425
Bram Moolenaare9a41262005-01-15 22:18:47 +00009426 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009427 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009428 listitem_T *li;
9429 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009430 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009431
Bram Moolenaare9a41262005-01-15 22:18:47 +00009432 if ((l = argvars[0].vval.v_list) != NULL)
9433 {
9434 li = l->lv_first;
9435 if (argvars[2].v_type != VAR_UNKNOWN)
9436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437 int error = FALSE;
9438
9439 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009440 if (argvars[3].v_type != VAR_UNKNOWN)
9441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009442 idx = get_tv_number_chk(&argvars[3], &error);
9443 if (!error)
9444 {
9445 li = list_find(l, idx);
9446 if (li == NULL)
9447 EMSGN(_(e_listidx), idx);
9448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009450 if (error)
9451 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009452 }
9453
9454 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009455 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009456 ++n;
9457 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009458 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009459 else if (argvars[0].v_type == VAR_DICT)
9460 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009461 int todo;
9462 dict_T *d;
9463 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009464
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009465 if ((d = argvars[0].vval.v_dict) != NULL)
9466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 int error = FALSE;
9468
Bram Moolenaare9a41262005-01-15 22:18:47 +00009469 if (argvars[2].v_type != VAR_UNKNOWN)
9470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009472 if (argvars[3].v_type != VAR_UNKNOWN)
9473 EMSG(_(e_invarg));
9474 }
9475
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009476 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009477 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009478 {
9479 if (!HASHITEM_EMPTY(hi))
9480 {
9481 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009482 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009483 ++n;
9484 }
9485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009486 }
9487 }
9488 else
9489 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009490 rettv->vval.v_number = n;
9491}
9492
9493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9495 *
9496 * Checks the existence of a cscope connection.
9497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009500 typval_T *argvars UNUSED;
9501 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
9503#ifdef FEAT_CSCOPE
9504 int num = 0;
9505 char_u *dbpath = NULL;
9506 char_u *prepend = NULL;
9507 char_u buf[NUMBUFLEN];
9508
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009509 if (argvars[0].v_type != VAR_UNKNOWN
9510 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 num = (int)get_tv_number(&argvars[0]);
9513 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009514 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 }
9517
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519#endif
9520}
9521
9522/*
9523 * "cursor(lnum, col)" function
9524 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009525 * Moves the cursor to the specified line and column.
9526 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009530 typval_T *argvars;
9531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
9533 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009534#ifdef FEAT_VIRTUALEDIT
9535 long coladd = 0;
9536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009538 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009539 if (argvars[1].v_type == VAR_UNKNOWN)
9540 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009541 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009542
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009543 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009544 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009545 line = pos.lnum;
9546 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009547#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009548 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009549#endif
9550 }
9551 else
9552 {
9553 line = get_tv_lnum(argvars);
9554 col = get_tv_number_chk(&argvars[1], NULL);
9555#ifdef FEAT_VIRTUALEDIT
9556 if (argvars[2].v_type != VAR_UNKNOWN)
9557 coladd = get_tv_number_chk(&argvars[2], NULL);
9558#endif
9559 }
9560 if (line < 0 || col < 0
9561#ifdef FEAT_VIRTUALEDIT
9562 || coladd < 0
9563#endif
9564 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 if (line > 0)
9567 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 if (col > 0)
9569 curwin->w_cursor.col = col - 1;
9570#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009571 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572#endif
9573
9574 /* Make sure the cursor is in a valid position. */
9575 check_cursor();
9576#ifdef FEAT_MBYTE
9577 /* Correct cursor for multi-byte character. */
9578 if (has_mbyte)
9579 mb_adjust_cursor();
9580#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009581
9582 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009583 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584}
9585
9586/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009587 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 */
9589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009591 typval_T *argvars;
9592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009594 int noref = 0;
9595
9596 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009597 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009598 if (noref < 0 || noref > 1)
9599 EMSG(_(e_invarg));
9600 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009601 {
9602 current_copyID += COPYID_INC;
9603 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605}
9606
9607/*
9608 * "delete()" function
9609 */
9610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 typval_T *argvars;
9613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619}
9620
9621/*
9622 * "did_filetype()" function
9623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009626 typval_T *argvars UNUSED;
9627 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631#endif
9632}
9633
9634/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009635 * "diff_filler()" function
9636 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009639 typval_T *argvars UNUSED;
9640 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009641{
9642#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009644#endif
9645}
9646
9647/*
9648 * "diff_hlID()" function
9649 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009652 typval_T *argvars UNUSED;
9653 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009654{
9655#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009657 static linenr_T prev_lnum = 0;
9658 static int changedtick = 0;
9659 static int fnum = 0;
9660 static int change_start = 0;
9661 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009662 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009663 int filler_lines;
9664 int col;
9665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009666 if (lnum < 0) /* ignore type error in {lnum} arg */
9667 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009668 if (lnum != prev_lnum
9669 || changedtick != curbuf->b_changedtick
9670 || fnum != curbuf->b_fnum)
9671 {
9672 /* New line, buffer, change: need to get the values. */
9673 filler_lines = diff_check(curwin, lnum);
9674 if (filler_lines < 0)
9675 {
9676 if (filler_lines == -1)
9677 {
9678 change_start = MAXCOL;
9679 change_end = -1;
9680 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9681 hlID = HLF_ADD; /* added line */
9682 else
9683 hlID = HLF_CHD; /* changed line */
9684 }
9685 else
9686 hlID = HLF_ADD; /* added line */
9687 }
9688 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009689 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009690 prev_lnum = lnum;
9691 changedtick = curbuf->b_changedtick;
9692 fnum = curbuf->b_fnum;
9693 }
9694
9695 if (hlID == HLF_CHD || hlID == HLF_TXD)
9696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009697 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009698 if (col >= change_start && col <= change_end)
9699 hlID = HLF_TXD; /* changed text */
9700 else
9701 hlID = HLF_CHD; /* changed line */
9702 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009703 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009704#endif
9705}
9706
9707/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009708 * "empty({expr})" function
9709 */
9710 static void
9711f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009712 typval_T *argvars;
9713 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009714{
9715 int n;
9716
9717 switch (argvars[0].v_type)
9718 {
9719 case VAR_STRING:
9720 case VAR_FUNC:
9721 n = argvars[0].vval.v_string == NULL
9722 || *argvars[0].vval.v_string == NUL;
9723 break;
9724 case VAR_NUMBER:
9725 n = argvars[0].vval.v_number == 0;
9726 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009727#ifdef FEAT_FLOAT
9728 case VAR_FLOAT:
9729 n = argvars[0].vval.v_float == 0.0;
9730 break;
9731#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009732 case VAR_LIST:
9733 n = argvars[0].vval.v_list == NULL
9734 || argvars[0].vval.v_list->lv_first == NULL;
9735 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009736 case VAR_DICT:
9737 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009739 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009740 default:
9741 EMSG2(_(e_intern2), "f_empty()");
9742 n = 0;
9743 }
9744
9745 rettv->vval.v_number = n;
9746}
9747
9748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 * "escape({string}, {chars})" function
9750 */
9751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009753 typval_T *argvars;
9754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755{
9756 char_u buf[NUMBUFLEN];
9757
Bram Moolenaar758711c2005-02-02 23:11:38 +00009758 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9759 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761}
9762
9763/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009764 * "eval()" function
9765 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009766 static void
9767f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 typval_T *argvars;
9769 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009770{
9771 char_u *s;
9772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009773 s = get_tv_string_chk(&argvars[0]);
9774 if (s != NULL)
9775 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9778 {
9779 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009780 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009782 else if (*s != NUL)
9783 EMSG(_(e_trailing));
9784}
9785
9786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 * "eventhandler()" function
9788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795}
9796
9797/*
9798 * "executable()" function
9799 */
9800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009802 typval_T *argvars;
9803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806}
9807
9808/*
9809 * "exists()" function
9810 */
9811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009813 typval_T *argvars;
9814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816 char_u *p;
9817 char_u *name;
9818 int n = FALSE;
9819 int len = 0;
9820
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009821 no_autoload = TRUE;
9822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 if (*p == '$') /* environment variable */
9825 {
9826 /* first try "normal" environment variables (fast) */
9827 if (mch_getenv(p + 1) != NULL)
9828 n = TRUE;
9829 else
9830 {
9831 /* try expanding things like $VIM and ${HOME} */
9832 p = expand_env_save(p);
9833 if (p != NULL && *p != '$')
9834 n = TRUE;
9835 vim_free(p);
9836 }
9837 }
9838 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009841 if (*skipwhite(p) != NUL)
9842 n = FALSE; /* trailing garbage */
9843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 else if (*p == '*') /* internal or user defined function */
9845 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009846 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 }
9848 else if (*p == ':')
9849 {
9850 n = cmd_exists(p + 1);
9851 }
9852 else if (*p == '#')
9853 {
9854#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009855 if (p[1] == '#')
9856 n = autocmd_supported(p + 2);
9857 else
9858 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859#endif
9860 }
9861 else /* internal variable */
9862 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009863 char_u *tofree;
9864 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009866 /* get_name_len() takes care of expanding curly braces */
9867 name = p;
9868 len = get_name_len(&p, &tofree, TRUE, FALSE);
9869 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009871 if (tofree != NULL)
9872 name = tofree;
9873 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9874 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009876 /* handle d.key, l[idx], f(expr) */
9877 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9878 if (n)
9879 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 }
9881 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009882 if (*p != NUL)
9883 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009885 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 }
9887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009889
9890 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891}
9892
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009893#ifdef FEAT_FLOAT
9894/*
9895 * "exp()" function
9896 */
9897 static void
9898f_exp(argvars, rettv)
9899 typval_T *argvars;
9900 typval_T *rettv;
9901{
9902 float_T f;
9903
9904 rettv->v_type = VAR_FLOAT;
9905 if (get_float_arg(argvars, &f) == OK)
9906 rettv->vval.v_float = exp(f);
9907 else
9908 rettv->vval.v_float = 0.0;
9909}
9910#endif
9911
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912/*
9913 * "expand()" function
9914 */
9915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009917 typval_T *argvars;
9918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919{
9920 char_u *s;
9921 int len;
9922 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009923 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927 rettv->v_type = VAR_STRING;
9928 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 if (*s == '%' || *s == '#' || *s == '<')
9930 {
9931 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009932 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 --emsg_off;
9934 }
9935 else
9936 {
9937 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009938 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009939 if (argvars[1].v_type != VAR_UNKNOWN
9940 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009941 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009942 if (!error)
9943 {
9944 ExpandInit(&xpc);
9945 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009946 if (p_wic)
9947 options += WILD_ICASE;
9948 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009949 }
9950 else
9951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 }
9953}
9954
9955/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009956 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009958 */
9959 static void
9960f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 typval_T *argvars;
9962 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009963{
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009964 char *arg_errmsg = N_("extend() argument");
9965
Bram Moolenaare9a41262005-01-15 22:18:47 +00009966 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009967 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009968 list_T *l1, *l2;
9969 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009971 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009972
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 l1 = argvars[0].vval.v_list;
9974 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009975 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009976 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 {
9978 if (argvars[2].v_type != VAR_UNKNOWN)
9979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009980 before = get_tv_number_chk(&argvars[2], &error);
9981 if (error)
9982 return; /* type error; errmsg already given */
9983
Bram Moolenaar758711c2005-02-02 23:11:38 +00009984 if (before == l1->lv_len)
9985 item = NULL;
9986 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009987 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009988 item = list_find(l1, before);
9989 if (item == NULL)
9990 {
9991 EMSGN(_(e_listidx), before);
9992 return;
9993 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009994 }
9995 }
9996 else
9997 item = NULL;
9998 list_extend(l1, l2, item);
9999
Bram Moolenaare9a41262005-01-15 22:18:47 +000010000 copy_tv(&argvars[0], rettv);
10001 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010002 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010003 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10004 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010005 dict_T *d1, *d2;
10006 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010007 char_u *action;
10008 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010009 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010010 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010011
10012 d1 = argvars[0].vval.v_dict;
10013 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010014 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010015 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010016 {
10017 /* Check the third argument. */
10018 if (argvars[2].v_type != VAR_UNKNOWN)
10019 {
10020 static char *(av[]) = {"keep", "force", "error"};
10021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010022 action = get_tv_string_chk(&argvars[2]);
10023 if (action == NULL)
10024 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010025 for (i = 0; i < 3; ++i)
10026 if (STRCMP(action, av[i]) == 0)
10027 break;
10028 if (i == 3)
10029 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010030 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010031 return;
10032 }
10033 }
10034 else
10035 action = (char_u *)"force";
10036
10037 /* Go over all entries in the second dict and add them to the
10038 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010039 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010041 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010042 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010043 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010044 --todo;
10045 di1 = dict_find(d1, hi2->hi_key, -1);
10046 if (di1 == NULL)
10047 {
10048 di1 = dictitem_copy(HI2DI(hi2));
10049 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10050 dictitem_free(di1);
10051 }
10052 else if (*action == 'e')
10053 {
10054 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10055 break;
10056 }
10057 else if (*action == 'f')
10058 {
10059 clear_tv(&di1->di_tv);
10060 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10061 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010062 }
10063 }
10064
Bram Moolenaare9a41262005-01-15 22:18:47 +000010065 copy_tv(&argvars[0], rettv);
10066 }
10067 }
10068 else
10069 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010070}
10071
10072/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010073 * "feedkeys()" function
10074 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010075 static void
10076f_feedkeys(argvars, rettv)
10077 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010078 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010079{
10080 int remap = TRUE;
10081 char_u *keys, *flags;
10082 char_u nbuf[NUMBUFLEN];
10083 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010084 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010085
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010086 /* This is not allowed in the sandbox. If the commands would still be
10087 * executed in the sandbox it would be OK, but it probably happens later,
10088 * when "sandbox" is no longer set. */
10089 if (check_secure())
10090 return;
10091
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010092 keys = get_tv_string(&argvars[0]);
10093 if (*keys != NUL)
10094 {
10095 if (argvars[1].v_type != VAR_UNKNOWN)
10096 {
10097 flags = get_tv_string_buf(&argvars[1], nbuf);
10098 for ( ; *flags != NUL; ++flags)
10099 {
10100 switch (*flags)
10101 {
10102 case 'n': remap = FALSE; break;
10103 case 'm': remap = TRUE; break;
10104 case 't': typed = TRUE; break;
10105 }
10106 }
10107 }
10108
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010109 /* Need to escape K_SPECIAL and CSI before putting the string in the
10110 * typeahead buffer. */
10111 keys_esc = vim_strsave_escape_csi(keys);
10112 if (keys_esc != NULL)
10113 {
10114 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010115 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010116 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010117 if (vgetc_busy)
10118 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010119 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010120 }
10121}
10122
10123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 * "filereadable()" function
10125 */
10126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010128 typval_T *argvars;
10129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010131 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 char_u *p;
10133 int n;
10134
Bram Moolenaarc236c162008-07-13 17:41:49 +000010135#ifndef O_NONBLOCK
10136# define O_NONBLOCK 0
10137#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010138 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010139 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10140 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 {
10142 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010143 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 }
10145 else
10146 n = FALSE;
10147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149}
10150
10151/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010152 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 * rights to write into.
10154 */
10155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 typval_T *argvars;
10158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010160 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161}
10162
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010163static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010164
10165 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010166findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010167 typval_T *argvars;
10168 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010169 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010170{
10171#ifdef FEAT_SEARCHPATH
10172 char_u *fname;
10173 char_u *fresult = NULL;
10174 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10175 char_u *p;
10176 char_u pathbuf[NUMBUFLEN];
10177 int count = 1;
10178 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010179 int error = FALSE;
10180#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010181
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010182 rettv->vval.v_string = NULL;
10183 rettv->v_type = VAR_STRING;
10184
10185#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010188 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10191 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010192 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 else
10194 {
10195 if (*p != NUL)
10196 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010199 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010201 }
10202
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010203 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10204 error = TRUE;
10205
10206 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 do
10209 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010210 if (rettv->v_type == VAR_STRING)
10211 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 fresult = find_file_in_path_option(first ? fname : NULL,
10213 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010214 0, first, path,
10215 find_what,
10216 curbuf->b_ffname,
10217 find_what == FINDFILE_DIR
10218 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010220
10221 if (fresult != NULL && rettv->v_type == VAR_LIST)
10222 list_append_string(rettv->vval.v_list, fresult, -1);
10223
10224 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010225 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010226
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010227 if (rettv->v_type == VAR_STRING)
10228 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010229#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010230}
10231
Bram Moolenaar33570922005-01-25 22:26:29 +000010232static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10233static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010234
10235/*
10236 * Implementation of map() and filter().
10237 */
10238 static void
10239filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010240 typval_T *argvars;
10241 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010242 int map;
10243{
10244 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010246 listitem_T *li, *nli;
10247 list_T *l = NULL;
10248 dictitem_T *di;
10249 hashtab_T *ht;
10250 hashitem_T *hi;
10251 dict_T *d = NULL;
10252 typval_T save_val;
10253 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010254 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010255 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010256 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10257 char *arg_errmsg = (map ? N_("map() argument")
10258 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010259 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010260 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010261
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010263 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010264 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010265 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 return;
10267 }
10268 else if (argvars[0].v_type == VAR_DICT)
10269 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010270 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010271 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 return;
10273 }
10274 else
10275 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010276 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 return;
10278 }
10279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010280 expr = get_tv_string_buf_chk(&argvars[1], buf);
10281 /* On type errors, the preceding call has already displayed an error
10282 * message. Avoid a misleading error message for an empty string that
10283 * was not passed as argument. */
10284 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 prepare_vimvar(VV_VAL, &save_val);
10287 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010288
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010289 /* We reset "did_emsg" to be able to detect whether an error
10290 * occurred during evaluation of the expression. */
10291 save_did_emsg = did_emsg;
10292 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010293
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010294 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010295 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010297 vimvars[VV_KEY].vv_type = VAR_STRING;
10298
10299 ht = &d->dv_hashtab;
10300 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010301 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010302 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 if (!HASHITEM_EMPTY(hi))
10305 {
10306 --todo;
10307 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010308 if (tv_check_lock(di->di_tv.v_lock,
10309 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 break;
10311 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010312 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010313 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 break;
10315 if (!map && rem)
10316 dictitem_remove(d, di);
10317 clear_tv(&vimvars[VV_KEY].vv_tv);
10318 }
10319 }
10320 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010321 }
10322 else
10323 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010324 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 for (li = l->lv_first; li != NULL; li = nli)
10327 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010328 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010329 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010330 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010331 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010332 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010333 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010334 break;
10335 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010337 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010338 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010339 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010340
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010341 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010342 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010343
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010344 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010345 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010346
10347 copy_tv(&argvars[0], rettv);
10348}
10349
10350 static int
10351filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010352 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010353 char_u *expr;
10354 int map;
10355 int *remp;
10356{
Bram Moolenaar33570922005-01-25 22:26:29 +000010357 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010358 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010359 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010360
Bram Moolenaar33570922005-01-25 22:26:29 +000010361 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010362 s = expr;
10363 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010364 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365 if (*s != NUL) /* check for trailing chars after expr */
10366 {
10367 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010368 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010369 }
10370 if (map)
10371 {
10372 /* map(): replace the list item value */
10373 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010374 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010375 *tv = rettv;
10376 }
10377 else
10378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010379 int error = FALSE;
10380
Bram Moolenaare9a41262005-01-15 22:18:47 +000010381 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010382 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010384 /* On type error, nothing has been removed; return FAIL to stop the
10385 * loop. The error message was given by get_tv_number_chk(). */
10386 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010387 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010388 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010389 retval = OK;
10390theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010391 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010392 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010393}
10394
10395/*
10396 * "filter()" function
10397 */
10398 static void
10399f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010400 typval_T *argvars;
10401 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010402{
10403 filter_map(argvars, rettv, FALSE);
10404}
10405
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010406/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010407 * "finddir({fname}[, {path}[, {count}]])" function
10408 */
10409 static void
10410f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010411 typval_T *argvars;
10412 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010413{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010414 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010415}
10416
10417/*
10418 * "findfile({fname}[, {path}[, {count}]])" function
10419 */
10420 static void
10421f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010422 typval_T *argvars;
10423 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010424{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010425 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010426}
10427
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010428#ifdef FEAT_FLOAT
10429/*
10430 * "float2nr({float})" function
10431 */
10432 static void
10433f_float2nr(argvars, rettv)
10434 typval_T *argvars;
10435 typval_T *rettv;
10436{
10437 float_T f;
10438
10439 if (get_float_arg(argvars, &f) == OK)
10440 {
10441 if (f < -0x7fffffff)
10442 rettv->vval.v_number = -0x7fffffff;
10443 else if (f > 0x7fffffff)
10444 rettv->vval.v_number = 0x7fffffff;
10445 else
10446 rettv->vval.v_number = (varnumber_T)f;
10447 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010448}
10449
10450/*
10451 * "floor({float})" function
10452 */
10453 static void
10454f_floor(argvars, rettv)
10455 typval_T *argvars;
10456 typval_T *rettv;
10457{
10458 float_T f;
10459
10460 rettv->v_type = VAR_FLOAT;
10461 if (get_float_arg(argvars, &f) == OK)
10462 rettv->vval.v_float = floor(f);
10463 else
10464 rettv->vval.v_float = 0.0;
10465}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010466
10467/*
10468 * "fmod()" function
10469 */
10470 static void
10471f_fmod(argvars, rettv)
10472 typval_T *argvars;
10473 typval_T *rettv;
10474{
10475 float_T fx, fy;
10476
10477 rettv->v_type = VAR_FLOAT;
10478 if (get_float_arg(argvars, &fx) == OK
10479 && get_float_arg(&argvars[1], &fy) == OK)
10480 rettv->vval.v_float = fmod(fx, fy);
10481 else
10482 rettv->vval.v_float = 0.0;
10483}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010484#endif
10485
Bram Moolenaar0d660222005-01-07 21:51:51 +000010486/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010487 * "fnameescape({string})" function
10488 */
10489 static void
10490f_fnameescape(argvars, rettv)
10491 typval_T *argvars;
10492 typval_T *rettv;
10493{
10494 rettv->vval.v_string = vim_strsave_fnameescape(
10495 get_tv_string(&argvars[0]), FALSE);
10496 rettv->v_type = VAR_STRING;
10497}
10498
10499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 * "fnamemodify({fname}, {mods})" function
10501 */
10502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010504 typval_T *argvars;
10505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507 char_u *fname;
10508 char_u *mods;
10509 int usedlen = 0;
10510 int len;
10511 char_u *fbuf = NULL;
10512 char_u buf[NUMBUFLEN];
10513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 fname = get_tv_string_chk(&argvars[0]);
10515 mods = get_tv_string_buf_chk(&argvars[1], buf);
10516 if (fname == NULL || mods == NULL)
10517 fname = NULL;
10518 else
10519 {
10520 len = (int)STRLEN(fname);
10521 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 vim_free(fbuf);
10530}
10531
Bram Moolenaar33570922005-01-25 22:26:29 +000010532static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533
10534/*
10535 * "foldclosed()" function
10536 */
10537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010539 typval_T *argvars;
10540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 int end;
10542{
10543#ifdef FEAT_FOLDING
10544 linenr_T lnum;
10545 linenr_T first, last;
10546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10549 {
10550 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10551 {
10552 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 return;
10557 }
10558 }
10559#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561}
10562
10563/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010564 * "foldclosed()" function
10565 */
10566 static void
10567f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010568 typval_T *argvars;
10569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010570{
10571 foldclosed_both(argvars, rettv, FALSE);
10572}
10573
10574/*
10575 * "foldclosedend()" function
10576 */
10577 static void
10578f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010579 typval_T *argvars;
10580 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010581{
10582 foldclosed_both(argvars, rettv, TRUE);
10583}
10584
10585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 * "foldlevel()" function
10587 */
10588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010590 typval_T *argvars;
10591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592{
10593#ifdef FEAT_FOLDING
10594 linenr_T lnum;
10595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600}
10601
10602/*
10603 * "foldtext()" function
10604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010606f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609{
10610#ifdef FEAT_FOLDING
10611 linenr_T lnum;
10612 char_u *s;
10613 char_u *r;
10614 int len;
10615 char *txt;
10616#endif
10617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 rettv->v_type = VAR_STRING;
10619 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010621 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10622 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10623 <= curbuf->b_ml.ml_line_count
10624 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
10626 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010627 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10628 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 {
10630 if (!linewhite(lnum))
10631 break;
10632 ++lnum;
10633 }
10634
10635 /* Find interesting text in this line. */
10636 s = skipwhite(ml_get(lnum));
10637 /* skip C comment-start */
10638 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010641 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010642 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010643 {
10644 s = skipwhite(ml_get(lnum + 1));
10645 if (*s == '*')
10646 s = skipwhite(s + 1);
10647 }
10648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 txt = _("+-%s%3ld lines: ");
10650 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010651 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 + 20 /* for %3ld */
10653 + STRLEN(s))); /* concatenated */
10654 if (r != NULL)
10655 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010656 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10657 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10658 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 len = (int)STRLEN(r);
10660 STRCAT(r, s);
10661 /* remove 'foldmarker' and 'commentstring' */
10662 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 }
10665 }
10666#endif
10667}
10668
10669/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010670 * "foldtextresult(lnum)" function
10671 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010674 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010675 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010676{
10677#ifdef FEAT_FOLDING
10678 linenr_T lnum;
10679 char_u *text;
10680 char_u buf[51];
10681 foldinfo_T foldinfo;
10682 int fold_count;
10683#endif
10684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 rettv->v_type = VAR_STRING;
10686 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010687#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 /* treat illegal types and illegal string values for {lnum} the same */
10690 if (lnum < 0)
10691 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010692 fold_count = foldedCount(curwin, lnum, &foldinfo);
10693 if (fold_count > 0)
10694 {
10695 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10696 &foldinfo, buf);
10697 if (text == buf)
10698 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010700 }
10701#endif
10702}
10703
10704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 * "foreground()" function
10706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010709 typval_T *argvars UNUSED;
10710 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712#ifdef FEAT_GUI
10713 if (gui.in_use)
10714 gui_mch_set_foreground();
10715#else
10716# ifdef WIN32
10717 win32_set_foreground();
10718# endif
10719#endif
10720}
10721
10722/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010723 * "function()" function
10724 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010727 typval_T *argvars;
10728 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010729{
10730 char_u *s;
10731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010733 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010734 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010735 /* Don't check an autoload name for existence here. */
10736 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010737 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010738 else
10739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010740 rettv->vval.v_string = vim_strsave(s);
10741 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010742 }
10743}
10744
10745/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010746 * "garbagecollect()" function
10747 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010748 static void
10749f_garbagecollect(argvars, rettv)
10750 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010751 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010752{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010753 /* This is postponed until we are back at the toplevel, because we may be
10754 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10755 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010756
10757 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10758 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010759}
10760
10761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010762 * "get()" function
10763 */
10764 static void
10765f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010766 typval_T *argvars;
10767 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010768{
Bram Moolenaar33570922005-01-25 22:26:29 +000010769 listitem_T *li;
10770 list_T *l;
10771 dictitem_T *di;
10772 dict_T *d;
10773 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010774
Bram Moolenaare9a41262005-01-15 22:18:47 +000010775 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010776 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010777 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 int error = FALSE;
10780
10781 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10782 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010783 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010784 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010785 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010786 else if (argvars[0].v_type == VAR_DICT)
10787 {
10788 if ((d = argvars[0].vval.v_dict) != NULL)
10789 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010790 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010791 if (di != NULL)
10792 tv = &di->di_tv;
10793 }
10794 }
10795 else
10796 EMSG2(_(e_listdictarg), "get()");
10797
10798 if (tv == NULL)
10799 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010800 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010801 copy_tv(&argvars[2], rettv);
10802 }
10803 else
10804 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805}
10806
Bram Moolenaar342337a2005-07-21 21:11:17 +000010807static 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 +000010808
10809/*
10810 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010811 * Return a range (from start to end) of lines in rettv from the specified
10812 * buffer.
10813 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010814 */
10815 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010816get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010817 buf_T *buf;
10818 linenr_T start;
10819 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010820 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010821 typval_T *rettv;
10822{
10823 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010824
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010825 if (retlist && rettv_list_alloc(rettv) == FAIL)
10826 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010827
10828 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10829 return;
10830
10831 if (!retlist)
10832 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010833 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10834 p = ml_get_buf(buf, start, FALSE);
10835 else
10836 p = (char_u *)"";
10837
10838 rettv->v_type = VAR_STRING;
10839 rettv->vval.v_string = vim_strsave(p);
10840 }
10841 else
10842 {
10843 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010844 return;
10845
10846 if (start < 1)
10847 start = 1;
10848 if (end > buf->b_ml.ml_line_count)
10849 end = buf->b_ml.ml_line_count;
10850 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010851 if (list_append_string(rettv->vval.v_list,
10852 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010853 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010854 }
10855}
10856
10857/*
10858 * "getbufline()" function
10859 */
10860 static void
10861f_getbufline(argvars, rettv)
10862 typval_T *argvars;
10863 typval_T *rettv;
10864{
10865 linenr_T lnum;
10866 linenr_T end;
10867 buf_T *buf;
10868
10869 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10870 ++emsg_off;
10871 buf = get_buf_tv(&argvars[0]);
10872 --emsg_off;
10873
Bram Moolenaar661b1822005-07-28 22:36:45 +000010874 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010875 if (argvars[2].v_type == VAR_UNKNOWN)
10876 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010877 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010878 end = get_tv_lnum_buf(&argvars[2], buf);
10879
Bram Moolenaar342337a2005-07-21 21:11:17 +000010880 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010881}
10882
Bram Moolenaar0d660222005-01-07 21:51:51 +000010883/*
10884 * "getbufvar()" function
10885 */
10886 static void
10887f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010888 typval_T *argvars;
10889 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010890{
10891 buf_T *buf;
10892 buf_T *save_curbuf;
10893 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010894 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10897 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010898 ++emsg_off;
10899 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010900
10901 rettv->v_type = VAR_STRING;
10902 rettv->vval.v_string = NULL;
10903
10904 if (buf != NULL && varname != NULL)
10905 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010906 /* set curbuf to be our buf, temporarily */
10907 save_curbuf = curbuf;
10908 curbuf = buf;
10909
Bram Moolenaar0d660222005-01-07 21:51:51 +000010910 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010911 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010912 else if (STRCMP(varname, "changedtick") == 0)
10913 {
10914 rettv->v_type = VAR_NUMBER;
10915 rettv->vval.v_number = curbuf->b_changedtick;
10916 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010917 else
10918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 if (*varname == NUL)
10920 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10921 * scope prefix before the NUL byte is required by
10922 * find_var_in_ht(). */
10923 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010924 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010925 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010926 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010927 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010928 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010929
10930 /* restore previous notion of curbuf */
10931 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010932 }
10933
10934 --emsg_off;
10935}
10936
10937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 * "getchar()" function
10939 */
10940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010942 typval_T *argvars;
10943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944{
10945 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010948 /* Position the cursor. Needed after a message that ends in a space. */
10949 windgoto(msg_row, msg_col);
10950
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 ++no_mapping;
10952 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010953 for (;;)
10954 {
10955 if (argvars[0].v_type == VAR_UNKNOWN)
10956 /* getchar(): blocking wait. */
10957 n = safe_vgetc();
10958 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10959 /* getchar(1): only check if char avail */
10960 n = vpeekc();
10961 else if (error || vpeekc() == NUL)
10962 /* illegal argument or getchar(0) and no char avail: return zero */
10963 n = 0;
10964 else
10965 /* getchar(0) and char avail: return char */
10966 n = safe_vgetc();
10967 if (n == K_IGNORE)
10968 continue;
10969 break;
10970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 --no_mapping;
10972 --allow_keys;
10973
Bram Moolenaar219b8702006-11-01 14:32:36 +000010974 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10975 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10976 vimvars[VV_MOUSE_COL].vv_nr = 0;
10977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 if (IS_SPECIAL(n) || mod_mask != 0)
10980 {
10981 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10982 int i = 0;
10983
10984 /* Turn a special key into three bytes, plus modifier. */
10985 if (mod_mask != 0)
10986 {
10987 temp[i++] = K_SPECIAL;
10988 temp[i++] = KS_MODIFIER;
10989 temp[i++] = mod_mask;
10990 }
10991 if (IS_SPECIAL(n))
10992 {
10993 temp[i++] = K_SPECIAL;
10994 temp[i++] = K_SECOND(n);
10995 temp[i++] = K_THIRD(n);
10996 }
10997#ifdef FEAT_MBYTE
10998 else if (has_mbyte)
10999 i += (*mb_char2bytes)(n, temp + i);
11000#endif
11001 else
11002 temp[i++] = n;
11003 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 rettv->v_type = VAR_STRING;
11005 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011006
11007#ifdef FEAT_MOUSE
11008 if (n == K_LEFTMOUSE
11009 || n == K_LEFTMOUSE_NM
11010 || n == K_LEFTDRAG
11011 || n == K_LEFTRELEASE
11012 || n == K_LEFTRELEASE_NM
11013 || n == K_MIDDLEMOUSE
11014 || n == K_MIDDLEDRAG
11015 || n == K_MIDDLERELEASE
11016 || n == K_RIGHTMOUSE
11017 || n == K_RIGHTDRAG
11018 || n == K_RIGHTRELEASE
11019 || n == K_X1MOUSE
11020 || n == K_X1DRAG
11021 || n == K_X1RELEASE
11022 || n == K_X2MOUSE
11023 || n == K_X2DRAG
11024 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011025 || n == K_MOUSELEFT
11026 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011027 || n == K_MOUSEDOWN
11028 || n == K_MOUSEUP)
11029 {
11030 int row = mouse_row;
11031 int col = mouse_col;
11032 win_T *win;
11033 linenr_T lnum;
11034# ifdef FEAT_WINDOWS
11035 win_T *wp;
11036# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011037 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011038
11039 if (row >= 0 && col >= 0)
11040 {
11041 /* Find the window at the mouse coordinates and compute the
11042 * text position. */
11043 win = mouse_find_win(&row, &col);
11044 (void)mouse_comp_pos(win, &row, &col, &lnum);
11045# ifdef FEAT_WINDOWS
11046 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011047 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011048# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011049 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011050 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11051 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11052 }
11053 }
11054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 }
11056}
11057
11058/*
11059 * "getcharmod()" function
11060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067}
11068
11069/*
11070 * "getcmdline()" function
11071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011074 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 rettv->v_type = VAR_STRING;
11078 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079}
11080
11081/*
11082 * "getcmdpos()" function
11083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011086 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090}
11091
11092/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011093 * "getcmdtype()" function
11094 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011095 static void
11096f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011097 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011098 typval_T *rettv;
11099{
11100 rettv->v_type = VAR_STRING;
11101 rettv->vval.v_string = alloc(2);
11102 if (rettv->vval.v_string != NULL)
11103 {
11104 rettv->vval.v_string[0] = get_cmdline_type();
11105 rettv->vval.v_string[1] = NUL;
11106 }
11107}
11108
11109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 * "getcwd()" function
11111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011114 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011117 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011120 rettv->vval.v_string = NULL;
11121 cwd = alloc(MAXPATHL);
11122 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011124 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11125 {
11126 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011128 if (rettv->vval.v_string != NULL)
11129 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011131 }
11132 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 }
11134}
11135
11136/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011137 * "getfontname()" function
11138 */
11139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011143{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->v_type = VAR_STRING;
11145 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011146#ifdef FEAT_GUI
11147 if (gui.in_use)
11148 {
11149 GuiFont font;
11150 char_u *name = NULL;
11151
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011152 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011153 {
11154 /* Get the "Normal" font. Either the name saved by
11155 * hl_set_font_name() or from the font ID. */
11156 font = gui.norm_font;
11157 name = hl_get_font_name();
11158 }
11159 else
11160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011162 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11163 return;
11164 font = gui_mch_get_font(name, FALSE);
11165 if (font == NOFONT)
11166 return; /* Invalid font name, return empty string. */
11167 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011169 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011170 gui_mch_free_font(font);
11171 }
11172#endif
11173}
11174
11175/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011176 * "getfperm({fname})" function
11177 */
11178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011180 typval_T *argvars;
11181 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011182{
11183 char_u *fname;
11184 struct stat st;
11185 char_u *perm = NULL;
11186 char_u flags[] = "rwx";
11187 int i;
11188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011192 if (mch_stat((char *)fname, &st) >= 0)
11193 {
11194 perm = vim_strsave((char_u *)"---------");
11195 if (perm != NULL)
11196 {
11197 for (i = 0; i < 9; i++)
11198 {
11199 if (st.st_mode & (1 << (8 - i)))
11200 perm[i] = flags[i % 3];
11201 }
11202 }
11203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011205}
11206
11207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 * "getfsize({fname})" function
11209 */
11210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 typval_T *argvars;
11213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214{
11215 char_u *fname;
11216 struct stat st;
11217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221
11222 if (mch_stat((char *)fname, &st) >= 0)
11223 {
11224 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011228 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011229
11230 /* non-perfect check for overflow */
11231 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11232 rettv->vval.v_number = -2;
11233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 }
11235 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237}
11238
11239/*
11240 * "getftime({fname})" function
11241 */
11242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011244 typval_T *argvars;
11245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246{
11247 char_u *fname;
11248 struct stat st;
11249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
11252 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256}
11257
11258/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011259 * "getftype({fname})" function
11260 */
11261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *argvars;
11264 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011265{
11266 char_u *fname;
11267 struct stat st;
11268 char_u *type = NULL;
11269 char *t;
11270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011274 if (mch_lstat((char *)fname, &st) >= 0)
11275 {
11276#ifdef S_ISREG
11277 if (S_ISREG(st.st_mode))
11278 t = "file";
11279 else if (S_ISDIR(st.st_mode))
11280 t = "dir";
11281# ifdef S_ISLNK
11282 else if (S_ISLNK(st.st_mode))
11283 t = "link";
11284# endif
11285# ifdef S_ISBLK
11286 else if (S_ISBLK(st.st_mode))
11287 t = "bdev";
11288# endif
11289# ifdef S_ISCHR
11290 else if (S_ISCHR(st.st_mode))
11291 t = "cdev";
11292# endif
11293# ifdef S_ISFIFO
11294 else if (S_ISFIFO(st.st_mode))
11295 t = "fifo";
11296# endif
11297# ifdef S_ISSOCK
11298 else if (S_ISSOCK(st.st_mode))
11299 t = "fifo";
11300# endif
11301 else
11302 t = "other";
11303#else
11304# ifdef S_IFMT
11305 switch (st.st_mode & S_IFMT)
11306 {
11307 case S_IFREG: t = "file"; break;
11308 case S_IFDIR: t = "dir"; break;
11309# ifdef S_IFLNK
11310 case S_IFLNK: t = "link"; break;
11311# endif
11312# ifdef S_IFBLK
11313 case S_IFBLK: t = "bdev"; break;
11314# endif
11315# ifdef S_IFCHR
11316 case S_IFCHR: t = "cdev"; break;
11317# endif
11318# ifdef S_IFIFO
11319 case S_IFIFO: t = "fifo"; break;
11320# endif
11321# ifdef S_IFSOCK
11322 case S_IFSOCK: t = "socket"; break;
11323# endif
11324 default: t = "other";
11325 }
11326# else
11327 if (mch_isdir(fname))
11328 t = "dir";
11329 else
11330 t = "file";
11331# endif
11332#endif
11333 type = vim_strsave((char_u *)t);
11334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011336}
11337
11338/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011339 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011340 */
11341 static void
11342f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 typval_T *argvars;
11344 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345{
11346 linenr_T lnum;
11347 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011348 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349
11350 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011351 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011352 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011353 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011354 retlist = FALSE;
11355 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011356 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011357 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011358 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011359 retlist = TRUE;
11360 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011361
Bram Moolenaar342337a2005-07-21 21:11:17 +000011362 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011363}
11364
11365/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011366 * "getmatches()" function
11367 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011368 static void
11369f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011370 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011371 typval_T *rettv;
11372{
11373#ifdef FEAT_SEARCH_EXTRA
11374 dict_T *dict;
11375 matchitem_T *cur = curwin->w_match_head;
11376
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011377 if (rettv_list_alloc(rettv) == OK)
11378 {
11379 while (cur != NULL)
11380 {
11381 dict = dict_alloc();
11382 if (dict == NULL)
11383 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011384 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11385 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11386 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11387 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11388 list_append_dict(rettv->vval.v_list, dict);
11389 cur = cur->next;
11390 }
11391 }
11392#endif
11393}
11394
11395/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011396 * "getpid()" function
11397 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011398 static void
11399f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011400 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011401 typval_T *rettv;
11402{
11403 rettv->vval.v_number = mch_get_pid();
11404}
11405
11406/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011407 * "getpos(string)" function
11408 */
11409 static void
11410f_getpos(argvars, rettv)
11411 typval_T *argvars;
11412 typval_T *rettv;
11413{
11414 pos_T *fp;
11415 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011416 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011417
11418 if (rettv_list_alloc(rettv) == OK)
11419 {
11420 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011421 fp = var2fpos(&argvars[0], TRUE, &fnum);
11422 if (fnum != -1)
11423 list_append_number(l, (varnumber_T)fnum);
11424 else
11425 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011426 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11427 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011428 list_append_number(l, (fp != NULL)
11429 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011430 : (varnumber_T)0);
11431 list_append_number(l,
11432#ifdef FEAT_VIRTUALEDIT
11433 (fp != NULL) ? (varnumber_T)fp->coladd :
11434#endif
11435 (varnumber_T)0);
11436 }
11437 else
11438 rettv->vval.v_number = FALSE;
11439}
11440
11441/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011442 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011443 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011444 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011445f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011446 typval_T *argvars UNUSED;
11447 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011448{
11449#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011450 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011451#endif
11452
Bram Moolenaar2641f772005-03-25 21:58:17 +000011453#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011454 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011455 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011456 wp = NULL;
11457 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11458 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011459 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011460 if (wp == NULL)
11461 return;
11462 }
11463
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011464 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011465 }
11466#endif
11467}
11468
11469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 * "getreg()" function
11471 */
11472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 typval_T *argvars;
11475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476{
11477 char_u *strregname;
11478 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011479 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011482 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 strregname = get_tv_string_chk(&argvars[0]);
11485 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011486 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011490 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 regname = (strregname == NULL ? '"' : *strregname);
11492 if (regname == 0)
11493 regname = '"';
11494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 rettv->vval.v_string = error ? NULL :
11497 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498}
11499
11500/*
11501 * "getregtype()" function
11502 */
11503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 typval_T *argvars;
11506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507{
11508 char_u *strregname;
11509 int regname;
11510 char_u buf[NUMBUFLEN + 2];
11511 long reglen = 0;
11512
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011513 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 {
11515 strregname = get_tv_string_chk(&argvars[0]);
11516 if (strregname == NULL) /* type error; errmsg already given */
11517 {
11518 rettv->v_type = VAR_STRING;
11519 rettv->vval.v_string = NULL;
11520 return;
11521 }
11522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 else
11524 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011525 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526
11527 regname = (strregname == NULL ? '"' : *strregname);
11528 if (regname == 0)
11529 regname = '"';
11530
11531 buf[0] = NUL;
11532 buf[1] = NUL;
11533 switch (get_reg_type(regname, &reglen))
11534 {
11535 case MLINE: buf[0] = 'V'; break;
11536 case MCHAR: buf[0] = 'v'; break;
11537#ifdef FEAT_VISUAL
11538 case MBLOCK:
11539 buf[0] = Ctrl_V;
11540 sprintf((char *)buf + 1, "%ld", reglen + 1);
11541 break;
11542#endif
11543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->v_type = VAR_STRING;
11545 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546}
11547
11548/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011549 * "gettabvar()" function
11550 */
11551 static void
11552f_gettabvar(argvars, rettv)
11553 typval_T *argvars;
11554 typval_T *rettv;
11555{
11556 tabpage_T *tp;
11557 dictitem_T *v;
11558 char_u *varname;
11559
11560 rettv->v_type = VAR_STRING;
11561 rettv->vval.v_string = NULL;
11562
11563 varname = get_tv_string_chk(&argvars[1]);
11564 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11565 if (tp != NULL && varname != NULL)
11566 {
11567 /* look up the variable */
11568 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11569 if (v != NULL)
11570 copy_tv(&v->di_tv, rettv);
11571 }
11572}
11573
11574/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011575 * "gettabwinvar()" function
11576 */
11577 static void
11578f_gettabwinvar(argvars, rettv)
11579 typval_T *argvars;
11580 typval_T *rettv;
11581{
11582 getwinvar(argvars, rettv, 1);
11583}
11584
11585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 * "getwinposx()" function
11587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011590 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594#ifdef FEAT_GUI
11595 if (gui.in_use)
11596 {
11597 int x, y;
11598
11599 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 }
11602#endif
11603}
11604
11605/*
11606 * "getwinposy()" function
11607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614#ifdef FEAT_GUI
11615 if (gui.in_use)
11616 {
11617 int x, y;
11618
11619 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011620 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 }
11622#endif
11623}
11624
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011625/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011626 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011627 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011628 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011629find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011630 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011631 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011632{
11633#ifdef FEAT_WINDOWS
11634 win_T *wp;
11635#endif
11636 int nr;
11637
11638 nr = get_tv_number_chk(vp, NULL);
11639
11640#ifdef FEAT_WINDOWS
11641 if (nr < 0)
11642 return NULL;
11643 if (nr == 0)
11644 return curwin;
11645
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011646 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11647 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011648 if (--nr <= 0)
11649 break;
11650 return wp;
11651#else
11652 if (nr == 0 || nr == 1)
11653 return curwin;
11654 return NULL;
11655#endif
11656}
11657
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658/*
11659 * "getwinvar()" function
11660 */
11661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011663 typval_T *argvars;
11664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011666 getwinvar(argvars, rettv, 0);
11667}
11668
11669/*
11670 * getwinvar() and gettabwinvar()
11671 */
11672 static void
11673getwinvar(argvars, rettv, off)
11674 typval_T *argvars;
11675 typval_T *rettv;
11676 int off; /* 1 for gettabwinvar() */
11677{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678 win_T *win, *oldcurwin;
11679 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011680 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011681 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011683#ifdef FEAT_WINDOWS
11684 if (off == 1)
11685 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11686 else
11687 tp = curtab;
11688#endif
11689 win = find_win_by_nr(&argvars[off], tp);
11690 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011693 rettv->v_type = VAR_STRING;
11694 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695
11696 if (win != NULL && varname != NULL)
11697 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011698 /* Set curwin to be our win, temporarily. Also set curbuf, so
11699 * that we can get buffer-local options. */
11700 oldcurwin = curwin;
11701 curwin = win;
11702 curbuf = win->w_buffer;
11703
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 else
11707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011708 if (*varname == NUL)
11709 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11710 * scope prefix before the NUL byte is required by
11711 * find_var_in_ht(). */
11712 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011714 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011716 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011718
11719 /* restore previous notion of curwin */
11720 curwin = oldcurwin;
11721 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 }
11723
11724 --emsg_off;
11725}
11726
11727/*
11728 * "glob()" function
11729 */
11730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011731f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011732 typval_T *argvars;
11733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011735 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011737 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011739 /* When the optional second argument is non-zero, don't remove matches
11740 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11741 if (argvars[1].v_type != VAR_UNKNOWN
11742 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011743 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011745 if (!error)
11746 {
11747 ExpandInit(&xpc);
11748 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011749 if (p_wic)
11750 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011751 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011752 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011753 }
11754 else
11755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756}
11757
11758/*
11759 * "globpath()" function
11760 */
11761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011763 typval_T *argvars;
11764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011766 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011768 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011769 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011771 /* When the optional second argument is non-zero, don't remove matches
11772 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11773 if (argvars[2].v_type != VAR_UNKNOWN
11774 && get_tv_number_chk(&argvars[2], &error))
11775 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011777 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011778 rettv->vval.v_string = NULL;
11779 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011780 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11781 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782}
11783
11784/*
11785 * "has()" function
11786 */
11787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 typval_T *argvars;
11790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791{
11792 int i;
11793 char_u *name;
11794 int n = FALSE;
11795 static char *(has_list[]) =
11796 {
11797#ifdef AMIGA
11798 "amiga",
11799# ifdef FEAT_ARP
11800 "arp",
11801# endif
11802#endif
11803#ifdef __BEOS__
11804 "beos",
11805#endif
11806#ifdef MSDOS
11807# ifdef DJGPP
11808 "dos32",
11809# else
11810 "dos16",
11811# endif
11812#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011813#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 "mac",
11815#endif
11816#if defined(MACOS_X_UNIX)
11817 "macunix",
11818#endif
11819#ifdef OS2
11820 "os2",
11821#endif
11822#ifdef __QNX__
11823 "qnx",
11824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825#ifdef UNIX
11826 "unix",
11827#endif
11828#ifdef VMS
11829 "vms",
11830#endif
11831#ifdef WIN16
11832 "win16",
11833#endif
11834#ifdef WIN32
11835 "win32",
11836#endif
11837#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11838 "win32unix",
11839#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011840#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 "win64",
11842#endif
11843#ifdef EBCDIC
11844 "ebcdic",
11845#endif
11846#ifndef CASE_INSENSITIVE_FILENAME
11847 "fname_case",
11848#endif
11849#ifdef FEAT_ARABIC
11850 "arabic",
11851#endif
11852#ifdef FEAT_AUTOCMD
11853 "autocmd",
11854#endif
11855#ifdef FEAT_BEVAL
11856 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011857# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11858 "balloon_multiline",
11859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860#endif
11861#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11862 "builtin_terms",
11863# ifdef ALL_BUILTIN_TCAPS
11864 "all_builtin_terms",
11865# endif
11866#endif
11867#ifdef FEAT_BYTEOFF
11868 "byte_offset",
11869#endif
11870#ifdef FEAT_CINDENT
11871 "cindent",
11872#endif
11873#ifdef FEAT_CLIENTSERVER
11874 "clientserver",
11875#endif
11876#ifdef FEAT_CLIPBOARD
11877 "clipboard",
11878#endif
11879#ifdef FEAT_CMDL_COMPL
11880 "cmdline_compl",
11881#endif
11882#ifdef FEAT_CMDHIST
11883 "cmdline_hist",
11884#endif
11885#ifdef FEAT_COMMENTS
11886 "comments",
11887#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011888#ifdef FEAT_CONCEAL
11889 "conceal",
11890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891#ifdef FEAT_CRYPT
11892 "cryptv",
11893#endif
11894#ifdef FEAT_CSCOPE
11895 "cscope",
11896#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011897#ifdef FEAT_CURSORBIND
11898 "cursorbind",
11899#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011900#ifdef CURSOR_SHAPE
11901 "cursorshape",
11902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903#ifdef DEBUG
11904 "debug",
11905#endif
11906#ifdef FEAT_CON_DIALOG
11907 "dialog_con",
11908#endif
11909#ifdef FEAT_GUI_DIALOG
11910 "dialog_gui",
11911#endif
11912#ifdef FEAT_DIFF
11913 "diff",
11914#endif
11915#ifdef FEAT_DIGRAPHS
11916 "digraphs",
11917#endif
11918#ifdef FEAT_DND
11919 "dnd",
11920#endif
11921#ifdef FEAT_EMACS_TAGS
11922 "emacs_tags",
11923#endif
11924 "eval", /* always present, of course! */
11925#ifdef FEAT_EX_EXTRA
11926 "ex_extra",
11927#endif
11928#ifdef FEAT_SEARCH_EXTRA
11929 "extra_search",
11930#endif
11931#ifdef FEAT_FKMAP
11932 "farsi",
11933#endif
11934#ifdef FEAT_SEARCHPATH
11935 "file_in_path",
11936#endif
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020011937#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011938 "filterpipe",
11939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940#ifdef FEAT_FIND_ID
11941 "find_in_path",
11942#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011943#ifdef FEAT_FLOAT
11944 "float",
11945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946#ifdef FEAT_FOLDING
11947 "folding",
11948#endif
11949#ifdef FEAT_FOOTER
11950 "footer",
11951#endif
11952#if !defined(USE_SYSTEM) && defined(UNIX)
11953 "fork",
11954#endif
11955#ifdef FEAT_GETTEXT
11956 "gettext",
11957#endif
11958#ifdef FEAT_GUI
11959 "gui",
11960#endif
11961#ifdef FEAT_GUI_ATHENA
11962# ifdef FEAT_GUI_NEXTAW
11963 "gui_neXtaw",
11964# else
11965 "gui_athena",
11966# endif
11967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968#ifdef FEAT_GUI_GTK
11969 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011972#ifdef FEAT_GUI_GNOME
11973 "gui_gnome",
11974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975#ifdef FEAT_GUI_MAC
11976 "gui_mac",
11977#endif
11978#ifdef FEAT_GUI_MOTIF
11979 "gui_motif",
11980#endif
11981#ifdef FEAT_GUI_PHOTON
11982 "gui_photon",
11983#endif
11984#ifdef FEAT_GUI_W16
11985 "gui_win16",
11986#endif
11987#ifdef FEAT_GUI_W32
11988 "gui_win32",
11989#endif
11990#ifdef FEAT_HANGULIN
11991 "hangul_input",
11992#endif
11993#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11994 "iconv",
11995#endif
11996#ifdef FEAT_INS_EXPAND
11997 "insert_expand",
11998#endif
11999#ifdef FEAT_JUMPLIST
12000 "jumplist",
12001#endif
12002#ifdef FEAT_KEYMAP
12003 "keymap",
12004#endif
12005#ifdef FEAT_LANGMAP
12006 "langmap",
12007#endif
12008#ifdef FEAT_LIBCALL
12009 "libcall",
12010#endif
12011#ifdef FEAT_LINEBREAK
12012 "linebreak",
12013#endif
12014#ifdef FEAT_LISP
12015 "lispindent",
12016#endif
12017#ifdef FEAT_LISTCMDS
12018 "listcmds",
12019#endif
12020#ifdef FEAT_LOCALMAP
12021 "localmap",
12022#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012023#ifdef FEAT_LUA
12024# ifndef DYNAMIC_LUA
12025 "lua",
12026# endif
12027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028#ifdef FEAT_MENU
12029 "menu",
12030#endif
12031#ifdef FEAT_SESSION
12032 "mksession",
12033#endif
12034#ifdef FEAT_MODIFY_FNAME
12035 "modify_fname",
12036#endif
12037#ifdef FEAT_MOUSE
12038 "mouse",
12039#endif
12040#ifdef FEAT_MOUSESHAPE
12041 "mouseshape",
12042#endif
12043#if defined(UNIX) || defined(VMS)
12044# ifdef FEAT_MOUSE_DEC
12045 "mouse_dec",
12046# endif
12047# ifdef FEAT_MOUSE_GPM
12048 "mouse_gpm",
12049# endif
12050# ifdef FEAT_MOUSE_JSB
12051 "mouse_jsbterm",
12052# endif
12053# ifdef FEAT_MOUSE_NET
12054 "mouse_netterm",
12055# endif
12056# ifdef FEAT_MOUSE_PTERM
12057 "mouse_pterm",
12058# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012059# ifdef FEAT_SYSMOUSE
12060 "mouse_sysmouse",
12061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062# ifdef FEAT_MOUSE_XTERM
12063 "mouse_xterm",
12064# endif
12065#endif
12066#ifdef FEAT_MBYTE
12067 "multi_byte",
12068#endif
12069#ifdef FEAT_MBYTE_IME
12070 "multi_byte_ime",
12071#endif
12072#ifdef FEAT_MULTI_LANG
12073 "multi_lang",
12074#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012075#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012076#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012077 "mzscheme",
12078#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080#ifdef FEAT_OLE
12081 "ole",
12082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083#ifdef FEAT_PATH_EXTRA
12084 "path_extra",
12085#endif
12086#ifdef FEAT_PERL
12087#ifndef DYNAMIC_PERL
12088 "perl",
12089#endif
12090#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012091#ifdef FEAT_PERSISTENT_UNDO
12092 "persistent_undo",
12093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094#ifdef FEAT_PYTHON
12095#ifndef DYNAMIC_PYTHON
12096 "python",
12097#endif
12098#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012099#ifdef FEAT_PYTHON3
12100#ifndef DYNAMIC_PYTHON3
12101 "python3",
12102#endif
12103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104#ifdef FEAT_POSTSCRIPT
12105 "postscript",
12106#endif
12107#ifdef FEAT_PRINTER
12108 "printer",
12109#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012110#ifdef FEAT_PROFILE
12111 "profile",
12112#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012113#ifdef FEAT_RELTIME
12114 "reltime",
12115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116#ifdef FEAT_QUICKFIX
12117 "quickfix",
12118#endif
12119#ifdef FEAT_RIGHTLEFT
12120 "rightleft",
12121#endif
12122#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12123 "ruby",
12124#endif
12125#ifdef FEAT_SCROLLBIND
12126 "scrollbind",
12127#endif
12128#ifdef FEAT_CMDL_INFO
12129 "showcmd",
12130 "cmdline_info",
12131#endif
12132#ifdef FEAT_SIGNS
12133 "signs",
12134#endif
12135#ifdef FEAT_SMARTINDENT
12136 "smartindent",
12137#endif
12138#ifdef FEAT_SNIFF
12139 "sniff",
12140#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012141#ifdef STARTUPTIME
12142 "startuptime",
12143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144#ifdef FEAT_STL_OPT
12145 "statusline",
12146#endif
12147#ifdef FEAT_SUN_WORKSHOP
12148 "sun_workshop",
12149#endif
12150#ifdef FEAT_NETBEANS_INTG
12151 "netbeans_intg",
12152#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012153#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012154 "spell",
12155#endif
12156#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 "syntax",
12158#endif
12159#if defined(USE_SYSTEM) || !defined(UNIX)
12160 "system",
12161#endif
12162#ifdef FEAT_TAG_BINS
12163 "tag_binary",
12164#endif
12165#ifdef FEAT_TAG_OLDSTATIC
12166 "tag_old_static",
12167#endif
12168#ifdef FEAT_TAG_ANYWHITE
12169 "tag_any_white",
12170#endif
12171#ifdef FEAT_TCL
12172# ifndef DYNAMIC_TCL
12173 "tcl",
12174# endif
12175#endif
12176#ifdef TERMINFO
12177 "terminfo",
12178#endif
12179#ifdef FEAT_TERMRESPONSE
12180 "termresponse",
12181#endif
12182#ifdef FEAT_TEXTOBJ
12183 "textobjects",
12184#endif
12185#ifdef HAVE_TGETENT
12186 "tgetent",
12187#endif
12188#ifdef FEAT_TITLE
12189 "title",
12190#endif
12191#ifdef FEAT_TOOLBAR
12192 "toolbar",
12193#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012194#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12195 "unnamedplus",
12196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197#ifdef FEAT_USR_CMDS
12198 "user-commands", /* was accidentally included in 5.4 */
12199 "user_commands",
12200#endif
12201#ifdef FEAT_VIMINFO
12202 "viminfo",
12203#endif
12204#ifdef FEAT_VERTSPLIT
12205 "vertsplit",
12206#endif
12207#ifdef FEAT_VIRTUALEDIT
12208 "virtualedit",
12209#endif
12210#ifdef FEAT_VISUAL
12211 "visual",
12212#endif
12213#ifdef FEAT_VISUALEXTRA
12214 "visualextra",
12215#endif
12216#ifdef FEAT_VREPLACE
12217 "vreplace",
12218#endif
12219#ifdef FEAT_WILDIGN
12220 "wildignore",
12221#endif
12222#ifdef FEAT_WILDMENU
12223 "wildmenu",
12224#endif
12225#ifdef FEAT_WINDOWS
12226 "windows",
12227#endif
12228#ifdef FEAT_WAK
12229 "winaltkeys",
12230#endif
12231#ifdef FEAT_WRITEBACKUP
12232 "writebackup",
12233#endif
12234#ifdef FEAT_XIM
12235 "xim",
12236#endif
12237#ifdef FEAT_XFONTSET
12238 "xfontset",
12239#endif
12240#ifdef USE_XSMP
12241 "xsmp",
12242#endif
12243#ifdef USE_XSMP_INTERACT
12244 "xsmp_interact",
12245#endif
12246#ifdef FEAT_XCLIPBOARD
12247 "xterm_clipboard",
12248#endif
12249#ifdef FEAT_XTERM_SAVE
12250 "xterm_save",
12251#endif
12252#if defined(UNIX) && defined(FEAT_X11)
12253 "X11",
12254#endif
12255 NULL
12256 };
12257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 for (i = 0; has_list[i] != NULL; ++i)
12260 if (STRICMP(name, has_list[i]) == 0)
12261 {
12262 n = TRUE;
12263 break;
12264 }
12265
12266 if (n == FALSE)
12267 {
12268 if (STRNICMP(name, "patch", 5) == 0)
12269 n = has_patch(atoi((char *)name + 5));
12270 else if (STRICMP(name, "vim_starting") == 0)
12271 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012272#ifdef FEAT_MBYTE
12273 else if (STRICMP(name, "multi_byte_encoding") == 0)
12274 n = has_mbyte;
12275#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012276#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12277 else if (STRICMP(name, "balloon_multiline") == 0)
12278 n = multiline_balloon_available();
12279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280#ifdef DYNAMIC_TCL
12281 else if (STRICMP(name, "tcl") == 0)
12282 n = tcl_enabled(FALSE);
12283#endif
12284#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12285 else if (STRICMP(name, "iconv") == 0)
12286 n = iconv_enabled(FALSE);
12287#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012288#ifdef DYNAMIC_LUA
12289 else if (STRICMP(name, "lua") == 0)
12290 n = lua_enabled(FALSE);
12291#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012292#ifdef DYNAMIC_MZSCHEME
12293 else if (STRICMP(name, "mzscheme") == 0)
12294 n = mzscheme_enabled(FALSE);
12295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296#ifdef DYNAMIC_RUBY
12297 else if (STRICMP(name, "ruby") == 0)
12298 n = ruby_enabled(FALSE);
12299#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012300#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301#ifdef DYNAMIC_PYTHON
12302 else if (STRICMP(name, "python") == 0)
12303 n = python_enabled(FALSE);
12304#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012305#endif
12306#ifdef FEAT_PYTHON3
12307#ifdef DYNAMIC_PYTHON3
12308 else if (STRICMP(name, "python3") == 0)
12309 n = python3_enabled(FALSE);
12310#endif
12311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312#ifdef DYNAMIC_PERL
12313 else if (STRICMP(name, "perl") == 0)
12314 n = perl_enabled(FALSE);
12315#endif
12316#ifdef FEAT_GUI
12317 else if (STRICMP(name, "gui_running") == 0)
12318 n = (gui.in_use || gui.starting);
12319# ifdef FEAT_GUI_W32
12320 else if (STRICMP(name, "gui_win32s") == 0)
12321 n = gui_is_win32s();
12322# endif
12323# ifdef FEAT_BROWSE
12324 else if (STRICMP(name, "browse") == 0)
12325 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12326# endif
12327#endif
12328#ifdef FEAT_SYN_HL
12329 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012330 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331#endif
12332#if defined(WIN3264)
12333 else if (STRICMP(name, "win95") == 0)
12334 n = mch_windows95();
12335#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012336#ifdef FEAT_NETBEANS_INTG
12337 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012338 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 }
12341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343}
12344
12345/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012346 * "has_key()" function
12347 */
12348 static void
12349f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012350 typval_T *argvars;
12351 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012352{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012353 if (argvars[0].v_type != VAR_DICT)
12354 {
12355 EMSG(_(e_dictreq));
12356 return;
12357 }
12358 if (argvars[0].vval.v_dict == NULL)
12359 return;
12360
12361 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012362 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012363}
12364
12365/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012366 * "haslocaldir()" function
12367 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012368 static void
12369f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012370 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012371 typval_T *rettv;
12372{
12373 rettv->vval.v_number = (curwin->w_localdir != NULL);
12374}
12375
12376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 * "hasmapto()" function
12378 */
12379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 typval_T *argvars;
12382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383{
12384 char_u *name;
12385 char_u *mode;
12386 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012387 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012390 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391 mode = (char_u *)"nvo";
12392 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012395 if (argvars[2].v_type != VAR_UNKNOWN)
12396 abbr = get_tv_number(&argvars[2]);
12397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398
Bram Moolenaar2c932302006-03-18 21:42:09 +000012399 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012400 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403}
12404
12405/*
12406 * "histadd()" function
12407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012410 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412{
12413#ifdef FEAT_CMDHIST
12414 int histype;
12415 char_u *str;
12416 char_u buf[NUMBUFLEN];
12417#endif
12418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012419 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 if (check_restricted() || check_secure())
12421 return;
12422#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012423 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12424 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 if (histype >= 0)
12426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012427 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 if (*str != NUL)
12429 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012430 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012432 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 return;
12434 }
12435 }
12436#endif
12437}
12438
12439/*
12440 * "histdel()" function
12441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012444 typval_T *argvars UNUSED;
12445 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446{
12447#ifdef FEAT_CMDHIST
12448 int n;
12449 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012450 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012452 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12453 if (str == NULL)
12454 n = 0;
12455 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012457 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012458 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012460 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012461 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 else
12463 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012464 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465 get_tv_string_buf(&argvars[1], buf));
12466 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467#endif
12468}
12469
12470/*
12471 * "histget()" function
12472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012475 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477{
12478#ifdef FEAT_CMDHIST
12479 int type;
12480 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012481 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012483 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12484 if (str == NULL)
12485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012487 {
12488 type = get_histtype(str);
12489 if (argvars[1].v_type == VAR_UNKNOWN)
12490 idx = get_history_idx(type);
12491 else
12492 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12493 /* -1 on type error */
12494 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012499 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500}
12501
12502/*
12503 * "histnr()" function
12504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509{
12510 int i;
12511
12512#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 char_u *history = get_tv_string_chk(&argvars[0]);
12514
12515 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516 if (i >= HIST_CMD && i < HIST_COUNT)
12517 i = get_history_idx(i);
12518 else
12519#endif
12520 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522}
12523
12524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525 * "highlightID(name)" function
12526 */
12527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012529 typval_T *argvars;
12530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533}
12534
12535/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012536 * "highlight_exists()" function
12537 */
12538 static void
12539f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012540 typval_T *argvars;
12541 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542{
12543 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12544}
12545
12546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547 * "hostname()" function
12548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012550f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012551 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553{
12554 char_u hostname[256];
12555
12556 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012557 rettv->v_type = VAR_STRING;
12558 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559}
12560
12561/*
12562 * iconv() function
12563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012565f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012566 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568{
12569#ifdef FEAT_MBYTE
12570 char_u buf1[NUMBUFLEN];
12571 char_u buf2[NUMBUFLEN];
12572 char_u *from, *to, *str;
12573 vimconv_T vimconv;
12574#endif
12575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012576 rettv->v_type = VAR_STRING;
12577 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578
12579#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580 str = get_tv_string(&argvars[0]);
12581 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12582 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 vimconv.vc_type = CONV_NONE;
12584 convert_setup(&vimconv, from, to);
12585
12586 /* If the encodings are equal, no conversion needed. */
12587 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012588 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012590 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591
12592 convert_setup(&vimconv, NULL, NULL);
12593 vim_free(from);
12594 vim_free(to);
12595#endif
12596}
12597
12598/*
12599 * "indent()" function
12600 */
12601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012602f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012603 typval_T *argvars;
12604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605{
12606 linenr_T lnum;
12607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012608 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613}
12614
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012615/*
12616 * "index()" function
12617 */
12618 static void
12619f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012620 typval_T *argvars;
12621 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012622{
Bram Moolenaar33570922005-01-25 22:26:29 +000012623 list_T *l;
12624 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012625 long idx = 0;
12626 int ic = FALSE;
12627
12628 rettv->vval.v_number = -1;
12629 if (argvars[0].v_type != VAR_LIST)
12630 {
12631 EMSG(_(e_listreq));
12632 return;
12633 }
12634 l = argvars[0].vval.v_list;
12635 if (l != NULL)
12636 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012637 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012638 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 int error = FALSE;
12641
Bram Moolenaar758711c2005-02-02 23:11:38 +000012642 /* Start at specified item. Use the cached index that list_find()
12643 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012644 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012645 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012646 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012647 ic = get_tv_number_chk(&argvars[3], &error);
12648 if (error)
12649 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012650 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012651
Bram Moolenaar758711c2005-02-02 23:11:38 +000012652 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012653 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012654 {
12655 rettv->vval.v_number = idx;
12656 break;
12657 }
12658 }
12659}
12660
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661static int inputsecret_flag = 0;
12662
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012663static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12664
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012666 * This function is used by f_input() and f_inputdialog() functions. The third
12667 * argument to f_input() specifies the type of completion to use at the
12668 * prompt. The third argument to f_inputdialog() specifies the value to return
12669 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670 */
12671 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012672get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012673 typval_T *argvars;
12674 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012675 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012677 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 char_u *p = NULL;
12679 int c;
12680 char_u buf[NUMBUFLEN];
12681 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012682 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012683 int xp_type = EXPAND_NOTHING;
12684 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688
12689#ifdef NO_CONSOLE_INPUT
12690 /* While starting up, there is no place to enter text. */
12691 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693#endif
12694
12695 cmd_silent = FALSE; /* Want to see the prompt. */
12696 if (prompt != NULL)
12697 {
12698 /* Only the part of the message after the last NL is considered as
12699 * prompt for the command line */
12700 p = vim_strrchr(prompt, '\n');
12701 if (p == NULL)
12702 p = prompt;
12703 else
12704 {
12705 ++p;
12706 c = *p;
12707 *p = NUL;
12708 msg_start();
12709 msg_clr_eos();
12710 msg_puts_attr(prompt, echo_attr);
12711 msg_didout = FALSE;
12712 msg_starthere();
12713 *p = c;
12714 }
12715 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 if (argvars[1].v_type != VAR_UNKNOWN)
12718 {
12719 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12720 if (defstr != NULL)
12721 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012723 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012724 {
12725 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012726 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012727 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012728
Bram Moolenaar4463f292005-09-25 22:20:24 +000012729 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012730
Bram Moolenaar4463f292005-09-25 22:20:24 +000012731 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12732 if (xp_name == NULL)
12733 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012734
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012735 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012736
Bram Moolenaar4463f292005-09-25 22:20:24 +000012737 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12738 &xp_arg) == FAIL)
12739 return;
12740 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012741 }
12742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012743 if (defstr != NULL)
12744 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012745 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12746 xp_type, xp_arg);
12747
12748 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012750 /* since the user typed this, no need to wait for return */
12751 need_wait_return = FALSE;
12752 msg_didout = FALSE;
12753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 cmd_silent = cmd_silent_save;
12755}
12756
12757/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012758 * "input()" function
12759 * Also handles inputsecret() when inputsecret is set.
12760 */
12761 static void
12762f_input(argvars, rettv)
12763 typval_T *argvars;
12764 typval_T *rettv;
12765{
12766 get_user_input(argvars, rettv, FALSE);
12767}
12768
12769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 * "inputdialog()" function
12771 */
12772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012774 typval_T *argvars;
12775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776{
12777#if defined(FEAT_GUI_TEXTDIALOG)
12778 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12779 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12780 {
12781 char_u *message;
12782 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012785 message = get_tv_string_chk(&argvars[0]);
12786 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012787 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012788 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 else
12790 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012791 if (message != NULL && defstr != NULL
12792 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012793 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 else
12796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 if (message != NULL && defstr != NULL
12798 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012799 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->vval.v_string = vim_strsave(
12801 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 }
12807 else
12808#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012809 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810}
12811
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012812/*
12813 * "inputlist()" function
12814 */
12815 static void
12816f_inputlist(argvars, rettv)
12817 typval_T *argvars;
12818 typval_T *rettv;
12819{
12820 listitem_T *li;
12821 int selected;
12822 int mouse_used;
12823
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012824#ifdef NO_CONSOLE_INPUT
12825 /* While starting up, there is no place to enter text. */
12826 if (no_console_input())
12827 return;
12828#endif
12829 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12830 {
12831 EMSG2(_(e_listarg), "inputlist()");
12832 return;
12833 }
12834
12835 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012836 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012837 lines_left = Rows; /* avoid more prompt */
12838 msg_scroll = TRUE;
12839 msg_clr_eos();
12840
12841 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12842 {
12843 msg_puts(get_tv_string(&li->li_tv));
12844 msg_putchar('\n');
12845 }
12846
12847 /* Ask for choice. */
12848 selected = prompt_for_number(&mouse_used);
12849 if (mouse_used)
12850 selected -= lines_left;
12851
12852 rettv->vval.v_number = selected;
12853}
12854
12855
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12857
12858/*
12859 * "inputrestore()" function
12860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012863 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865{
12866 if (ga_userinput.ga_len > 0)
12867 {
12868 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12870 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012871 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 }
12873 else if (p_verbose > 1)
12874 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012875 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 }
12878}
12879
12880/*
12881 * "inputsave()" function
12882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012885 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012888 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 if (ga_grow(&ga_userinput, 1) == OK)
12890 {
12891 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12892 + ga_userinput.ga_len);
12893 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012894 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 }
12896 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898}
12899
12900/*
12901 * "inputsecret()" function
12902 */
12903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012905 typval_T *argvars;
12906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907{
12908 ++cmdline_star;
12909 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 --cmdline_star;
12912 --inputsecret_flag;
12913}
12914
12915/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012916 * "insert()" function
12917 */
12918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 typval_T *argvars;
12921 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012922{
12923 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012924 listitem_T *item;
12925 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012926 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012927
12928 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012929 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012930 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012931 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012932 {
12933 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 before = get_tv_number_chk(&argvars[2], &error);
12935 if (error)
12936 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012937
Bram Moolenaar758711c2005-02-02 23:11:38 +000012938 if (before == l->lv_len)
12939 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012940 else
12941 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012942 item = list_find(l, before);
12943 if (item == NULL)
12944 {
12945 EMSGN(_(e_listidx), before);
12946 l = NULL;
12947 }
12948 }
12949 if (l != NULL)
12950 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012951 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012952 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012953 }
12954 }
12955}
12956
12957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 * "isdirectory()" function
12959 */
12960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012962 typval_T *argvars;
12963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966}
12967
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012968/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012969 * "islocked()" function
12970 */
12971 static void
12972f_islocked(argvars, rettv)
12973 typval_T *argvars;
12974 typval_T *rettv;
12975{
12976 lval_T lv;
12977 char_u *end;
12978 dictitem_T *di;
12979
12980 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012981 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12982 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012983 if (end != NULL && lv.ll_name != NULL)
12984 {
12985 if (*end != NUL)
12986 EMSG(_(e_trailing));
12987 else
12988 {
12989 if (lv.ll_tv == NULL)
12990 {
12991 if (check_changedtick(lv.ll_name))
12992 rettv->vval.v_number = 1; /* always locked */
12993 else
12994 {
12995 di = find_var(lv.ll_name, NULL);
12996 if (di != NULL)
12997 {
12998 /* Consider a variable locked when:
12999 * 1. the variable itself is locked
13000 * 2. the value of the variable is locked.
13001 * 3. the List or Dict value is locked.
13002 */
13003 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13004 || tv_islocked(&di->di_tv));
13005 }
13006 }
13007 }
13008 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013009 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013010 else if (lv.ll_newkey != NULL)
13011 EMSG2(_(e_dictkey), lv.ll_newkey);
13012 else if (lv.ll_list != NULL)
13013 /* List item. */
13014 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13015 else
13016 /* Dictionary item. */
13017 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13018 }
13019 }
13020
13021 clear_lval(&lv);
13022}
13023
Bram Moolenaar33570922005-01-25 22:26:29 +000013024static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013025
13026/*
13027 * Turn a dict into a list:
13028 * "what" == 0: list of keys
13029 * "what" == 1: list of values
13030 * "what" == 2: list of items
13031 */
13032 static void
13033dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 typval_T *argvars;
13035 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013036 int what;
13037{
Bram Moolenaar33570922005-01-25 22:26:29 +000013038 list_T *l2;
13039 dictitem_T *di;
13040 hashitem_T *hi;
13041 listitem_T *li;
13042 listitem_T *li2;
13043 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013044 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013045
Bram Moolenaar8c711452005-01-14 21:53:12 +000013046 if (argvars[0].v_type != VAR_DICT)
13047 {
13048 EMSG(_(e_dictreq));
13049 return;
13050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013051 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013052 return;
13053
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013054 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013055 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013057 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013058 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013060 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013062 --todo;
13063 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013064
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013065 li = listitem_alloc();
13066 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013067 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013068 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013069
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013070 if (what == 0)
13071 {
13072 /* keys() */
13073 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013074 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013075 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13076 }
13077 else if (what == 1)
13078 {
13079 /* values() */
13080 copy_tv(&di->di_tv, &li->li_tv);
13081 }
13082 else
13083 {
13084 /* items() */
13085 l2 = list_alloc();
13086 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013087 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013088 li->li_tv.vval.v_list = l2;
13089 if (l2 == NULL)
13090 break;
13091 ++l2->lv_refcount;
13092
13093 li2 = listitem_alloc();
13094 if (li2 == NULL)
13095 break;
13096 list_append(l2, li2);
13097 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013098 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013099 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13100
13101 li2 = listitem_alloc();
13102 if (li2 == NULL)
13103 break;
13104 list_append(l2, li2);
13105 copy_tv(&di->di_tv, &li2->li_tv);
13106 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013107 }
13108 }
13109}
13110
13111/*
13112 * "items(dict)" function
13113 */
13114 static void
13115f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013116 typval_T *argvars;
13117 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013118{
13119 dict_list(argvars, rettv, 2);
13120}
13121
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013123 * "join()" function
13124 */
13125 static void
13126f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013127 typval_T *argvars;
13128 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013129{
13130 garray_T ga;
13131 char_u *sep;
13132
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013133 if (argvars[0].v_type != VAR_LIST)
13134 {
13135 EMSG(_(e_listreq));
13136 return;
13137 }
13138 if (argvars[0].vval.v_list == NULL)
13139 return;
13140 if (argvars[1].v_type == VAR_UNKNOWN)
13141 sep = (char_u *)" ";
13142 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013143 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013144
13145 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146
13147 if (sep != NULL)
13148 {
13149 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013150 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013151 ga_append(&ga, NUL);
13152 rettv->vval.v_string = (char_u *)ga.ga_data;
13153 }
13154 else
13155 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013156}
13157
13158/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013159 * "keys()" function
13160 */
13161 static void
13162f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *argvars;
13164 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013165{
13166 dict_list(argvars, rettv, 0);
13167}
13168
13169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 * "last_buffer_nr()" function.
13171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013174 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176{
13177 int n = 0;
13178 buf_T *buf;
13179
13180 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13181 if (n < buf->b_fnum)
13182 n = buf->b_fnum;
13183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013184 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013185}
13186
13187/*
13188 * "len()" function
13189 */
13190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013192 typval_T *argvars;
13193 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013194{
13195 switch (argvars[0].v_type)
13196 {
13197 case VAR_STRING:
13198 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199 rettv->vval.v_number = (varnumber_T)STRLEN(
13200 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013201 break;
13202 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013204 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013205 case VAR_DICT:
13206 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13207 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013208 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013209 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210 break;
13211 }
13212}
13213
Bram Moolenaar33570922005-01-25 22:26:29 +000013214static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013215
13216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013218 typval_T *argvars;
13219 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013220 int type;
13221{
13222#ifdef FEAT_LIBCALL
13223 char_u *string_in;
13224 char_u **string_result;
13225 int nr_result;
13226#endif
13227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013229 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013231
13232 if (check_restricted() || check_secure())
13233 return;
13234
13235#ifdef FEAT_LIBCALL
13236 /* The first two args must be strings, otherwise its meaningless */
13237 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13238 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013239 string_in = NULL;
13240 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013241 string_in = argvars[2].vval.v_string;
13242 if (type == VAR_NUMBER)
13243 string_result = NULL;
13244 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013246 if (mch_libcall(argvars[0].vval.v_string,
13247 argvars[1].vval.v_string,
13248 string_in,
13249 argvars[2].vval.v_number,
13250 string_result,
13251 &nr_result) == OK
13252 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013253 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013254 }
13255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256}
13257
13258/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013259 * "libcall()" function
13260 */
13261 static void
13262f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013263 typval_T *argvars;
13264 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013265{
13266 libcall_common(argvars, rettv, VAR_STRING);
13267}
13268
13269/*
13270 * "libcallnr()" function
13271 */
13272 static void
13273f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013274 typval_T *argvars;
13275 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013276{
13277 libcall_common(argvars, rettv, VAR_NUMBER);
13278}
13279
13280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 * "line(string)" function
13282 */
13283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013285 typval_T *argvars;
13286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287{
13288 linenr_T lnum = 0;
13289 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013290 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013292 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 if (fp != NULL)
13294 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296}
13297
13298/*
13299 * "line2byte(lnum)" function
13300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013303 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305{
13306#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308#else
13309 linenr_T lnum;
13310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13316 if (rettv->vval.v_number >= 0)
13317 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318#endif
13319}
13320
13321/*
13322 * "lispindent(lnum)" function
13323 */
13324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013325f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 typval_T *argvars;
13327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328{
13329#ifdef FEAT_LISP
13330 pos_T pos;
13331 linenr_T lnum;
13332
13333 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13336 {
13337 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013338 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 curwin->w_cursor = pos;
13340 }
13341 else
13342#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344}
13345
13346/*
13347 * "localtime()" function
13348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013351 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355}
13356
Bram Moolenaar33570922005-01-25 22:26:29 +000013357static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358
13359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013361 typval_T *argvars;
13362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 int exact;
13364{
13365 char_u *keys;
13366 char_u *which;
13367 char_u buf[NUMBUFLEN];
13368 char_u *keys_buf = NULL;
13369 char_u *rhs;
13370 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013371 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013372 int get_dict = FALSE;
13373 mapblock_T *mp;
13374 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375
13376 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 rettv->v_type = VAR_STRING;
13378 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013380 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381 if (*keys == NUL)
13382 return;
13383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013384 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013387 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013388 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013389 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013390 if (argvars[3].v_type != VAR_UNKNOWN)
13391 get_dict = get_tv_number(&argvars[3]);
13392 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 else
13395 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013396 if (which == NULL)
13397 return;
13398
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 mode = get_map_mode(&which, 0);
13400
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013401 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013402 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013404
13405 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013407 /* Return a string. */
13408 if (rhs != NULL)
13409 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410
Bram Moolenaarbd743252010-10-20 21:23:33 +020013411 }
13412 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13413 {
13414 /* Return a dictionary. */
13415 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13416 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13417 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418
Bram Moolenaarbd743252010-10-20 21:23:33 +020013419 dict_add_nr_str(dict, "lhs", 0L, lhs);
13420 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13421 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13422 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13423 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13424 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13425 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13426 dict_add_nr_str(dict, "mode", 0L, mapmode);
13427
13428 vim_free(lhs);
13429 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 }
13431}
13432
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013433#ifdef FEAT_FLOAT
13434/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013435 * "log()" function
13436 */
13437 static void
13438f_log(argvars, rettv)
13439 typval_T *argvars;
13440 typval_T *rettv;
13441{
13442 float_T f;
13443
13444 rettv->v_type = VAR_FLOAT;
13445 if (get_float_arg(argvars, &f) == OK)
13446 rettv->vval.v_float = log(f);
13447 else
13448 rettv->vval.v_float = 0.0;
13449}
13450
13451/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013452 * "log10()" function
13453 */
13454 static void
13455f_log10(argvars, rettv)
13456 typval_T *argvars;
13457 typval_T *rettv;
13458{
13459 float_T f;
13460
13461 rettv->v_type = VAR_FLOAT;
13462 if (get_float_arg(argvars, &f) == OK)
13463 rettv->vval.v_float = log10(f);
13464 else
13465 rettv->vval.v_float = 0.0;
13466}
13467#endif
13468
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013470 * "map()" function
13471 */
13472 static void
13473f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013474 typval_T *argvars;
13475 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013476{
13477 filter_map(argvars, rettv, TRUE);
13478}
13479
13480/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013481 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 */
13483 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013484f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013485 typval_T *argvars;
13486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013488 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489}
13490
13491/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 */
13494 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013495f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013496 typval_T *argvars;
13497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500}
13501
Bram Moolenaar33570922005-01-25 22:26:29 +000013502static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503
13504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013506 typval_T *argvars;
13507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 int type;
13509{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013510 char_u *str = NULL;
13511 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 char_u *pat;
13513 regmatch_T regmatch;
13514 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013515 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 char_u *save_cpo;
13517 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013518 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013519 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013520 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013521 list_T *l = NULL;
13522 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013523 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013524 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525
13526 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13527 save_cpo = p_cpo;
13528 p_cpo = (char_u *)"";
13529
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013530 rettv->vval.v_number = -1;
13531 if (type == 3)
13532 {
13533 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013534 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013535 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013536 }
13537 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539 rettv->v_type = VAR_STRING;
13540 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013543 if (argvars[0].v_type == VAR_LIST)
13544 {
13545 if ((l = argvars[0].vval.v_list) == NULL)
13546 goto theend;
13547 li = l->lv_first;
13548 }
13549 else
13550 expr = str = get_tv_string(&argvars[0]);
13551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013552 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13553 if (pat == NULL)
13554 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013555
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013558 int error = FALSE;
13559
13560 start = get_tv_number_chk(&argvars[2], &error);
13561 if (error)
13562 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013563 if (l != NULL)
13564 {
13565 li = list_find(l, start);
13566 if (li == NULL)
13567 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013568 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013569 }
13570 else
13571 {
13572 if (start < 0)
13573 start = 0;
13574 if (start > (long)STRLEN(str))
13575 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013576 /* When "count" argument is there ignore matches before "start",
13577 * otherwise skip part of the string. Differs when pattern is "^"
13578 * or "\<". */
13579 if (argvars[3].v_type != VAR_UNKNOWN)
13580 startcol = start;
13581 else
13582 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013583 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013584
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013585 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 nth = get_tv_number_chk(&argvars[3], &error);
13587 if (error)
13588 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 }
13590
13591 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13592 if (regmatch.regprog != NULL)
13593 {
13594 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013595
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013596 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013597 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013598 if (l != NULL)
13599 {
13600 if (li == NULL)
13601 {
13602 match = FALSE;
13603 break;
13604 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013605 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013606 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013607 if (str == NULL)
13608 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013609 }
13610
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013611 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013612
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013613 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013614 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013615 if (l == NULL && !match)
13616 break;
13617
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013618 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013619 if (l != NULL)
13620 {
13621 li = li->li_next;
13622 ++idx;
13623 }
13624 else
13625 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013626#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013627 startcol = (colnr_T)(regmatch.startp[0]
13628 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013629#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013630 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013631#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013632 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013633 }
13634
13635 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013637 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013638 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013639 int i;
13640
13641 /* return list with matched string and submatches */
13642 for (i = 0; i < NSUBEXP; ++i)
13643 {
13644 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013645 {
13646 if (list_append_string(rettv->vval.v_list,
13647 (char_u *)"", 0) == FAIL)
13648 break;
13649 }
13650 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013651 regmatch.startp[i],
13652 (int)(regmatch.endp[i] - regmatch.startp[i]))
13653 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013654 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013655 }
13656 }
13657 else if (type == 2)
13658 {
13659 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013660 if (l != NULL)
13661 copy_tv(&li->li_tv, rettv);
13662 else
13663 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013665 }
13666 else if (l != NULL)
13667 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 else
13669 {
13670 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 (varnumber_T)(regmatch.startp[0] - str);
13673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013676 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 }
13678 }
13679 vim_free(regmatch.regprog);
13680 }
13681
13682theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013683 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684 p_cpo = save_cpo;
13685}
13686
13687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013688 * "match()" function
13689 */
13690 static void
13691f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013692 typval_T *argvars;
13693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013694{
13695 find_some_match(argvars, rettv, 1);
13696}
13697
13698/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013699 * "matchadd()" function
13700 */
13701 static void
13702f_matchadd(argvars, rettv)
13703 typval_T *argvars;
13704 typval_T *rettv;
13705{
13706#ifdef FEAT_SEARCH_EXTRA
13707 char_u buf[NUMBUFLEN];
13708 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13709 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13710 int prio = 10; /* default priority */
13711 int id = -1;
13712 int error = FALSE;
13713
13714 rettv->vval.v_number = -1;
13715
13716 if (grp == NULL || pat == NULL)
13717 return;
13718 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013719 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013720 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013721 if (argvars[3].v_type != VAR_UNKNOWN)
13722 id = get_tv_number_chk(&argvars[3], &error);
13723 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013724 if (error == TRUE)
13725 return;
13726 if (id >= 1 && id <= 3)
13727 {
13728 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13729 return;
13730 }
13731
13732 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13733#endif
13734}
13735
13736/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013737 * "matcharg()" function
13738 */
13739 static void
13740f_matcharg(argvars, rettv)
13741 typval_T *argvars;
13742 typval_T *rettv;
13743{
13744 if (rettv_list_alloc(rettv) == OK)
13745 {
13746#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013747 int id = get_tv_number(&argvars[0]);
13748 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013749
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013750 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013751 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013752 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13753 {
13754 list_append_string(rettv->vval.v_list,
13755 syn_id2name(m->hlg_id), -1);
13756 list_append_string(rettv->vval.v_list, m->pattern, -1);
13757 }
13758 else
13759 {
13760 list_append_string(rettv->vval.v_list, NUL, -1);
13761 list_append_string(rettv->vval.v_list, NUL, -1);
13762 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013763 }
13764#endif
13765 }
13766}
13767
13768/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013769 * "matchdelete()" function
13770 */
13771 static void
13772f_matchdelete(argvars, rettv)
13773 typval_T *argvars;
13774 typval_T *rettv;
13775{
13776#ifdef FEAT_SEARCH_EXTRA
13777 rettv->vval.v_number = match_delete(curwin,
13778 (int)get_tv_number(&argvars[0]), TRUE);
13779#endif
13780}
13781
13782/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013783 * "matchend()" function
13784 */
13785 static void
13786f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013787 typval_T *argvars;
13788 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789{
13790 find_some_match(argvars, rettv, 0);
13791}
13792
13793/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013794 * "matchlist()" function
13795 */
13796 static void
13797f_matchlist(argvars, rettv)
13798 typval_T *argvars;
13799 typval_T *rettv;
13800{
13801 find_some_match(argvars, rettv, 3);
13802}
13803
13804/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013805 * "matchstr()" function
13806 */
13807 static void
13808f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013809 typval_T *argvars;
13810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013811{
13812 find_some_match(argvars, rettv, 2);
13813}
13814
Bram Moolenaar33570922005-01-25 22:26:29 +000013815static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013816
13817 static void
13818max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013819 typval_T *argvars;
13820 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013821 int domax;
13822{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013823 long n = 0;
13824 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013826
13827 if (argvars[0].v_type == VAR_LIST)
13828 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013829 list_T *l;
13830 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013831
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013832 l = argvars[0].vval.v_list;
13833 if (l != NULL)
13834 {
13835 li = l->lv_first;
13836 if (li != NULL)
13837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013838 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013839 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013840 {
13841 li = li->li_next;
13842 if (li == NULL)
13843 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013845 if (domax ? i > n : i < n)
13846 n = i;
13847 }
13848 }
13849 }
13850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013851 else if (argvars[0].v_type == VAR_DICT)
13852 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013853 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013854 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013855 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013856 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013857
13858 d = argvars[0].vval.v_dict;
13859 if (d != NULL)
13860 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013861 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013862 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013863 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013864 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013865 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013866 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013867 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013868 if (first)
13869 {
13870 n = i;
13871 first = FALSE;
13872 }
13873 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013874 n = i;
13875 }
13876 }
13877 }
13878 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013879 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013880 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013882}
13883
13884/*
13885 * "max()" function
13886 */
13887 static void
13888f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013889 typval_T *argvars;
13890 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013891{
13892 max_min(argvars, rettv, TRUE);
13893}
13894
13895/*
13896 * "min()" function
13897 */
13898 static void
13899f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013900 typval_T *argvars;
13901 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013902{
13903 max_min(argvars, rettv, FALSE);
13904}
13905
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013906static int mkdir_recurse __ARGS((char_u *dir, int prot));
13907
13908/*
13909 * Create the directory in which "dir" is located, and higher levels when
13910 * needed.
13911 */
13912 static int
13913mkdir_recurse(dir, prot)
13914 char_u *dir;
13915 int prot;
13916{
13917 char_u *p;
13918 char_u *updir;
13919 int r = FAIL;
13920
13921 /* Get end of directory name in "dir".
13922 * We're done when it's "/" or "c:/". */
13923 p = gettail_sep(dir);
13924 if (p <= get_past_head(dir))
13925 return OK;
13926
13927 /* If the directory exists we're done. Otherwise: create it.*/
13928 updir = vim_strnsave(dir, (int)(p - dir));
13929 if (updir == NULL)
13930 return FAIL;
13931 if (mch_isdir(updir))
13932 r = OK;
13933 else if (mkdir_recurse(updir, prot) == OK)
13934 r = vim_mkdir_emsg(updir, prot);
13935 vim_free(updir);
13936 return r;
13937}
13938
13939#ifdef vim_mkdir
13940/*
13941 * "mkdir()" function
13942 */
13943 static void
13944f_mkdir(argvars, rettv)
13945 typval_T *argvars;
13946 typval_T *rettv;
13947{
13948 char_u *dir;
13949 char_u buf[NUMBUFLEN];
13950 int prot = 0755;
13951
13952 rettv->vval.v_number = FAIL;
13953 if (check_restricted() || check_secure())
13954 return;
13955
13956 dir = get_tv_string_buf(&argvars[0], buf);
13957 if (argvars[1].v_type != VAR_UNKNOWN)
13958 {
13959 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 prot = get_tv_number_chk(&argvars[2], NULL);
13961 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013962 mkdir_recurse(dir, prot);
13963 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013964 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013965}
13966#endif
13967
Bram Moolenaar0d660222005-01-07 21:51:51 +000013968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 * "mode()" function
13970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013972f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013973 typval_T *argvars;
13974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013976 char_u buf[3];
13977
13978 buf[1] = NUL;
13979 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980
13981#ifdef FEAT_VISUAL
13982 if (VIsual_active)
13983 {
13984 if (VIsual_select)
13985 buf[0] = VIsual_mode + 's' - 'v';
13986 else
13987 buf[0] = VIsual_mode;
13988 }
13989 else
13990#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013991 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13992 || State == CONFIRM)
13993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013995 if (State == ASKMORE)
13996 buf[1] = 'm';
13997 else if (State == CONFIRM)
13998 buf[1] = '?';
13999 }
14000 else if (State == EXTERNCMD)
14001 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 else if (State & INSERT)
14003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014004#ifdef FEAT_VREPLACE
14005 if (State & VREPLACE_FLAG)
14006 {
14007 buf[0] = 'R';
14008 buf[1] = 'v';
14009 }
14010 else
14011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 if (State & REPLACE_FLAG)
14013 buf[0] = 'R';
14014 else
14015 buf[0] = 'i';
14016 }
14017 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014020 if (exmode_active)
14021 buf[1] = 'v';
14022 }
14023 else if (exmode_active)
14024 {
14025 buf[0] = 'c';
14026 buf[1] = 'e';
14027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014031 if (finish_op)
14032 buf[1] = 'o';
14033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014035 /* Clear out the minor mode when the argument is not a non-zero number or
14036 * non-empty string. */
14037 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014038 buf[1] = NUL;
14039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014040 rettv->vval.v_string = vim_strsave(buf);
14041 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042}
14043
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014044#ifdef FEAT_MZSCHEME
14045/*
14046 * "mzeval()" function
14047 */
14048 static void
14049f_mzeval(argvars, rettv)
14050 typval_T *argvars;
14051 typval_T *rettv;
14052{
14053 char_u *str;
14054 char_u buf[NUMBUFLEN];
14055
14056 str = get_tv_string_buf(&argvars[0], buf);
14057 do_mzeval(str, rettv);
14058}
14059#endif
14060
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014062 * "nextnonblank()" function
14063 */
14064 static void
14065f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014066 typval_T *argvars;
14067 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014068{
14069 linenr_T lnum;
14070
14071 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074 {
14075 lnum = 0;
14076 break;
14077 }
14078 if (*skipwhite(ml_get(lnum)) != NUL)
14079 break;
14080 }
14081 rettv->vval.v_number = lnum;
14082}
14083
14084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085 * "nr2char()" function
14086 */
14087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014089 typval_T *argvars;
14090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091{
14092 char_u buf[NUMBUFLEN];
14093
14094#ifdef FEAT_MBYTE
14095 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014096 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 else
14098#endif
14099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 buf[1] = NUL;
14102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014103 rettv->v_type = VAR_STRING;
14104 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014105}
14106
14107/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014108 * "pathshorten()" function
14109 */
14110 static void
14111f_pathshorten(argvars, rettv)
14112 typval_T *argvars;
14113 typval_T *rettv;
14114{
14115 char_u *p;
14116
14117 rettv->v_type = VAR_STRING;
14118 p = get_tv_string_chk(&argvars[0]);
14119 if (p == NULL)
14120 rettv->vval.v_string = NULL;
14121 else
14122 {
14123 p = vim_strsave(p);
14124 rettv->vval.v_string = p;
14125 if (p != NULL)
14126 shorten_dir(p);
14127 }
14128}
14129
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014130#ifdef FEAT_FLOAT
14131/*
14132 * "pow()" function
14133 */
14134 static void
14135f_pow(argvars, rettv)
14136 typval_T *argvars;
14137 typval_T *rettv;
14138{
14139 float_T fx, fy;
14140
14141 rettv->v_type = VAR_FLOAT;
14142 if (get_float_arg(argvars, &fx) == OK
14143 && get_float_arg(&argvars[1], &fy) == OK)
14144 rettv->vval.v_float = pow(fx, fy);
14145 else
14146 rettv->vval.v_float = 0.0;
14147}
14148#endif
14149
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014150/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014151 * "prevnonblank()" function
14152 */
14153 static void
14154f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014155 typval_T *argvars;
14156 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014157{
14158 linenr_T lnum;
14159
14160 lnum = get_tv_lnum(argvars);
14161 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14162 lnum = 0;
14163 else
14164 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14165 --lnum;
14166 rettv->vval.v_number = lnum;
14167}
14168
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014169#ifdef HAVE_STDARG_H
14170/* This dummy va_list is here because:
14171 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14172 * - locally in the function results in a "used before set" warning
14173 * - using va_start() to initialize it gives "function with fixed args" error */
14174static va_list ap;
14175#endif
14176
Bram Moolenaar8c711452005-01-14 21:53:12 +000014177/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014178 * "printf()" function
14179 */
14180 static void
14181f_printf(argvars, rettv)
14182 typval_T *argvars;
14183 typval_T *rettv;
14184{
14185 rettv->v_type = VAR_STRING;
14186 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014187#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014188 {
14189 char_u buf[NUMBUFLEN];
14190 int len;
14191 char_u *s;
14192 int saved_did_emsg = did_emsg;
14193 char *fmt;
14194
14195 /* Get the required length, allocate the buffer and do it for real. */
14196 did_emsg = FALSE;
14197 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014198 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014199 if (!did_emsg)
14200 {
14201 s = alloc(len + 1);
14202 if (s != NULL)
14203 {
14204 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014205 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014206 }
14207 }
14208 did_emsg |= saved_did_emsg;
14209 }
14210#endif
14211}
14212
14213/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014214 * "pumvisible()" function
14215 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014216 static void
14217f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014218 typval_T *argvars UNUSED;
14219 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014220{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014221#ifdef FEAT_INS_EXPAND
14222 if (pum_visible())
14223 rettv->vval.v_number = 1;
14224#endif
14225}
14226
14227/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014228 * "range()" function
14229 */
14230 static void
14231f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014232 typval_T *argvars;
14233 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014234{
14235 long start;
14236 long end;
14237 long stride = 1;
14238 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014239 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014242 if (argvars[1].v_type == VAR_UNKNOWN)
14243 {
14244 end = start - 1;
14245 start = 0;
14246 }
14247 else
14248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014250 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014252 }
14253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 if (error)
14255 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014256 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014257 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014258 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014259 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014260 else
14261 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014262 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014263 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014264 if (list_append_number(rettv->vval.v_list,
14265 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014266 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014267 }
14268}
14269
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014270/*
14271 * "readfile()" function
14272 */
14273 static void
14274f_readfile(argvars, rettv)
14275 typval_T *argvars;
14276 typval_T *rettv;
14277{
14278 int binary = FALSE;
14279 char_u *fname;
14280 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014281 listitem_T *li;
14282#define FREAD_SIZE 200 /* optimized for text lines */
14283 char_u buf[FREAD_SIZE];
14284 int readlen; /* size of last fread() */
14285 int buflen; /* nr of valid chars in buf[] */
14286 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14287 int tolist; /* first byte in buf[] still to be put in list */
14288 int chop; /* how many CR to chop off */
14289 char_u *prev = NULL; /* previously read bytes, if any */
14290 int prevlen = 0; /* length of "prev" if not NULL */
14291 char_u *s;
14292 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014293 long maxline = MAXLNUM;
14294 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014295
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014296 if (argvars[1].v_type != VAR_UNKNOWN)
14297 {
14298 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14299 binary = TRUE;
14300 if (argvars[2].v_type != VAR_UNKNOWN)
14301 maxline = get_tv_number(&argvars[2]);
14302 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014304 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014305 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014306
14307 /* Always open the file in binary mode, library functions have a mind of
14308 * their own about CR-LF conversion. */
14309 fname = get_tv_string(&argvars[0]);
14310 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14311 {
14312 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14313 return;
14314 }
14315
14316 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014317 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014318 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014319 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014320 buflen = filtd + readlen;
14321 tolist = 0;
14322 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14323 {
Bram Moolenaarf56a6de2011-07-07 17:36:56 +020014324 if (readlen <= 0 || buf[filtd] == '\n')
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014325 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014326 /* In binary mode add an empty list item when the last
14327 * non-empty line ends in a '\n'. */
14328 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014329 break;
14330
14331 /* Found end-of-line or end-of-file: add a text line to the
14332 * list. */
14333 chop = 0;
14334 if (!binary)
14335 while (filtd - chop - 1 >= tolist
14336 && buf[filtd - chop - 1] == '\r')
14337 ++chop;
14338 len = filtd - tolist - chop;
14339 if (prev == NULL)
14340 s = vim_strnsave(buf + tolist, len);
14341 else
14342 {
14343 s = alloc((unsigned)(prevlen + len + 1));
14344 if (s != NULL)
14345 {
14346 mch_memmove(s, prev, prevlen);
14347 vim_free(prev);
14348 prev = NULL;
14349 mch_memmove(s + prevlen, buf + tolist, len);
14350 s[prevlen + len] = NUL;
14351 }
14352 }
14353 tolist = filtd + 1;
14354
14355 li = listitem_alloc();
14356 if (li == NULL)
14357 {
14358 vim_free(s);
14359 break;
14360 }
14361 li->li_tv.v_type = VAR_STRING;
14362 li->li_tv.v_lock = 0;
14363 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014364 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014365
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014366 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014367 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014368 if (readlen <= 0)
14369 break;
14370 }
14371 else if (buf[filtd] == NUL)
14372 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014373#ifdef FEAT_MBYTE
14374 else if (buf[filtd] == 0xef
14375 && enc_utf8
14376 && filtd + 2 < buflen
14377 && !binary
14378 && buf[filtd + 1] == 0xbb
14379 && buf[filtd + 2] == 0xbf)
14380 {
14381 /* remove utf-8 byte order mark */
14382 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14383 --filtd;
14384 buflen -= 3;
14385 }
14386#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014387 }
14388 if (readlen <= 0)
14389 break;
14390
14391 if (tolist == 0)
14392 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014393 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014394 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014395 /* "buf" is full, need to move text to an allocated buffer */
14396 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014397 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014398 prev = vim_strnsave(buf, buflen);
14399 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014400 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014401 else
14402 {
14403 s = alloc((unsigned)(prevlen + buflen));
14404 if (s != NULL)
14405 {
14406 mch_memmove(s, prev, prevlen);
14407 mch_memmove(s + prevlen, buf, buflen);
14408 vim_free(prev);
14409 prev = s;
14410 prevlen += buflen;
14411 }
14412 }
14413 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014414 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014415 }
14416 else
14417 {
14418 mch_memmove(buf, buf + tolist, buflen - tolist);
14419 filtd -= tolist;
14420 }
14421 }
14422
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014423 /*
14424 * For a negative line count use only the lines at the end of the file,
14425 * free the rest.
14426 */
14427 if (maxline < 0)
14428 while (cnt > -maxline)
14429 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014430 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014431 --cnt;
14432 }
14433
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014434 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014435 fclose(fd);
14436}
14437
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014438#if defined(FEAT_RELTIME)
14439static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14440
14441/*
14442 * Convert a List to proftime_T.
14443 * Return FAIL when there is something wrong.
14444 */
14445 static int
14446list2proftime(arg, tm)
14447 typval_T *arg;
14448 proftime_T *tm;
14449{
14450 long n1, n2;
14451 int error = FALSE;
14452
14453 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14454 || arg->vval.v_list->lv_len != 2)
14455 return FAIL;
14456 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14457 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14458# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014459 tm->HighPart = n1;
14460 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014461# else
14462 tm->tv_sec = n1;
14463 tm->tv_usec = n2;
14464# endif
14465 return error ? FAIL : OK;
14466}
14467#endif /* FEAT_RELTIME */
14468
14469/*
14470 * "reltime()" function
14471 */
14472 static void
14473f_reltime(argvars, rettv)
14474 typval_T *argvars;
14475 typval_T *rettv;
14476{
14477#ifdef FEAT_RELTIME
14478 proftime_T res;
14479 proftime_T start;
14480
14481 if (argvars[0].v_type == VAR_UNKNOWN)
14482 {
14483 /* No arguments: get current time. */
14484 profile_start(&res);
14485 }
14486 else if (argvars[1].v_type == VAR_UNKNOWN)
14487 {
14488 if (list2proftime(&argvars[0], &res) == FAIL)
14489 return;
14490 profile_end(&res);
14491 }
14492 else
14493 {
14494 /* Two arguments: compute the difference. */
14495 if (list2proftime(&argvars[0], &start) == FAIL
14496 || list2proftime(&argvars[1], &res) == FAIL)
14497 return;
14498 profile_sub(&res, &start);
14499 }
14500
14501 if (rettv_list_alloc(rettv) == OK)
14502 {
14503 long n1, n2;
14504
14505# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014506 n1 = res.HighPart;
14507 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014508# else
14509 n1 = res.tv_sec;
14510 n2 = res.tv_usec;
14511# endif
14512 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14513 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14514 }
14515#endif
14516}
14517
14518/*
14519 * "reltimestr()" function
14520 */
14521 static void
14522f_reltimestr(argvars, rettv)
14523 typval_T *argvars;
14524 typval_T *rettv;
14525{
14526#ifdef FEAT_RELTIME
14527 proftime_T tm;
14528#endif
14529
14530 rettv->v_type = VAR_STRING;
14531 rettv->vval.v_string = NULL;
14532#ifdef FEAT_RELTIME
14533 if (list2proftime(&argvars[0], &tm) == OK)
14534 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14535#endif
14536}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014537
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14539static void make_connection __ARGS((void));
14540static int check_connection __ARGS((void));
14541
14542 static void
14543make_connection()
14544{
14545 if (X_DISPLAY == NULL
14546# ifdef FEAT_GUI
14547 && !gui.in_use
14548# endif
14549 )
14550 {
14551 x_force_connect = TRUE;
14552 setup_term_clip();
14553 x_force_connect = FALSE;
14554 }
14555}
14556
14557 static int
14558check_connection()
14559{
14560 make_connection();
14561 if (X_DISPLAY == NULL)
14562 {
14563 EMSG(_("E240: No connection to Vim server"));
14564 return FAIL;
14565 }
14566 return OK;
14567}
14568#endif
14569
14570#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014571static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014572
14573 static void
14574remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014575 typval_T *argvars;
14576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014577 int expr;
14578{
14579 char_u *server_name;
14580 char_u *keys;
14581 char_u *r = NULL;
14582 char_u buf[NUMBUFLEN];
14583# ifdef WIN32
14584 HWND w;
14585# else
14586 Window w;
14587# endif
14588
14589 if (check_restricted() || check_secure())
14590 return;
14591
14592# ifdef FEAT_X11
14593 if (check_connection() == FAIL)
14594 return;
14595# endif
14596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 server_name = get_tv_string_chk(&argvars[0]);
14598 if (server_name == NULL)
14599 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600 keys = get_tv_string_buf(&argvars[1], buf);
14601# ifdef WIN32
14602 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14603# else
14604 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14605 < 0)
14606# endif
14607 {
14608 if (r != NULL)
14609 EMSG(r); /* sending worked but evaluation failed */
14610 else
14611 EMSG2(_("E241: Unable to send to %s"), server_name);
14612 return;
14613 }
14614
14615 rettv->vval.v_string = r;
14616
14617 if (argvars[2].v_type != VAR_UNKNOWN)
14618 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014620 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014623 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014624 v.di_tv.v_type = VAR_STRING;
14625 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014626 idvar = get_tv_string_chk(&argvars[2]);
14627 if (idvar != NULL)
14628 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014629 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014630 }
14631}
14632#endif
14633
14634/*
14635 * "remote_expr()" function
14636 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637 static void
14638f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641{
14642 rettv->v_type = VAR_STRING;
14643 rettv->vval.v_string = NULL;
14644#ifdef FEAT_CLIENTSERVER
14645 remote_common(argvars, rettv, TRUE);
14646#endif
14647}
14648
14649/*
14650 * "remote_foreground()" function
14651 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652 static void
14653f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014654 typval_T *argvars UNUSED;
14655 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014657#ifdef FEAT_CLIENTSERVER
14658# ifdef WIN32
14659 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 {
14661 char_u *server_name = get_tv_string_chk(&argvars[0]);
14662
14663 if (server_name != NULL)
14664 serverForeground(server_name);
14665 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014666# else
14667 /* Send a foreground() expression to the server. */
14668 argvars[1].v_type = VAR_STRING;
14669 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14670 argvars[2].v_type = VAR_UNKNOWN;
14671 remote_common(argvars, rettv, TRUE);
14672 vim_free(argvars[1].vval.v_string);
14673# endif
14674#endif
14675}
14676
Bram Moolenaar0d660222005-01-07 21:51:51 +000014677 static void
14678f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014679 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681{
14682#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014684 char_u *s = NULL;
14685# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014686 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689
14690 if (check_restricted() || check_secure())
14691 {
14692 rettv->vval.v_number = -1;
14693 return;
14694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014695 serverid = get_tv_string_chk(&argvars[0]);
14696 if (serverid == NULL)
14697 {
14698 rettv->vval.v_number = -1;
14699 return; /* type error; errmsg already given */
14700 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014702 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014703 if (n == 0)
14704 rettv->vval.v_number = -1;
14705 else
14706 {
14707 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14708 rettv->vval.v_number = (s != NULL);
14709 }
14710# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711 if (check_connection() == FAIL)
14712 return;
14713
14714 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014715 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716# endif
14717
14718 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014720 char_u *retvar;
14721
Bram Moolenaar33570922005-01-25 22:26:29 +000014722 v.di_tv.v_type = VAR_STRING;
14723 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724 retvar = get_tv_string_chk(&argvars[1]);
14725 if (retvar != NULL)
14726 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014728 }
14729#else
14730 rettv->vval.v_number = -1;
14731#endif
14732}
14733
Bram Moolenaar0d660222005-01-07 21:51:51 +000014734 static void
14735f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738{
14739 char_u *r = NULL;
14740
14741#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 char_u *serverid = get_tv_string_chk(&argvars[0]);
14743
14744 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014745 {
14746# ifdef WIN32
14747 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014748 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014750 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751 if (n != 0)
14752 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14753 if (r == NULL)
14754# else
14755 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014756 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014757# endif
14758 EMSG(_("E277: Unable to read a server reply"));
14759 }
14760#endif
14761 rettv->v_type = VAR_STRING;
14762 rettv->vval.v_string = r;
14763}
14764
14765/*
14766 * "remote_send()" function
14767 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014768 static void
14769f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014771 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014772{
14773 rettv->v_type = VAR_STRING;
14774 rettv->vval.v_string = NULL;
14775#ifdef FEAT_CLIENTSERVER
14776 remote_common(argvars, rettv, FALSE);
14777#endif
14778}
14779
14780/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014781 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014782 */
14783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 typval_T *argvars;
14786 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014787{
Bram Moolenaar33570922005-01-25 22:26:29 +000014788 list_T *l;
14789 listitem_T *item, *item2;
14790 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014791 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014792 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014793 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014794 dict_T *d;
14795 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014796 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014797
Bram Moolenaar8c711452005-01-14 21:53:12 +000014798 if (argvars[0].v_type == VAR_DICT)
14799 {
14800 if (argvars[2].v_type != VAR_UNKNOWN)
14801 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014802 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014803 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014805 key = get_tv_string_chk(&argvars[1]);
14806 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 di = dict_find(d, key, -1);
14809 if (di == NULL)
14810 EMSG2(_(e_dictkey), key);
14811 else
14812 {
14813 *rettv = di->di_tv;
14814 init_tv(&di->di_tv);
14815 dictitem_remove(d, di);
14816 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014818 }
14819 }
14820 else if (argvars[0].v_type != VAR_LIST)
14821 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014822 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014823 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825 int error = FALSE;
14826
14827 idx = get_tv_number_chk(&argvars[1], &error);
14828 if (error)
14829 ; /* type error: do nothing, errmsg already given */
14830 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014831 EMSGN(_(e_listidx), idx);
14832 else
14833 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014834 if (argvars[2].v_type == VAR_UNKNOWN)
14835 {
14836 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014837 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014838 *rettv = item->li_tv;
14839 vim_free(item);
14840 }
14841 else
14842 {
14843 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014844 end = get_tv_number_chk(&argvars[2], &error);
14845 if (error)
14846 ; /* type error: do nothing */
14847 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014848 EMSGN(_(e_listidx), end);
14849 else
14850 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014851 int cnt = 0;
14852
14853 for (li = item; li != NULL; li = li->li_next)
14854 {
14855 ++cnt;
14856 if (li == item2)
14857 break;
14858 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014859 if (li == NULL) /* didn't find "item2" after "item" */
14860 EMSG(_(e_invrange));
14861 else
14862 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014863 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014864 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014865 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014866 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014867 l->lv_first = item;
14868 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014869 item->li_prev = NULL;
14870 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014871 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014872 }
14873 }
14874 }
14875 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014876 }
14877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878}
14879
14880/*
14881 * "rename({from}, {to})" function
14882 */
14883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014884f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014885 typval_T *argvars;
14886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887{
14888 char_u buf[NUMBUFLEN];
14889
14890 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014893 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14894 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895}
14896
14897/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014898 * "repeat()" function
14899 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014900 static void
14901f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014902 typval_T *argvars;
14903 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014904{
14905 char_u *p;
14906 int n;
14907 int slen;
14908 int len;
14909 char_u *r;
14910 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014911
14912 n = get_tv_number(&argvars[1]);
14913 if (argvars[0].v_type == VAR_LIST)
14914 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014915 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014916 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014917 if (list_extend(rettv->vval.v_list,
14918 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014919 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014920 }
14921 else
14922 {
14923 p = get_tv_string(&argvars[0]);
14924 rettv->v_type = VAR_STRING;
14925 rettv->vval.v_string = NULL;
14926
14927 slen = (int)STRLEN(p);
14928 len = slen * n;
14929 if (len <= 0)
14930 return;
14931
14932 r = alloc(len + 1);
14933 if (r != NULL)
14934 {
14935 for (i = 0; i < n; i++)
14936 mch_memmove(r + i * slen, p, (size_t)slen);
14937 r[len] = NUL;
14938 }
14939
14940 rettv->vval.v_string = r;
14941 }
14942}
14943
14944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 * "resolve()" function
14946 */
14947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014948f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014949 typval_T *argvars;
14950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951{
14952 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020014953#ifdef HAVE_READLINK
14954 char_u *buf = NULL;
14955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014957 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958#ifdef FEAT_SHORTCUT
14959 {
14960 char_u *v = NULL;
14961
14962 v = mch_resolve_shortcut(p);
14963 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014964 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014966 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 }
14968#else
14969# ifdef HAVE_READLINK
14970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 char_u *cpy;
14972 int len;
14973 char_u *remain = NULL;
14974 char_u *q;
14975 int is_relative_to_current = FALSE;
14976 int has_trailing_pathsep = FALSE;
14977 int limit = 100;
14978
14979 p = vim_strsave(p);
14980
14981 if (p[0] == '.' && (vim_ispathsep(p[1])
14982 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14983 is_relative_to_current = TRUE;
14984
14985 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014986 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014989 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
14990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991
14992 q = getnextcomp(p);
14993 if (*q != NUL)
14994 {
14995 /* Separate the first path component in "p", and keep the
14996 * remainder (beginning with the path separator). */
14997 remain = vim_strsave(q - 1);
14998 q[-1] = NUL;
14999 }
15000
Bram Moolenaard9462e32011-04-11 21:35:11 +020015001 buf = alloc(MAXPATHL + 1);
15002 if (buf == NULL)
15003 goto fail;
15004
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 for (;;)
15006 {
15007 for (;;)
15008 {
15009 len = readlink((char *)p, (char *)buf, MAXPATHL);
15010 if (len <= 0)
15011 break;
15012 buf[len] = NUL;
15013
15014 if (limit-- == 0)
15015 {
15016 vim_free(p);
15017 vim_free(remain);
15018 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 goto fail;
15021 }
15022
15023 /* Ensure that the result will have a trailing path separator
15024 * if the argument has one. */
15025 if (remain == NULL && has_trailing_pathsep)
15026 add_pathsep(buf);
15027
15028 /* Separate the first path component in the link value and
15029 * concatenate the remainders. */
15030 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15031 if (*q != NUL)
15032 {
15033 if (remain == NULL)
15034 remain = vim_strsave(q - 1);
15035 else
15036 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015037 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 if (cpy != NULL)
15039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040 vim_free(remain);
15041 remain = cpy;
15042 }
15043 }
15044 q[-1] = NUL;
15045 }
15046
15047 q = gettail(p);
15048 if (q > p && *q == NUL)
15049 {
15050 /* Ignore trailing path separator. */
15051 q[-1] = NUL;
15052 q = gettail(p);
15053 }
15054 if (q > p && !mch_isFullName(buf))
15055 {
15056 /* symlink is relative to directory of argument */
15057 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15058 if (cpy != NULL)
15059 {
15060 STRCPY(cpy, p);
15061 STRCPY(gettail(cpy), buf);
15062 vim_free(p);
15063 p = cpy;
15064 }
15065 }
15066 else
15067 {
15068 vim_free(p);
15069 p = vim_strsave(buf);
15070 }
15071 }
15072
15073 if (remain == NULL)
15074 break;
15075
15076 /* Append the first path component of "remain" to "p". */
15077 q = getnextcomp(remain + 1);
15078 len = q - remain - (*q != NUL);
15079 cpy = vim_strnsave(p, STRLEN(p) + len);
15080 if (cpy != NULL)
15081 {
15082 STRNCAT(cpy, remain, len);
15083 vim_free(p);
15084 p = cpy;
15085 }
15086 /* Shorten "remain". */
15087 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015088 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089 else
15090 {
15091 vim_free(remain);
15092 remain = NULL;
15093 }
15094 }
15095
15096 /* If the result is a relative path name, make it explicitly relative to
15097 * the current directory if and only if the argument had this form. */
15098 if (!vim_ispathsep(*p))
15099 {
15100 if (is_relative_to_current
15101 && *p != NUL
15102 && !(p[0] == '.'
15103 && (p[1] == NUL
15104 || vim_ispathsep(p[1])
15105 || (p[1] == '.'
15106 && (p[2] == NUL
15107 || vim_ispathsep(p[2]))))))
15108 {
15109 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015110 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 if (cpy != NULL)
15112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 vim_free(p);
15114 p = cpy;
15115 }
15116 }
15117 else if (!is_relative_to_current)
15118 {
15119 /* Strip leading "./". */
15120 q = p;
15121 while (q[0] == '.' && vim_ispathsep(q[1]))
15122 q += 2;
15123 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015124 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 }
15126 }
15127
15128 /* Ensure that the result will have no trailing path separator
15129 * if the argument had none. But keep "/" or "//". */
15130 if (!has_trailing_pathsep)
15131 {
15132 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015133 if (after_pathsep(p, q))
15134 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135 }
15136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 }
15139# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141# endif
15142#endif
15143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015144 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145
15146#ifdef HAVE_READLINK
15147fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015148 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015150 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151}
15152
15153/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 */
15156 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015158 typval_T *argvars;
15159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160{
Bram Moolenaar33570922005-01-25 22:26:29 +000015161 list_T *l;
15162 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163
Bram Moolenaar0d660222005-01-07 21:51:51 +000015164 if (argvars[0].v_type != VAR_LIST)
15165 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015166 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015167 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168 {
15169 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015170 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015171 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172 while (li != NULL)
15173 {
15174 ni = li->li_prev;
15175 list_append(l, li);
15176 li = ni;
15177 }
15178 rettv->vval.v_list = l;
15179 rettv->v_type = VAR_LIST;
15180 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015181 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183}
15184
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015185#define SP_NOMOVE 0x01 /* don't move cursor */
15186#define SP_REPEAT 0x02 /* repeat to find outer pair */
15187#define SP_RETCOUNT 0x04 /* return matchcount */
15188#define SP_SETPCMARK 0x08 /* set previous context mark */
15189#define SP_START 0x10 /* accept match at start position */
15190#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15191#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015192
Bram Moolenaar33570922005-01-25 22:26:29 +000015193static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194
15195/*
15196 * Get flags for a search function.
15197 * Possibly sets "p_ws".
15198 * Returns BACKWARD, FORWARD or zero (for an error).
15199 */
15200 static int
15201get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015202 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015203 int *flagsp;
15204{
15205 int dir = FORWARD;
15206 char_u *flags;
15207 char_u nbuf[NUMBUFLEN];
15208 int mask;
15209
15210 if (varp->v_type != VAR_UNKNOWN)
15211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 flags = get_tv_string_buf_chk(varp, nbuf);
15213 if (flags == NULL)
15214 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015215 while (*flags != NUL)
15216 {
15217 switch (*flags)
15218 {
15219 case 'b': dir = BACKWARD; break;
15220 case 'w': p_ws = TRUE; break;
15221 case 'W': p_ws = FALSE; break;
15222 default: mask = 0;
15223 if (flagsp != NULL)
15224 switch (*flags)
15225 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015226 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015227 case 'e': mask = SP_END; break;
15228 case 'm': mask = SP_RETCOUNT; break;
15229 case 'n': mask = SP_NOMOVE; break;
15230 case 'p': mask = SP_SUBPAT; break;
15231 case 'r': mask = SP_REPEAT; break;
15232 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015233 }
15234 if (mask == 0)
15235 {
15236 EMSG2(_(e_invarg2), flags);
15237 dir = 0;
15238 }
15239 else
15240 *flagsp |= mask;
15241 }
15242 if (dir == 0)
15243 break;
15244 ++flags;
15245 }
15246 }
15247 return dir;
15248}
15249
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015251 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015253 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015254search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015255 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015256 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015257 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015259 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 char_u *pat;
15261 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015262 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 int save_p_ws = p_ws;
15264 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015265 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015266 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015267 proftime_T tm;
15268#ifdef FEAT_RELTIME
15269 long time_limit = 0;
15270#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015271 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015272 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015274 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015275 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015276 if (dir == 0)
15277 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015278 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015279 if (flags & SP_START)
15280 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015281 if (flags & SP_END)
15282 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015283
Bram Moolenaar76929292008-01-06 19:07:36 +000015284 /* Optional arguments: line number to stop searching and timeout. */
15285 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015286 {
15287 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15288 if (lnum_stop < 0)
15289 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015290#ifdef FEAT_RELTIME
15291 if (argvars[3].v_type != VAR_UNKNOWN)
15292 {
15293 time_limit = get_tv_number_chk(&argvars[3], NULL);
15294 if (time_limit < 0)
15295 goto theend;
15296 }
15297#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015298 }
15299
Bram Moolenaar76929292008-01-06 19:07:36 +000015300#ifdef FEAT_RELTIME
15301 /* Set the time limit, if there is one. */
15302 profile_setlimit(time_limit, &tm);
15303#endif
15304
Bram Moolenaar231334e2005-07-25 20:46:57 +000015305 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015306 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015307 * Check to make sure only those flags are set.
15308 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15309 * flags cannot be set. Check for that condition also.
15310 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015311 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015312 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015313 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015314 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015315 goto theend;
15316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015318 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015319 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015320 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015321 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015323 if (flags & SP_SUBPAT)
15324 retval = subpatnum;
15325 else
15326 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015327 if (flags & SP_SETPCMARK)
15328 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015330 if (match_pos != NULL)
15331 {
15332 /* Store the match cursor position */
15333 match_pos->lnum = pos.lnum;
15334 match_pos->col = pos.col + 1;
15335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336 /* "/$" will put the cursor after the end of the line, may need to
15337 * correct that here */
15338 check_cursor();
15339 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015340
15341 /* If 'n' flag is used: restore cursor position. */
15342 if (flags & SP_NOMOVE)
15343 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015344 else
15345 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015346theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015348
15349 return retval;
15350}
15351
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015352#ifdef FEAT_FLOAT
15353/*
15354 * "round({float})" function
15355 */
15356 static void
15357f_round(argvars, rettv)
15358 typval_T *argvars;
15359 typval_T *rettv;
15360{
15361 float_T f;
15362
15363 rettv->v_type = VAR_FLOAT;
15364 if (get_float_arg(argvars, &f) == OK)
15365 /* round() is not in C90, use ceil() or floor() instead. */
15366 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15367 else
15368 rettv->vval.v_float = 0.0;
15369}
15370#endif
15371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015372/*
15373 * "search()" function
15374 */
15375 static void
15376f_search(argvars, rettv)
15377 typval_T *argvars;
15378 typval_T *rettv;
15379{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015380 int flags = 0;
15381
15382 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383}
15384
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015386 * "searchdecl()" function
15387 */
15388 static void
15389f_searchdecl(argvars, rettv)
15390 typval_T *argvars;
15391 typval_T *rettv;
15392{
15393 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015394 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015395 int error = FALSE;
15396 char_u *name;
15397
15398 rettv->vval.v_number = 1; /* default: FAIL */
15399
15400 name = get_tv_string_chk(&argvars[0]);
15401 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015402 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015403 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015404 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15405 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15406 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015407 if (!error && name != NULL)
15408 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015409 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015410}
15411
15412/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015413 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015415 static int
15416searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015417 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015418 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419{
15420 char_u *spat, *mpat, *epat;
15421 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 int dir;
15424 int flags = 0;
15425 char_u nbuf1[NUMBUFLEN];
15426 char_u nbuf2[NUMBUFLEN];
15427 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015428 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015429 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015430 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015433 spat = get_tv_string_chk(&argvars[0]);
15434 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15435 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15436 if (spat == NULL || mpat == NULL || epat == NULL)
15437 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 /* Handle the optional fourth argument: flags */
15440 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015441 if (dir == 0)
15442 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015443
15444 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015445 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15446 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015447 if ((flags & (SP_END | SP_SUBPAT)) != 0
15448 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015449 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015450 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015451 goto theend;
15452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015454 /* Using 'r' implies 'W', otherwise it doesn't work. */
15455 if (flags & SP_REPEAT)
15456 p_ws = FALSE;
15457
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015458 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015459 if (argvars[3].v_type == VAR_UNKNOWN
15460 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 skip = (char_u *)"";
15462 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015464 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015465 if (argvars[5].v_type != VAR_UNKNOWN)
15466 {
15467 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15468 if (lnum_stop < 0)
15469 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015470#ifdef FEAT_RELTIME
15471 if (argvars[6].v_type != VAR_UNKNOWN)
15472 {
15473 time_limit = get_tv_number_chk(&argvars[6], NULL);
15474 if (time_limit < 0)
15475 goto theend;
15476 }
15477#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015478 }
15479 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015480 if (skip == NULL)
15481 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015483 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015484 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015485
15486theend:
15487 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015488
15489 return retval;
15490}
15491
15492/*
15493 * "searchpair()" function
15494 */
15495 static void
15496f_searchpair(argvars, rettv)
15497 typval_T *argvars;
15498 typval_T *rettv;
15499{
15500 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15501}
15502
15503/*
15504 * "searchpairpos()" function
15505 */
15506 static void
15507f_searchpairpos(argvars, rettv)
15508 typval_T *argvars;
15509 typval_T *rettv;
15510{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015511 pos_T match_pos;
15512 int lnum = 0;
15513 int col = 0;
15514
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015515 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015516 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015517
15518 if (searchpair_cmn(argvars, &match_pos) > 0)
15519 {
15520 lnum = match_pos.lnum;
15521 col = match_pos.col;
15522 }
15523
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015524 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15525 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015526}
15527
15528/*
15529 * Search for a start/middle/end thing.
15530 * Used by searchpair(), see its documentation for the details.
15531 * Returns 0 or -1 for no match,
15532 */
15533 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015534do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15535 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015536 char_u *spat; /* start pattern */
15537 char_u *mpat; /* middle pattern */
15538 char_u *epat; /* end pattern */
15539 int dir; /* BACKWARD or FORWARD */
15540 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015541 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015542 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015543 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015544 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015545{
15546 char_u *save_cpo;
15547 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15548 long retval = 0;
15549 pos_T pos;
15550 pos_T firstpos;
15551 pos_T foundpos;
15552 pos_T save_cursor;
15553 pos_T save_pos;
15554 int n;
15555 int r;
15556 int nest = 1;
15557 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015558 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015559 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015560
15561 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15562 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015563 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015564
Bram Moolenaar76929292008-01-06 19:07:36 +000015565#ifdef FEAT_RELTIME
15566 /* Set the time limit, if there is one. */
15567 profile_setlimit(time_limit, &tm);
15568#endif
15569
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015570 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15571 * start/middle/end (pat3, for the top pair). */
15572 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15573 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15574 if (pat2 == NULL || pat3 == NULL)
15575 goto theend;
15576 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15577 if (*mpat == NUL)
15578 STRCPY(pat3, pat2);
15579 else
15580 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15581 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015582 if (flags & SP_START)
15583 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015584
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 save_cursor = curwin->w_cursor;
15586 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015587 clearpos(&firstpos);
15588 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 pat = pat3;
15590 for (;;)
15591 {
15592 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015593 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15595 /* didn't find it or found the first match again: FAIL */
15596 break;
15597
15598 if (firstpos.lnum == 0)
15599 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015600 if (equalpos(pos, foundpos))
15601 {
15602 /* Found the same position again. Can happen with a pattern that
15603 * has "\zs" at the end and searching backwards. Advance one
15604 * character and try again. */
15605 if (dir == BACKWARD)
15606 decl(&pos);
15607 else
15608 incl(&pos);
15609 }
15610 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015612 /* clear the start flag to avoid getting stuck here */
15613 options &= ~SEARCH_START;
15614
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 /* If the skip pattern matches, ignore this match. */
15616 if (*skip != NUL)
15617 {
15618 save_pos = curwin->w_cursor;
15619 curwin->w_cursor = pos;
15620 r = eval_to_bool(skip, &err, NULL, FALSE);
15621 curwin->w_cursor = save_pos;
15622 if (err)
15623 {
15624 /* Evaluating {skip} caused an error, break here. */
15625 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015626 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 break;
15628 }
15629 if (r)
15630 continue;
15631 }
15632
15633 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15634 {
15635 /* Found end when searching backwards or start when searching
15636 * forward: nested pair. */
15637 ++nest;
15638 pat = pat2; /* nested, don't search for middle */
15639 }
15640 else
15641 {
15642 /* Found end when searching forward or start when searching
15643 * backward: end of (nested) pair; or found middle in outer pair. */
15644 if (--nest == 1)
15645 pat = pat3; /* outer level, search for middle */
15646 }
15647
15648 if (nest == 0)
15649 {
15650 /* Found the match: return matchcount or line number. */
15651 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015652 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015654 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015655 if (flags & SP_SETPCMARK)
15656 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 curwin->w_cursor = pos;
15658 if (!(flags & SP_REPEAT))
15659 break;
15660 nest = 1; /* search for next unmatched */
15661 }
15662 }
15663
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015664 if (match_pos != NULL)
15665 {
15666 /* Store the match cursor position */
15667 match_pos->lnum = curwin->w_cursor.lnum;
15668 match_pos->col = curwin->w_cursor.col + 1;
15669 }
15670
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015672 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 curwin->w_cursor = save_cursor;
15674
15675theend:
15676 vim_free(pat2);
15677 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015678 if (p_cpo == empty_option)
15679 p_cpo = save_cpo;
15680 else
15681 /* Darn, evaluating the {skip} expression changed the value. */
15682 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015683
15684 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685}
15686
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015687/*
15688 * "searchpos()" function
15689 */
15690 static void
15691f_searchpos(argvars, rettv)
15692 typval_T *argvars;
15693 typval_T *rettv;
15694{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015695 pos_T match_pos;
15696 int lnum = 0;
15697 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015698 int n;
15699 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015701 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015702 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015703
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015704 n = search_cmn(argvars, &match_pos, &flags);
15705 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015706 {
15707 lnum = match_pos.lnum;
15708 col = match_pos.col;
15709 }
15710
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015711 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15712 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015713 if (flags & SP_SUBPAT)
15714 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015715}
15716
15717
Bram Moolenaar0d660222005-01-07 21:51:51 +000015718 static void
15719f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015720 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015723#ifdef FEAT_CLIENTSERVER
15724 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015725 char_u *server = get_tv_string_chk(&argvars[0]);
15726 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727
Bram Moolenaar0d660222005-01-07 21:51:51 +000015728 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015729 if (server == NULL || reply == NULL)
15730 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731 if (check_restricted() || check_secure())
15732 return;
15733# ifdef FEAT_X11
15734 if (check_connection() == FAIL)
15735 return;
15736# endif
15737
15738 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015740 EMSG(_("E258: Unable to send to client"));
15741 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 rettv->vval.v_number = 0;
15744#else
15745 rettv->vval.v_number = -1;
15746#endif
15747}
15748
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749 static void
15750f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015751 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015752 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753{
15754 char_u *r = NULL;
15755
15756#ifdef FEAT_CLIENTSERVER
15757# ifdef WIN32
15758 r = serverGetVimNames();
15759# else
15760 make_connection();
15761 if (X_DISPLAY != NULL)
15762 r = serverGetVimNames(X_DISPLAY);
15763# endif
15764#endif
15765 rettv->v_type = VAR_STRING;
15766 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767}
15768
15769/*
15770 * "setbufvar()" function
15771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015775 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776{
15777 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015780 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 char_u nbuf[NUMBUFLEN];
15782
15783 if (check_restricted() || check_secure())
15784 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015785 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15786 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 varp = &argvars[2];
15789
15790 if (buf != NULL && varname != NULL && varp != NULL)
15791 {
15792 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794
15795 if (*varname == '&')
15796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015797 long numval;
15798 char_u *strval;
15799 int error = FALSE;
15800
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015802 numval = get_tv_number_chk(varp, &error);
15803 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015804 if (!error && strval != NULL)
15805 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 }
15807 else
15808 {
15809 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15810 if (bufvarname != NULL)
15811 {
15812 STRCPY(bufvarname, "b:");
15813 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015814 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 vim_free(bufvarname);
15816 }
15817 }
15818
15819 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822}
15823
15824/*
15825 * "setcmdpos()" function
15826 */
15827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015828f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015829 typval_T *argvars;
15830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015832 int pos = (int)get_tv_number(&argvars[0]) - 1;
15833
15834 if (pos >= 0)
15835 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836}
15837
15838/*
15839 * "setline()" function
15840 */
15841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015842f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015843 typval_T *argvars;
15844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845{
15846 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015847 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015848 list_T *l = NULL;
15849 listitem_T *li = NULL;
15850 long added = 0;
15851 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015853 lnum = get_tv_lnum(&argvars[0]);
15854 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015856 l = argvars[1].vval.v_list;
15857 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015859 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015860 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015861
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015862 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015863 for (;;)
15864 {
15865 if (l != NULL)
15866 {
15867 /* list argument, get next string */
15868 if (li == NULL)
15869 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015870 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015871 li = li->li_next;
15872 }
15873
15874 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015875 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015876 break;
15877 if (lnum <= curbuf->b_ml.ml_line_count)
15878 {
15879 /* existing line, replace it */
15880 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15881 {
15882 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015883 if (lnum == curwin->w_cursor.lnum)
15884 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015885 rettv->vval.v_number = 0; /* OK */
15886 }
15887 }
15888 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15889 {
15890 /* lnum is one past the last line, append the line */
15891 ++added;
15892 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15893 rettv->vval.v_number = 0; /* OK */
15894 }
15895
15896 if (l == NULL) /* only one string argument */
15897 break;
15898 ++lnum;
15899 }
15900
15901 if (added > 0)
15902 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903}
15904
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015905static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15906
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015908 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015909 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015910 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015911set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015912 win_T *wp UNUSED;
15913 typval_T *list_arg UNUSED;
15914 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015915 typval_T *rettv;
15916{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015917#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015918 char_u *act;
15919 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015920#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015921
Bram Moolenaar2641f772005-03-25 21:58:17 +000015922 rettv->vval.v_number = -1;
15923
15924#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015925 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015926 EMSG(_(e_listreq));
15927 else
15928 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015929 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015930
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015931 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015932 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015933 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015934 if (act == NULL)
15935 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015936 if (*act == 'a' || *act == 'r')
15937 action = *act;
15938 }
15939
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015940 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015941 rettv->vval.v_number = 0;
15942 }
15943#endif
15944}
15945
15946/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015947 * "setloclist()" function
15948 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015949 static void
15950f_setloclist(argvars, rettv)
15951 typval_T *argvars;
15952 typval_T *rettv;
15953{
15954 win_T *win;
15955
15956 rettv->vval.v_number = -1;
15957
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015958 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015959 if (win != NULL)
15960 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15961}
15962
15963/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015964 * "setmatches()" function
15965 */
15966 static void
15967f_setmatches(argvars, rettv)
15968 typval_T *argvars;
15969 typval_T *rettv;
15970{
15971#ifdef FEAT_SEARCH_EXTRA
15972 list_T *l;
15973 listitem_T *li;
15974 dict_T *d;
15975
15976 rettv->vval.v_number = -1;
15977 if (argvars[0].v_type != VAR_LIST)
15978 {
15979 EMSG(_(e_listreq));
15980 return;
15981 }
15982 if ((l = argvars[0].vval.v_list) != NULL)
15983 {
15984
15985 /* To some extent make sure that we are dealing with a list from
15986 * "getmatches()". */
15987 li = l->lv_first;
15988 while (li != NULL)
15989 {
15990 if (li->li_tv.v_type != VAR_DICT
15991 || (d = li->li_tv.vval.v_dict) == NULL)
15992 {
15993 EMSG(_(e_invarg));
15994 return;
15995 }
15996 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15997 && dict_find(d, (char_u *)"pattern", -1) != NULL
15998 && dict_find(d, (char_u *)"priority", -1) != NULL
15999 && dict_find(d, (char_u *)"id", -1) != NULL))
16000 {
16001 EMSG(_(e_invarg));
16002 return;
16003 }
16004 li = li->li_next;
16005 }
16006
16007 clear_matches(curwin);
16008 li = l->lv_first;
16009 while (li != NULL)
16010 {
16011 d = li->li_tv.vval.v_dict;
16012 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16013 get_dict_string(d, (char_u *)"pattern", FALSE),
16014 (int)get_dict_number(d, (char_u *)"priority"),
16015 (int)get_dict_number(d, (char_u *)"id"));
16016 li = li->li_next;
16017 }
16018 rettv->vval.v_number = 0;
16019 }
16020#endif
16021}
16022
16023/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016024 * "setpos()" function
16025 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016026 static void
16027f_setpos(argvars, rettv)
16028 typval_T *argvars;
16029 typval_T *rettv;
16030{
16031 pos_T pos;
16032 int fnum;
16033 char_u *name;
16034
Bram Moolenaar08250432008-02-13 11:42:46 +000016035 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016036 name = get_tv_string_chk(argvars);
16037 if (name != NULL)
16038 {
16039 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16040 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016041 if (--pos.col < 0)
16042 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016043 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016044 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016045 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016046 if (fnum == curbuf->b_fnum)
16047 {
16048 curwin->w_cursor = pos;
16049 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016050 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016051 }
16052 else
16053 EMSG(_(e_invarg));
16054 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016055 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16056 {
16057 /* set mark */
16058 if (setmark_pos(name[1], &pos, fnum) == OK)
16059 rettv->vval.v_number = 0;
16060 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016061 else
16062 EMSG(_(e_invarg));
16063 }
16064 }
16065}
16066
16067/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016068 * "setqflist()" function
16069 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016070 static void
16071f_setqflist(argvars, rettv)
16072 typval_T *argvars;
16073 typval_T *rettv;
16074{
16075 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16076}
16077
16078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 * "setreg()" function
16080 */
16081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016082f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016083 typval_T *argvars;
16084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085{
16086 int regname;
16087 char_u *strregname;
16088 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016089 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 int append;
16091 char_u yank_type;
16092 long block_len;
16093
16094 block_len = -1;
16095 yank_type = MAUTO;
16096 append = FALSE;
16097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016099 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016101 if (strregname == NULL)
16102 return; /* type error; errmsg already given */
16103 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 if (regname == 0 || regname == '@')
16105 regname = '"';
16106 else if (regname == '=')
16107 return;
16108
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 stropt = get_tv_string_chk(&argvars[2]);
16112 if (stropt == NULL)
16113 return; /* type error */
16114 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 switch (*stropt)
16116 {
16117 case 'a': case 'A': /* append */
16118 append = TRUE;
16119 break;
16120 case 'v': case 'c': /* character-wise selection */
16121 yank_type = MCHAR;
16122 break;
16123 case 'V': case 'l': /* line-wise selection */
16124 yank_type = MLINE;
16125 break;
16126#ifdef FEAT_VISUAL
16127 case 'b': case Ctrl_V: /* block-wise selection */
16128 yank_type = MBLOCK;
16129 if (VIM_ISDIGIT(stropt[1]))
16130 {
16131 ++stropt;
16132 block_len = getdigits(&stropt) - 1;
16133 --stropt;
16134 }
16135 break;
16136#endif
16137 }
16138 }
16139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016140 strval = get_tv_string_chk(&argvars[1]);
16141 if (strval != NULL)
16142 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016144 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145}
16146
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016147/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016148 * "settabvar()" function
16149 */
16150 static void
16151f_settabvar(argvars, rettv)
16152 typval_T *argvars;
16153 typval_T *rettv;
16154{
16155 tabpage_T *save_curtab;
16156 char_u *varname, *tabvarname;
16157 typval_T *varp;
16158 tabpage_T *tp;
16159
16160 rettv->vval.v_number = 0;
16161
16162 if (check_restricted() || check_secure())
16163 return;
16164
16165 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16166 varname = get_tv_string_chk(&argvars[1]);
16167 varp = &argvars[2];
16168
16169 if (tp != NULL && varname != NULL && varp != NULL)
16170 {
16171 save_curtab = curtab;
16172 goto_tabpage_tp(tp);
16173
16174 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16175 if (tabvarname != NULL)
16176 {
16177 STRCPY(tabvarname, "t:");
16178 STRCPY(tabvarname + 2, varname);
16179 set_var(tabvarname, varp, TRUE);
16180 vim_free(tabvarname);
16181 }
16182
16183 /* Restore current tabpage */
16184 if (valid_tabpage(save_curtab))
16185 goto_tabpage_tp(save_curtab);
16186 }
16187}
16188
16189/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016190 * "settabwinvar()" function
16191 */
16192 static void
16193f_settabwinvar(argvars, rettv)
16194 typval_T *argvars;
16195 typval_T *rettv;
16196{
16197 setwinvar(argvars, rettv, 1);
16198}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199
16200/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016201 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016204f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016205 typval_T *argvars;
16206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016208 setwinvar(argvars, rettv, 0);
16209}
16210
16211/*
16212 * "setwinvar()" and "settabwinvar()" functions
16213 */
16214 static void
16215setwinvar(argvars, rettv, off)
16216 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016217 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016218 int off;
16219{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 win_T *win;
16221#ifdef FEAT_WINDOWS
16222 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016223 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224#endif
16225 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016226 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016228 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229
16230 if (check_restricted() || check_secure())
16231 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016232
16233#ifdef FEAT_WINDOWS
16234 if (off == 1)
16235 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16236 else
16237 tp = curtab;
16238#endif
16239 win = find_win_by_nr(&argvars[off], tp);
16240 varname = get_tv_string_chk(&argvars[off + 1]);
16241 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242
16243 if (win != NULL && varname != NULL && varp != NULL)
16244 {
16245#ifdef FEAT_WINDOWS
16246 /* set curwin to be our win, temporarily */
16247 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016248 save_curtab = curtab;
16249 goto_tabpage_tp(tp);
16250 if (!win_valid(win))
16251 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 curwin = win;
16253 curbuf = curwin->w_buffer;
16254#endif
16255
16256 if (*varname == '&')
16257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 long numval;
16259 char_u *strval;
16260 int error = FALSE;
16261
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016263 numval = get_tv_number_chk(varp, &error);
16264 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016265 if (!error && strval != NULL)
16266 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 }
16268 else
16269 {
16270 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16271 if (winvarname != NULL)
16272 {
16273 STRCPY(winvarname, "w:");
16274 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016275 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 vim_free(winvarname);
16277 }
16278 }
16279
16280#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016281 /* Restore current tabpage and window, if still valid (autocomands can
16282 * make them invalid). */
16283 if (valid_tabpage(save_curtab))
16284 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 if (win_valid(save_curwin))
16286 {
16287 curwin = save_curwin;
16288 curbuf = curwin->w_buffer;
16289 }
16290#endif
16291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292}
16293
16294/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016295 * "shellescape({string})" function
16296 */
16297 static void
16298f_shellescape(argvars, rettv)
16299 typval_T *argvars;
16300 typval_T *rettv;
16301{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016302 rettv->vval.v_string = vim_strsave_shellescape(
16303 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016304 rettv->v_type = VAR_STRING;
16305}
16306
16307/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309 */
16310 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016311f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016312 typval_T *argvars;
16313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317 p = get_tv_string(&argvars[0]);
16318 rettv->vval.v_string = vim_strsave(p);
16319 simplify_filename(rettv->vval.v_string); /* simplify in place */
16320 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321}
16322
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016323#ifdef FEAT_FLOAT
16324/*
16325 * "sin()" function
16326 */
16327 static void
16328f_sin(argvars, rettv)
16329 typval_T *argvars;
16330 typval_T *rettv;
16331{
16332 float_T f;
16333
16334 rettv->v_type = VAR_FLOAT;
16335 if (get_float_arg(argvars, &f) == OK)
16336 rettv->vval.v_float = sin(f);
16337 else
16338 rettv->vval.v_float = 0.0;
16339}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016340
16341/*
16342 * "sinh()" function
16343 */
16344 static void
16345f_sinh(argvars, rettv)
16346 typval_T *argvars;
16347 typval_T *rettv;
16348{
16349 float_T f;
16350
16351 rettv->v_type = VAR_FLOAT;
16352 if (get_float_arg(argvars, &f) == OK)
16353 rettv->vval.v_float = sinh(f);
16354 else
16355 rettv->vval.v_float = 0.0;
16356}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016357#endif
16358
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359static int
16360#ifdef __BORLANDC__
16361 _RTLENTRYF
16362#endif
16363 item_compare __ARGS((const void *s1, const void *s2));
16364static int
16365#ifdef __BORLANDC__
16366 _RTLENTRYF
16367#endif
16368 item_compare2 __ARGS((const void *s1, const void *s2));
16369
16370static int item_compare_ic;
16371static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016372static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016373static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016374#define ITEM_COMPARE_FAIL 999
16375
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016377 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379 static int
16380#ifdef __BORLANDC__
16381_RTLENTRYF
16382#endif
16383item_compare(s1, s2)
16384 const void *s1;
16385 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016387 char_u *p1, *p2;
16388 char_u *tofree1, *tofree2;
16389 int res;
16390 char_u numbuf1[NUMBUFLEN];
16391 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016393 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16394 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016395 if (p1 == NULL)
16396 p1 = (char_u *)"";
16397 if (p2 == NULL)
16398 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 if (item_compare_ic)
16400 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016402 res = STRCMP(p1, p2);
16403 vim_free(tofree1);
16404 vim_free(tofree2);
16405 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406}
16407
16408 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409#ifdef __BORLANDC__
16410_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412item_compare2(s1, s2)
16413 const void *s1;
16414 const void *s2;
16415{
16416 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016417 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016418 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016421 /* shortcut after failure in previous call; compare all items equal */
16422 if (item_compare_func_err)
16423 return 0;
16424
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016425 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16426 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016427 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16428 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429
16430 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016431 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016432 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16433 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016434 clear_tv(&argv[0]);
16435 clear_tv(&argv[1]);
16436
16437 if (res == FAIL)
16438 res = ITEM_COMPARE_FAIL;
16439 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016440 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16441 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016442 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443 clear_tv(&rettv);
16444 return res;
16445}
16446
16447/*
16448 * "sort({list})" function
16449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016451f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 typval_T *argvars;
16453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454{
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 list_T *l;
16456 listitem_T *li;
16457 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 long len;
16459 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460
Bram Moolenaar0d660222005-01-07 21:51:51 +000016461 if (argvars[0].v_type != VAR_LIST)
16462 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 else
16464 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016466 if (l == NULL || tv_check_lock(l->lv_lock,
16467 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468 return;
16469 rettv->vval.v_list = l;
16470 rettv->v_type = VAR_LIST;
16471 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472
Bram Moolenaar0d660222005-01-07 21:51:51 +000016473 len = list_len(l);
16474 if (len <= 1)
16475 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477 item_compare_ic = FALSE;
16478 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016479 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 if (argvars[1].v_type != VAR_UNKNOWN)
16481 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016482 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016484 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016485 else
16486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016487 int error = FALSE;
16488
16489 i = get_tv_number_chk(&argvars[1], &error);
16490 if (error)
16491 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492 if (i == 1)
16493 item_compare_ic = TRUE;
16494 else
16495 item_compare_func = get_tv_string(&argvars[1]);
16496 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016497
16498 if (argvars[2].v_type != VAR_UNKNOWN)
16499 {
16500 /* optional third argument: {dict} */
16501 if (argvars[2].v_type != VAR_DICT)
16502 {
16503 EMSG(_(e_dictreq));
16504 return;
16505 }
16506 item_compare_selfdict = argvars[2].vval.v_dict;
16507 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016511 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 if (ptrs == NULL)
16513 return;
16514 i = 0;
16515 for (li = l->lv_first; li != NULL; li = li->li_next)
16516 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016518 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016519 /* test the compare function */
16520 if (item_compare_func != NULL
16521 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16522 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016523 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 {
16526 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016527 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528 item_compare_func == NULL ? item_compare : item_compare2);
16529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 if (!item_compare_func_err)
16531 {
16532 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016533 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016534 l->lv_len = 0;
16535 for (i = 0; i < len; ++i)
16536 list_append(l, ptrs[i]);
16537 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016538 }
16539
16540 vim_free(ptrs);
16541 }
16542}
16543
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016544/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016545 * "soundfold({word})" function
16546 */
16547 static void
16548f_soundfold(argvars, rettv)
16549 typval_T *argvars;
16550 typval_T *rettv;
16551{
16552 char_u *s;
16553
16554 rettv->v_type = VAR_STRING;
16555 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016556#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016557 rettv->vval.v_string = eval_soundfold(s);
16558#else
16559 rettv->vval.v_string = vim_strsave(s);
16560#endif
16561}
16562
16563/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016564 * "spellbadword()" function
16565 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016566 static void
16567f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016568 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016569 typval_T *rettv;
16570{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016571 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016572 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016573 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016574
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016575 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016576 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016577
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016578#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016579 if (argvars[0].v_type == VAR_UNKNOWN)
16580 {
16581 /* Find the start and length of the badly spelled word. */
16582 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16583 if (len != 0)
16584 word = ml_get_cursor();
16585 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016586 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016587 {
16588 char_u *str = get_tv_string_chk(&argvars[0]);
16589 int capcol = -1;
16590
16591 if (str != NULL)
16592 {
16593 /* Check the argument for spelling. */
16594 while (*str != NUL)
16595 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016596 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016597 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016598 {
16599 word = str;
16600 break;
16601 }
16602 str += len;
16603 }
16604 }
16605 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016606#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016607
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016608 list_append_string(rettv->vval.v_list, word, len);
16609 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016610 attr == HLF_SPB ? "bad" :
16611 attr == HLF_SPR ? "rare" :
16612 attr == HLF_SPL ? "local" :
16613 attr == HLF_SPC ? "caps" :
16614 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016615}
16616
16617/*
16618 * "spellsuggest()" function
16619 */
16620 static void
16621f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016622 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016623 typval_T *rettv;
16624{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016625#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016626 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016627 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016628 int maxcount;
16629 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016630 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016631 listitem_T *li;
16632 int need_capital = FALSE;
16633#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016635 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016636 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016637
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016638#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016639 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016640 {
16641 str = get_tv_string(&argvars[0]);
16642 if (argvars[1].v_type != VAR_UNKNOWN)
16643 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016644 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016645 if (maxcount <= 0)
16646 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016647 if (argvars[2].v_type != VAR_UNKNOWN)
16648 {
16649 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16650 if (typeerr)
16651 return;
16652 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016653 }
16654 else
16655 maxcount = 25;
16656
Bram Moolenaar4770d092006-01-12 23:22:24 +000016657 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016658
16659 for (i = 0; i < ga.ga_len; ++i)
16660 {
16661 str = ((char_u **)ga.ga_data)[i];
16662
16663 li = listitem_alloc();
16664 if (li == NULL)
16665 vim_free(str);
16666 else
16667 {
16668 li->li_tv.v_type = VAR_STRING;
16669 li->li_tv.v_lock = 0;
16670 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016671 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016672 }
16673 }
16674 ga_clear(&ga);
16675 }
16676#endif
16677}
16678
Bram Moolenaar0d660222005-01-07 21:51:51 +000016679 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016680f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016681 typval_T *argvars;
16682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683{
16684 char_u *str;
16685 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016686 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016687 regmatch_T regmatch;
16688 char_u patbuf[NUMBUFLEN];
16689 char_u *save_cpo;
16690 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016692 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016693 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016694
16695 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16696 save_cpo = p_cpo;
16697 p_cpo = (char_u *)"";
16698
16699 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016700 if (argvars[1].v_type != VAR_UNKNOWN)
16701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016702 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16703 if (pat == NULL)
16704 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016706 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016707 }
16708 if (pat == NULL || *pat == NUL)
16709 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016711 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016713 if (typeerr)
16714 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715
Bram Moolenaar0d660222005-01-07 21:51:51 +000016716 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16717 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016719 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016720 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016721 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016722 if (*str == NUL)
16723 match = FALSE; /* empty item at the end */
16724 else
16725 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726 if (match)
16727 end = regmatch.startp[0];
16728 else
16729 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016730 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16731 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016732 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016733 if (list_append_string(rettv->vval.v_list, str,
16734 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016735 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016736 }
16737 if (!match)
16738 break;
16739 /* Advance to just after the match. */
16740 if (regmatch.endp[0] > str)
16741 col = 0;
16742 else
16743 {
16744 /* Don't get stuck at the same match. */
16745#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016746 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016747#else
16748 col = 1;
16749#endif
16750 }
16751 str = regmatch.endp[0];
16752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756
Bram Moolenaar0d660222005-01-07 21:51:51 +000016757 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758}
16759
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016760#ifdef FEAT_FLOAT
16761/*
16762 * "sqrt()" function
16763 */
16764 static void
16765f_sqrt(argvars, rettv)
16766 typval_T *argvars;
16767 typval_T *rettv;
16768{
16769 float_T f;
16770
16771 rettv->v_type = VAR_FLOAT;
16772 if (get_float_arg(argvars, &f) == OK)
16773 rettv->vval.v_float = sqrt(f);
16774 else
16775 rettv->vval.v_float = 0.0;
16776}
16777
16778/*
16779 * "str2float()" function
16780 */
16781 static void
16782f_str2float(argvars, rettv)
16783 typval_T *argvars;
16784 typval_T *rettv;
16785{
16786 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16787
16788 if (*p == '+')
16789 p = skipwhite(p + 1);
16790 (void)string2float(p, &rettv->vval.v_float);
16791 rettv->v_type = VAR_FLOAT;
16792}
16793#endif
16794
Bram Moolenaar2c932302006-03-18 21:42:09 +000016795/*
16796 * "str2nr()" function
16797 */
16798 static void
16799f_str2nr(argvars, rettv)
16800 typval_T *argvars;
16801 typval_T *rettv;
16802{
16803 int base = 10;
16804 char_u *p;
16805 long n;
16806
16807 if (argvars[1].v_type != VAR_UNKNOWN)
16808 {
16809 base = get_tv_number(&argvars[1]);
16810 if (base != 8 && base != 10 && base != 16)
16811 {
16812 EMSG(_(e_invarg));
16813 return;
16814 }
16815 }
16816
16817 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016818 if (*p == '+')
16819 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016820 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16821 rettv->vval.v_number = n;
16822}
16823
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824#ifdef HAVE_STRFTIME
16825/*
16826 * "strftime({format}[, {time}])" function
16827 */
16828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016829f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016830 typval_T *argvars;
16831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832{
16833 char_u result_buf[256];
16834 struct tm *curtime;
16835 time_t seconds;
16836 char_u *p;
16837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016838 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016840 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016841 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 seconds = time(NULL);
16843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016844 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 curtime = localtime(&seconds);
16846 /* MSVC returns NULL for an invalid value of seconds. */
16847 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016848 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 else
16850 {
16851# ifdef FEAT_MBYTE
16852 vimconv_T conv;
16853 char_u *enc;
16854
16855 conv.vc_type = CONV_NONE;
16856 enc = enc_locale();
16857 convert_setup(&conv, p_enc, enc);
16858 if (conv.vc_type != CONV_NONE)
16859 p = string_convert(&conv, p, NULL);
16860# endif
16861 if (p != NULL)
16862 (void)strftime((char *)result_buf, sizeof(result_buf),
16863 (char *)p, curtime);
16864 else
16865 result_buf[0] = NUL;
16866
16867# ifdef FEAT_MBYTE
16868 if (conv.vc_type != CONV_NONE)
16869 vim_free(p);
16870 convert_setup(&conv, enc, p_enc);
16871 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016872 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 else
16874# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016875 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876
16877# ifdef FEAT_MBYTE
16878 /* Release conversion descriptors */
16879 convert_setup(&conv, NULL, NULL);
16880 vim_free(enc);
16881# endif
16882 }
16883}
16884#endif
16885
16886/*
16887 * "stridx()" function
16888 */
16889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016890f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 typval_T *argvars;
16892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893{
16894 char_u buf[NUMBUFLEN];
16895 char_u *needle;
16896 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016899 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 needle = get_tv_string_chk(&argvars[1]);
16902 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016903 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016904 if (needle == NULL || haystack == NULL)
16905 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906
Bram Moolenaar33570922005-01-25 22:26:29 +000016907 if (argvars[2].v_type != VAR_UNKNOWN)
16908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016909 int error = FALSE;
16910
16911 start_idx = get_tv_number_chk(&argvars[2], &error);
16912 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016914 if (start_idx >= 0)
16915 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 }
16917
16918 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16919 if (pos != NULL)
16920 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921}
16922
16923/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016924 * "string()" function
16925 */
16926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016927f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016928 typval_T *argvars;
16929 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016930{
16931 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016932 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016934 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016935 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016936 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016937 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016938 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939}
16940
16941/*
16942 * "strlen()" function
16943 */
16944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016945f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016946 typval_T *argvars;
16947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016949 rettv->vval.v_number = (varnumber_T)(STRLEN(
16950 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951}
16952
16953/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016954 * "strchars()" function
16955 */
16956 static void
16957f_strchars(argvars, rettv)
16958 typval_T *argvars;
16959 typval_T *rettv;
16960{
16961 char_u *s = get_tv_string(&argvars[0]);
16962#ifdef FEAT_MBYTE
16963 varnumber_T len = 0;
16964
16965 while (*s != NUL)
16966 {
16967 mb_cptr2char_adv(&s);
16968 ++len;
16969 }
16970 rettv->vval.v_number = len;
16971#else
16972 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16973#endif
16974}
16975
16976/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016977 * "strdisplaywidth()" function
16978 */
16979 static void
16980f_strdisplaywidth(argvars, rettv)
16981 typval_T *argvars;
16982 typval_T *rettv;
16983{
16984 char_u *s = get_tv_string(&argvars[0]);
16985 int col = 0;
16986
16987 if (argvars[1].v_type != VAR_UNKNOWN)
16988 col = get_tv_number(&argvars[1]);
16989
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016990 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016991}
16992
16993/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016994 * "strwidth()" function
16995 */
16996 static void
16997f_strwidth(argvars, rettv)
16998 typval_T *argvars;
16999 typval_T *rettv;
17000{
17001 char_u *s = get_tv_string(&argvars[0]);
17002
17003 rettv->vval.v_number = (varnumber_T)(
17004#ifdef FEAT_MBYTE
17005 mb_string2cells(s, -1)
17006#else
17007 STRLEN(s)
17008#endif
17009 );
17010}
17011
17012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 * "strpart()" function
17014 */
17015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017016f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017017 typval_T *argvars;
17018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019{
17020 char_u *p;
17021 int n;
17022 int len;
17023 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017024 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 slen = (int)STRLEN(p);
17028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 n = get_tv_number_chk(&argvars[1], &error);
17030 if (error)
17031 len = 0;
17032 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017033 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 else
17035 len = slen - n; /* default len: all bytes that are available. */
17036
17037 /*
17038 * Only return the overlap between the specified part and the actual
17039 * string.
17040 */
17041 if (n < 0)
17042 {
17043 len += n;
17044 n = 0;
17045 }
17046 else if (n > slen)
17047 n = slen;
17048 if (len < 0)
17049 len = 0;
17050 else if (n + len > slen)
17051 len = slen - n;
17052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017053 rettv->v_type = VAR_STRING;
17054 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055}
17056
17057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058 * "strridx()" function
17059 */
17060 static void
17061f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017062 typval_T *argvars;
17063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064{
17065 char_u buf[NUMBUFLEN];
17066 char_u *needle;
17067 char_u *haystack;
17068 char_u *rest;
17069 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017070 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017072 needle = get_tv_string_chk(&argvars[1]);
17073 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017074
17075 rettv->vval.v_number = -1;
17076 if (needle == NULL || haystack == NULL)
17077 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017078
17079 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017080 if (argvars[2].v_type != VAR_UNKNOWN)
17081 {
17082 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017083 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017084 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017085 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017086 }
17087 else
17088 end_idx = haystack_len;
17089
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017091 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017093 lastmatch = haystack + end_idx;
17094 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017096 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097 for (rest = haystack; *rest != '\0'; ++rest)
17098 {
17099 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017100 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 break;
17102 lastmatch = rest;
17103 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017104 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105
17106 if (lastmatch == NULL)
17107 rettv->vval.v_number = -1;
17108 else
17109 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17110}
17111
17112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113 * "strtrans()" function
17114 */
17115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 typval_T *argvars;
17118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120 rettv->v_type = VAR_STRING;
17121 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122}
17123
17124/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125 * "submatch()" function
17126 */
17127 static void
17128f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 typval_T *argvars;
17130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017131{
17132 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017133 rettv->vval.v_string =
17134 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135}
17136
17137/*
17138 * "substitute()" function
17139 */
17140 static void
17141f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017142 typval_T *argvars;
17143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144{
17145 char_u patbuf[NUMBUFLEN];
17146 char_u subbuf[NUMBUFLEN];
17147 char_u flagsbuf[NUMBUFLEN];
17148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017149 char_u *str = get_tv_string_chk(&argvars[0]);
17150 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17151 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17152 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17153
Bram Moolenaar0d660222005-01-07 21:51:51 +000017154 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017155 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17156 rettv->vval.v_string = NULL;
17157 else
17158 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159}
17160
17161/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017162 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168{
17169 int id = 0;
17170#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017171 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 long col;
17173 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017174 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017176 lnum = get_tv_lnum(argvars); /* -1 on type error */
17177 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17178 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017180 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017181 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017182 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183#endif
17184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186}
17187
17188/*
17189 * "synIDattr(id, what [, mode])" function
17190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017193 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195{
17196 char_u *p = NULL;
17197#ifdef FEAT_SYN_HL
17198 int id;
17199 char_u *what;
17200 char_u *mode;
17201 char_u modebuf[NUMBUFLEN];
17202 int modec;
17203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204 id = get_tv_number(&argvars[0]);
17205 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017206 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017208 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017210 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 modec = 0; /* replace invalid with current */
17212 }
17213 else
17214 {
17215#ifdef FEAT_GUI
17216 if (gui.in_use)
17217 modec = 'g';
17218 else
17219#endif
17220 if (t_colors > 1)
17221 modec = 'c';
17222 else
17223 modec = 't';
17224 }
17225
17226
17227 switch (TOLOWER_ASC(what[0]))
17228 {
17229 case 'b':
17230 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17231 p = highlight_color(id, what, modec);
17232 else /* bold */
17233 p = highlight_has_attr(id, HL_BOLD, modec);
17234 break;
17235
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017236 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 p = highlight_color(id, what, modec);
17238 break;
17239
17240 case 'i':
17241 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17242 p = highlight_has_attr(id, HL_INVERSE, modec);
17243 else /* italic */
17244 p = highlight_has_attr(id, HL_ITALIC, modec);
17245 break;
17246
17247 case 'n': /* name */
17248 p = get_highlight_name(NULL, id - 1);
17249 break;
17250
17251 case 'r': /* reverse */
17252 p = highlight_has_attr(id, HL_INVERSE, modec);
17253 break;
17254
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017255 case 's':
17256 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17257 p = highlight_color(id, what, modec);
17258 else /* standout */
17259 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 break;
17261
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017262 case 'u':
17263 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17264 /* underline */
17265 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17266 else
17267 /* undercurl */
17268 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 break;
17270 }
17271
17272 if (p != NULL)
17273 p = vim_strsave(p);
17274#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275 rettv->v_type = VAR_STRING;
17276 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277}
17278
17279/*
17280 * "synIDtrans(id)" function
17281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017283f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017284 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286{
17287 int id;
17288
17289#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017290 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291
17292 if (id > 0)
17293 id = syn_get_final_id(id);
17294 else
17295#endif
17296 id = 0;
17297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017298 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299}
17300
17301/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017302 * "synconcealed(lnum, col)" function
17303 */
17304 static void
17305f_synconcealed(argvars, rettv)
17306 typval_T *argvars UNUSED;
17307 typval_T *rettv;
17308{
17309#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17310 long lnum;
17311 long col;
17312 int syntax_flags = 0;
17313 int cchar;
17314 int matchid = 0;
17315 char_u str[NUMBUFLEN];
17316#endif
17317
17318 rettv->v_type = VAR_LIST;
17319 rettv->vval.v_list = NULL;
17320
17321#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17322 lnum = get_tv_lnum(argvars); /* -1 on type error */
17323 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17324
17325 vim_memset(str, NUL, sizeof(str));
17326
17327 if (rettv_list_alloc(rettv) != FAIL)
17328 {
17329 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17330 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17331 && curwin->w_p_cole > 0)
17332 {
17333 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17334 syntax_flags = get_syntax_info(&matchid);
17335
17336 /* get the conceal character */
17337 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17338 {
17339 cchar = syn_get_sub_char();
17340 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17341 cchar = lcs_conceal;
17342 if (cchar != NUL)
17343 {
17344# ifdef FEAT_MBYTE
17345 if (has_mbyte)
17346 (*mb_char2bytes)(cchar, str);
17347 else
17348# endif
17349 str[0] = cchar;
17350 }
17351 }
17352 }
17353
17354 list_append_number(rettv->vval.v_list,
17355 (syntax_flags & HL_CONCEAL) != 0);
17356 /* -1 to auto-determine strlen */
17357 list_append_string(rettv->vval.v_list, str, -1);
17358 list_append_number(rettv->vval.v_list, matchid);
17359 }
17360#endif
17361}
17362
17363/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017364 * "synstack(lnum, col)" function
17365 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017366 static void
17367f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017368 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017369 typval_T *rettv;
17370{
17371#ifdef FEAT_SYN_HL
17372 long lnum;
17373 long col;
17374 int i;
17375 int id;
17376#endif
17377
17378 rettv->v_type = VAR_LIST;
17379 rettv->vval.v_list = NULL;
17380
17381#ifdef FEAT_SYN_HL
17382 lnum = get_tv_lnum(argvars); /* -1 on type error */
17383 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17384
17385 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017386 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017387 && rettv_list_alloc(rettv) != FAIL)
17388 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017389 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017390 for (i = 0; ; ++i)
17391 {
17392 id = syn_get_stack_item(i);
17393 if (id < 0)
17394 break;
17395 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17396 break;
17397 }
17398 }
17399#endif
17400}
17401
17402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 * "system()" function
17404 */
17405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017407 typval_T *argvars;
17408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017410 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017412 char_u *infile = NULL;
17413 char_u buf[NUMBUFLEN];
17414 int err = FALSE;
17415 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017417 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017418 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017419
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017420 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017421 {
17422 /*
17423 * Write the string to a temp file, to be used for input of the shell
17424 * command.
17425 */
17426 if ((infile = vim_tempname('i')) == NULL)
17427 {
17428 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017429 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017430 }
17431
17432 fd = mch_fopen((char *)infile, WRITEBIN);
17433 if (fd == NULL)
17434 {
17435 EMSG2(_(e_notopen), infile);
17436 goto done;
17437 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017438 p = get_tv_string_buf_chk(&argvars[1], buf);
17439 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017440 {
17441 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017442 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017443 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017444 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17445 err = TRUE;
17446 if (fclose(fd) != 0)
17447 err = TRUE;
17448 if (err)
17449 {
17450 EMSG(_("E677: Error writing temp file"));
17451 goto done;
17452 }
17453 }
17454
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017455 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17456 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017457
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458#ifdef USE_CR
17459 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017460 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 {
17462 char_u *s;
17463
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017464 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 {
17466 if (*s == CAR)
17467 *s = NL;
17468 }
17469 }
17470#else
17471# ifdef USE_CRNL
17472 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017473 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 {
17475 char_u *s, *d;
17476
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017477 d = res;
17478 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 {
17480 if (s[0] == CAR && s[1] == NL)
17481 ++s;
17482 *d++ = *s;
17483 }
17484 *d = NUL;
17485 }
17486# endif
17487#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017488
17489done:
17490 if (infile != NULL)
17491 {
17492 mch_remove(infile);
17493 vim_free(infile);
17494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495 rettv->v_type = VAR_STRING;
17496 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497}
17498
17499/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017500 * "tabpagebuflist()" function
17501 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017502 static void
17503f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017504 typval_T *argvars UNUSED;
17505 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017506{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017507#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017508 tabpage_T *tp;
17509 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017510
17511 if (argvars[0].v_type == VAR_UNKNOWN)
17512 wp = firstwin;
17513 else
17514 {
17515 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17516 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017517 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017518 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017519 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017520 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017521 for (; wp != NULL; wp = wp->w_next)
17522 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017523 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017524 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017525 }
17526#endif
17527}
17528
17529
17530/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017531 * "tabpagenr()" function
17532 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017533 static void
17534f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017535 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017536 typval_T *rettv;
17537{
17538 int nr = 1;
17539#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017540 char_u *arg;
17541
17542 if (argvars[0].v_type != VAR_UNKNOWN)
17543 {
17544 arg = get_tv_string_chk(&argvars[0]);
17545 nr = 0;
17546 if (arg != NULL)
17547 {
17548 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017549 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017550 else
17551 EMSG2(_(e_invexpr2), arg);
17552 }
17553 }
17554 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017555 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017556#endif
17557 rettv->vval.v_number = nr;
17558}
17559
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017560
17561#ifdef FEAT_WINDOWS
17562static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17563
17564/*
17565 * Common code for tabpagewinnr() and winnr().
17566 */
17567 static int
17568get_winnr(tp, argvar)
17569 tabpage_T *tp;
17570 typval_T *argvar;
17571{
17572 win_T *twin;
17573 int nr = 1;
17574 win_T *wp;
17575 char_u *arg;
17576
17577 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17578 if (argvar->v_type != VAR_UNKNOWN)
17579 {
17580 arg = get_tv_string_chk(argvar);
17581 if (arg == NULL)
17582 nr = 0; /* type error; errmsg already given */
17583 else if (STRCMP(arg, "$") == 0)
17584 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17585 else if (STRCMP(arg, "#") == 0)
17586 {
17587 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17588 if (twin == NULL)
17589 nr = 0;
17590 }
17591 else
17592 {
17593 EMSG2(_(e_invexpr2), arg);
17594 nr = 0;
17595 }
17596 }
17597
17598 if (nr > 0)
17599 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17600 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017601 {
17602 if (wp == NULL)
17603 {
17604 /* didn't find it in this tabpage */
17605 nr = 0;
17606 break;
17607 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017608 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017609 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017610 return nr;
17611}
17612#endif
17613
17614/*
17615 * "tabpagewinnr()" function
17616 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017617 static void
17618f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017619 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017620 typval_T *rettv;
17621{
17622 int nr = 1;
17623#ifdef FEAT_WINDOWS
17624 tabpage_T *tp;
17625
17626 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17627 if (tp == NULL)
17628 nr = 0;
17629 else
17630 nr = get_winnr(tp, &argvars[1]);
17631#endif
17632 rettv->vval.v_number = nr;
17633}
17634
17635
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017636/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017637 * "tagfiles()" function
17638 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017639 static void
17640f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017641 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017642 typval_T *rettv;
17643{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017644 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017645 tagname_T tn;
17646 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017647
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017648 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017649 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017650 fname = alloc(MAXPATHL);
17651 if (fname == NULL)
17652 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017653
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017654 for (first = TRUE; ; first = FALSE)
17655 if (get_tagfname(&tn, first, fname) == FAIL
17656 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017657 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017658 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017659 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017660}
17661
17662/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017663 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017664 */
17665 static void
17666f_taglist(argvars, rettv)
17667 typval_T *argvars;
17668 typval_T *rettv;
17669{
17670 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017671
17672 tag_pattern = get_tv_string(&argvars[0]);
17673
17674 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017675 if (*tag_pattern == NUL)
17676 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017677
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017678 if (rettv_list_alloc(rettv) == OK)
17679 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017680}
17681
17682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 * "tempname()" function
17684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017686f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017687 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689{
17690 static int x = 'A';
17691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017692 rettv->v_type = VAR_STRING;
17693 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694
17695 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17696 * names. Skip 'I' and 'O', they are used for shell redirection. */
17697 do
17698 {
17699 if (x == 'Z')
17700 x = '0';
17701 else if (x == '9')
17702 x = 'A';
17703 else
17704 {
17705#ifdef EBCDIC
17706 if (x == 'I')
17707 x = 'J';
17708 else if (x == 'R')
17709 x = 'S';
17710 else
17711#endif
17712 ++x;
17713 }
17714 } while (x == 'I' || x == 'O');
17715}
17716
17717/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017718 * "test(list)" function: Just checking the walls...
17719 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017720 static void
17721f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017722 typval_T *argvars UNUSED;
17723 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017724{
17725 /* Used for unit testing. Change the code below to your liking. */
17726#if 0
17727 listitem_T *li;
17728 list_T *l;
17729 char_u *bad, *good;
17730
17731 if (argvars[0].v_type != VAR_LIST)
17732 return;
17733 l = argvars[0].vval.v_list;
17734 if (l == NULL)
17735 return;
17736 li = l->lv_first;
17737 if (li == NULL)
17738 return;
17739 bad = get_tv_string(&li->li_tv);
17740 li = li->li_next;
17741 if (li == NULL)
17742 return;
17743 good = get_tv_string(&li->li_tv);
17744 rettv->vval.v_number = test_edit_score(bad, good);
17745#endif
17746}
17747
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017748#ifdef FEAT_FLOAT
17749/*
17750 * "tan()" function
17751 */
17752 static void
17753f_tan(argvars, rettv)
17754 typval_T *argvars;
17755 typval_T *rettv;
17756{
17757 float_T f;
17758
17759 rettv->v_type = VAR_FLOAT;
17760 if (get_float_arg(argvars, &f) == OK)
17761 rettv->vval.v_float = tan(f);
17762 else
17763 rettv->vval.v_float = 0.0;
17764}
17765
17766/*
17767 * "tanh()" function
17768 */
17769 static void
17770f_tanh(argvars, rettv)
17771 typval_T *argvars;
17772 typval_T *rettv;
17773{
17774 float_T f;
17775
17776 rettv->v_type = VAR_FLOAT;
17777 if (get_float_arg(argvars, &f) == OK)
17778 rettv->vval.v_float = tanh(f);
17779 else
17780 rettv->vval.v_float = 0.0;
17781}
17782#endif
17783
Bram Moolenaard52d9742005-08-21 22:20:28 +000017784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785 * "tolower(string)" function
17786 */
17787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017788f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017789 typval_T *argvars;
17790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791{
17792 char_u *p;
17793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017794 p = vim_strsave(get_tv_string(&argvars[0]));
17795 rettv->v_type = VAR_STRING;
17796 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797
17798 if (p != NULL)
17799 while (*p != NUL)
17800 {
17801#ifdef FEAT_MBYTE
17802 int l;
17803
17804 if (enc_utf8)
17805 {
17806 int c, lc;
17807
17808 c = utf_ptr2char(p);
17809 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017810 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 /* TODO: reallocate string when byte count changes. */
17812 if (utf_char2len(lc) == l)
17813 utf_char2bytes(lc, p);
17814 p += l;
17815 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017816 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 p += l; /* skip multi-byte character */
17818 else
17819#endif
17820 {
17821 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17822 ++p;
17823 }
17824 }
17825}
17826
17827/*
17828 * "toupper(string)" function
17829 */
17830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017831f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017832 typval_T *argvars;
17833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017835 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017836 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837}
17838
17839/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017840 * "tr(string, fromstr, tostr)" function
17841 */
17842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017843f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017844 typval_T *argvars;
17845 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017846{
17847 char_u *instr;
17848 char_u *fromstr;
17849 char_u *tostr;
17850 char_u *p;
17851#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017852 int inlen;
17853 int fromlen;
17854 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017855 int idx;
17856 char_u *cpstr;
17857 int cplen;
17858 int first = TRUE;
17859#endif
17860 char_u buf[NUMBUFLEN];
17861 char_u buf2[NUMBUFLEN];
17862 garray_T ga;
17863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017864 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017865 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17866 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017867
17868 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869 rettv->v_type = VAR_STRING;
17870 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017871 if (fromstr == NULL || tostr == NULL)
17872 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017873 ga_init2(&ga, (int)sizeof(char), 80);
17874
17875#ifdef FEAT_MBYTE
17876 if (!has_mbyte)
17877#endif
17878 /* not multi-byte: fromstr and tostr must be the same length */
17879 if (STRLEN(fromstr) != STRLEN(tostr))
17880 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017881#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017882error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017883#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017884 EMSG2(_(e_invarg2), fromstr);
17885 ga_clear(&ga);
17886 return;
17887 }
17888
17889 /* fromstr and tostr have to contain the same number of chars */
17890 while (*instr != NUL)
17891 {
17892#ifdef FEAT_MBYTE
17893 if (has_mbyte)
17894 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017895 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017896 cpstr = instr;
17897 cplen = inlen;
17898 idx = 0;
17899 for (p = fromstr; *p != NUL; p += fromlen)
17900 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017901 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017902 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17903 {
17904 for (p = tostr; *p != NUL; p += tolen)
17905 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017906 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017907 if (idx-- == 0)
17908 {
17909 cplen = tolen;
17910 cpstr = p;
17911 break;
17912 }
17913 }
17914 if (*p == NUL) /* tostr is shorter than fromstr */
17915 goto error;
17916 break;
17917 }
17918 ++idx;
17919 }
17920
17921 if (first && cpstr == instr)
17922 {
17923 /* Check that fromstr and tostr have the same number of
17924 * (multi-byte) characters. Done only once when a character
17925 * of instr doesn't appear in fromstr. */
17926 first = FALSE;
17927 for (p = tostr; *p != NUL; p += tolen)
17928 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017929 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017930 --idx;
17931 }
17932 if (idx != 0)
17933 goto error;
17934 }
17935
17936 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017937 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017938 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017939
17940 instr += inlen;
17941 }
17942 else
17943#endif
17944 {
17945 /* When not using multi-byte chars we can do it faster. */
17946 p = vim_strchr(fromstr, *instr);
17947 if (p != NULL)
17948 ga_append(&ga, tostr[p - fromstr]);
17949 else
17950 ga_append(&ga, *instr);
17951 ++instr;
17952 }
17953 }
17954
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017955 /* add a terminating NUL */
17956 ga_grow(&ga, 1);
17957 ga_append(&ga, NUL);
17958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017959 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017960}
17961
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017962#ifdef FEAT_FLOAT
17963/*
17964 * "trunc({float})" function
17965 */
17966 static void
17967f_trunc(argvars, rettv)
17968 typval_T *argvars;
17969 typval_T *rettv;
17970{
17971 float_T f;
17972
17973 rettv->v_type = VAR_FLOAT;
17974 if (get_float_arg(argvars, &f) == OK)
17975 /* trunc() is not in C90, use floor() or ceil() instead. */
17976 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17977 else
17978 rettv->vval.v_float = 0.0;
17979}
17980#endif
17981
Bram Moolenaar8299df92004-07-10 09:47:34 +000017982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 * "type(expr)" function
17984 */
17985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017986f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017987 typval_T *argvars;
17988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017990 int n;
17991
17992 switch (argvars[0].v_type)
17993 {
17994 case VAR_NUMBER: n = 0; break;
17995 case VAR_STRING: n = 1; break;
17996 case VAR_FUNC: n = 2; break;
17997 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017998 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017999#ifdef FEAT_FLOAT
18000 case VAR_FLOAT: n = 5; break;
18001#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018002 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18003 }
18004 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005}
18006
18007/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018008 * "undofile(name)" function
18009 */
18010 static void
18011f_undofile(argvars, rettv)
18012 typval_T *argvars;
18013 typval_T *rettv;
18014{
18015 rettv->v_type = VAR_STRING;
18016#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018017 {
18018 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18019
18020 if (ffname != NULL)
18021 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18022 vim_free(ffname);
18023 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018024#else
18025 rettv->vval.v_string = NULL;
18026#endif
18027}
18028
18029/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018030 * "undotree()" function
18031 */
18032 static void
18033f_undotree(argvars, rettv)
18034 typval_T *argvars UNUSED;
18035 typval_T *rettv;
18036{
18037 if (rettv_dict_alloc(rettv) == OK)
18038 {
18039 dict_T *dict = rettv->vval.v_dict;
18040 list_T *list;
18041
Bram Moolenaar730cde92010-06-27 05:18:54 +020018042 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018043 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018044 dict_add_nr_str(dict, "save_last",
18045 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018046 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18047 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018048 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018049
18050 list = list_alloc();
18051 if (list != NULL)
18052 {
18053 u_eval_tree(curbuf->b_u_oldhead, list);
18054 dict_add_list(dict, "entries", list);
18055 }
18056 }
18057}
18058
18059/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018060 * "values(dict)" function
18061 */
18062 static void
18063f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018064 typval_T *argvars;
18065 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018066{
18067 dict_list(argvars, rettv, 1);
18068}
18069
18070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 * "virtcol(string)" function
18072 */
18073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018074f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018075 typval_T *argvars;
18076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077{
18078 colnr_T vcol = 0;
18079 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018080 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018082 fp = var2fpos(&argvars[0], FALSE, &fnum);
18083 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18084 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 {
18086 getvvcol(curwin, fp, NULL, NULL, &vcol);
18087 ++vcol;
18088 }
18089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018090 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091}
18092
18093/*
18094 * "visualmode()" function
18095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018097f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018098 typval_T *argvars UNUSED;
18099 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100{
18101#ifdef FEAT_VISUAL
18102 char_u str[2];
18103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018104 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 str[0] = curbuf->b_visual_mode_eval;
18106 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018107 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108
18109 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018110 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112#endif
18113}
18114
18115/*
18116 * "winbufnr(nr)" function
18117 */
18118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018119f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018120 typval_T *argvars;
18121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122{
18123 win_T *wp;
18124
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018125 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018127 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130}
18131
18132/*
18133 * "wincol()" function
18134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139{
18140 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142}
18143
18144/*
18145 * "winheight(nr)" function
18146 */
18147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018149 typval_T *argvars;
18150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151{
18152 win_T *wp;
18153
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018154 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018156 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018158 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159}
18160
18161/*
18162 * "winline()" function
18163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018165f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168{
18169 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171}
18172
18173/*
18174 * "winnr()" function
18175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018177f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180{
18181 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018182
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018184 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018186 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187}
18188
18189/*
18190 * "winrestcmd()" function
18191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018193f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018194 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196{
18197#ifdef FEAT_WINDOWS
18198 win_T *wp;
18199 int winnr = 1;
18200 garray_T ga;
18201 char_u buf[50];
18202
18203 ga_init2(&ga, (int)sizeof(char), 70);
18204 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18205 {
18206 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18207 ga_concat(&ga, buf);
18208# ifdef FEAT_VERTSPLIT
18209 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18210 ga_concat(&ga, buf);
18211# endif
18212 ++winnr;
18213 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018214 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018216 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018218 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221}
18222
18223/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018224 * "winrestview()" function
18225 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018226 static void
18227f_winrestview(argvars, rettv)
18228 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018229 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018230{
18231 dict_T *dict;
18232
18233 if (argvars[0].v_type != VAR_DICT
18234 || (dict = argvars[0].vval.v_dict) == NULL)
18235 EMSG(_(e_invarg));
18236 else
18237 {
18238 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18239 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18240#ifdef FEAT_VIRTUALEDIT
18241 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18242#endif
18243 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018244 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018245
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018246 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018247#ifdef FEAT_DIFF
18248 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18249#endif
18250 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18251 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18252
18253 check_cursor();
18254 changed_cline_bef_curs();
18255 invalidate_botline();
18256 redraw_later(VALID);
18257
18258 if (curwin->w_topline == 0)
18259 curwin->w_topline = 1;
18260 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18261 curwin->w_topline = curbuf->b_ml.ml_line_count;
18262#ifdef FEAT_DIFF
18263 check_topfill(curwin, TRUE);
18264#endif
18265 }
18266}
18267
18268/*
18269 * "winsaveview()" function
18270 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018271 static void
18272f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018273 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018274 typval_T *rettv;
18275{
18276 dict_T *dict;
18277
Bram Moolenaara800b422010-06-27 01:15:55 +020018278 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018279 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018280 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018281
18282 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18283 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18284#ifdef FEAT_VIRTUALEDIT
18285 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18286#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018287 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018288 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18289
18290 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18291#ifdef FEAT_DIFF
18292 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18293#endif
18294 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18295 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18296}
18297
18298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 * "winwidth(nr)" function
18300 */
18301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018302f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018303 typval_T *argvars;
18304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305{
18306 win_T *wp;
18307
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018308 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 else
18312#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018313 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018315 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316#endif
18317}
18318
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018320 * "writefile()" function
18321 */
18322 static void
18323f_writefile(argvars, rettv)
18324 typval_T *argvars;
18325 typval_T *rettv;
18326{
18327 int binary = FALSE;
18328 char_u *fname;
18329 FILE *fd;
18330 listitem_T *li;
18331 char_u *s;
18332 int ret = 0;
18333 int c;
18334
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018335 if (check_restricted() || check_secure())
18336 return;
18337
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018338 if (argvars[0].v_type != VAR_LIST)
18339 {
18340 EMSG2(_(e_listarg), "writefile()");
18341 return;
18342 }
18343 if (argvars[0].vval.v_list == NULL)
18344 return;
18345
18346 if (argvars[2].v_type != VAR_UNKNOWN
18347 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18348 binary = TRUE;
18349
18350 /* Always open the file in binary mode, library functions have a mind of
18351 * their own about CR-LF conversion. */
18352 fname = get_tv_string(&argvars[1]);
18353 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18354 {
18355 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18356 ret = -1;
18357 }
18358 else
18359 {
18360 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18361 li = li->li_next)
18362 {
18363 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18364 {
18365 if (*s == '\n')
18366 c = putc(NUL, fd);
18367 else
18368 c = putc(*s, fd);
18369 if (c == EOF)
18370 {
18371 ret = -1;
18372 break;
18373 }
18374 }
18375 if (!binary || li->li_next != NULL)
18376 if (putc('\n', fd) == EOF)
18377 {
18378 ret = -1;
18379 break;
18380 }
18381 if (ret < 0)
18382 {
18383 EMSG(_(e_write));
18384 break;
18385 }
18386 }
18387 fclose(fd);
18388 }
18389
18390 rettv->vval.v_number = ret;
18391}
18392
18393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018395 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 */
18397 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018398var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018399 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018400 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018401 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018403 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018405 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406
Bram Moolenaara5525202006-03-02 22:52:09 +000018407 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018408 if (varp->v_type == VAR_LIST)
18409 {
18410 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018411 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018412 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018413 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018414
18415 l = varp->vval.v_list;
18416 if (l == NULL)
18417 return NULL;
18418
18419 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018420 pos.lnum = list_find_nr(l, 0L, &error);
18421 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018422 return NULL; /* invalid line number */
18423
18424 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018425 pos.col = list_find_nr(l, 1L, &error);
18426 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018427 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018428 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018429
18430 /* We accept "$" for the column number: last column. */
18431 li = list_find(l, 1L);
18432 if (li != NULL && li->li_tv.v_type == VAR_STRING
18433 && li->li_tv.vval.v_string != NULL
18434 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18435 pos.col = len + 1;
18436
Bram Moolenaara5525202006-03-02 22:52:09 +000018437 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018438 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018439 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018440 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018441
Bram Moolenaara5525202006-03-02 22:52:09 +000018442#ifdef FEAT_VIRTUALEDIT
18443 /* Get the virtual offset. Defaults to zero. */
18444 pos.coladd = list_find_nr(l, 2L, &error);
18445 if (error)
18446 pos.coladd = 0;
18447#endif
18448
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018449 return &pos;
18450 }
18451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018452 name = get_tv_string_chk(varp);
18453 if (name == NULL)
18454 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018455 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018457#ifdef FEAT_VISUAL
18458 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18459 {
18460 if (VIsual_active)
18461 return &VIsual;
18462 return &curwin->w_cursor;
18463 }
18464#endif
18465 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018467 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18469 return NULL;
18470 return pp;
18471 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018472
18473#ifdef FEAT_VIRTUALEDIT
18474 pos.coladd = 0;
18475#endif
18476
Bram Moolenaar477933c2007-07-17 14:32:23 +000018477 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018478 {
18479 pos.col = 0;
18480 if (name[1] == '0') /* "w0": first visible line */
18481 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018482 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018483 pos.lnum = curwin->w_topline;
18484 return &pos;
18485 }
18486 else if (name[1] == '$') /* "w$": last visible line */
18487 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018488 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018489 pos.lnum = curwin->w_botline - 1;
18490 return &pos;
18491 }
18492 }
18493 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018495 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 {
18497 pos.lnum = curbuf->b_ml.ml_line_count;
18498 pos.col = 0;
18499 }
18500 else
18501 {
18502 pos.lnum = curwin->w_cursor.lnum;
18503 pos.col = (colnr_T)STRLEN(ml_get_curline());
18504 }
18505 return &pos;
18506 }
18507 return NULL;
18508}
18509
18510/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018511 * Convert list in "arg" into a position and optional file number.
18512 * When "fnump" is NULL there is no file number, only 3 items.
18513 * Note that the column is passed on as-is, the caller may want to decrement
18514 * it to use 1 for the first column.
18515 * Return FAIL when conversion is not possible, doesn't check the position for
18516 * validity.
18517 */
18518 static int
18519list2fpos(arg, posp, fnump)
18520 typval_T *arg;
18521 pos_T *posp;
18522 int *fnump;
18523{
18524 list_T *l = arg->vval.v_list;
18525 long i = 0;
18526 long n;
18527
Bram Moolenaarbde35262006-07-23 20:12:24 +000018528 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18529 * when "fnump" isn't NULL and "coladd" is optional. */
18530 if (arg->v_type != VAR_LIST
18531 || l == NULL
18532 || l->lv_len < (fnump == NULL ? 2 : 3)
18533 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018534 return FAIL;
18535
18536 if (fnump != NULL)
18537 {
18538 n = list_find_nr(l, i++, NULL); /* fnum */
18539 if (n < 0)
18540 return FAIL;
18541 if (n == 0)
18542 n = curbuf->b_fnum; /* current buffer */
18543 *fnump = n;
18544 }
18545
18546 n = list_find_nr(l, i++, NULL); /* lnum */
18547 if (n < 0)
18548 return FAIL;
18549 posp->lnum = n;
18550
18551 n = list_find_nr(l, i++, NULL); /* col */
18552 if (n < 0)
18553 return FAIL;
18554 posp->col = n;
18555
18556#ifdef FEAT_VIRTUALEDIT
18557 n = list_find_nr(l, i, NULL);
18558 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018559 posp->coladd = 0;
18560 else
18561 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018562#endif
18563
18564 return OK;
18565}
18566
18567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 * Get the length of an environment variable name.
18569 * Advance "arg" to the first character after the name.
18570 * Return 0 for error.
18571 */
18572 static int
18573get_env_len(arg)
18574 char_u **arg;
18575{
18576 char_u *p;
18577 int len;
18578
18579 for (p = *arg; vim_isIDc(*p); ++p)
18580 ;
18581 if (p == *arg) /* no name found */
18582 return 0;
18583
18584 len = (int)(p - *arg);
18585 *arg = p;
18586 return len;
18587}
18588
18589/*
18590 * Get the length of the name of a function or internal variable.
18591 * "arg" is advanced to the first non-white character after the name.
18592 * Return 0 if something is wrong.
18593 */
18594 static int
18595get_id_len(arg)
18596 char_u **arg;
18597{
18598 char_u *p;
18599 int len;
18600
18601 /* Find the end of the name. */
18602 for (p = *arg; eval_isnamec(*p); ++p)
18603 ;
18604 if (p == *arg) /* no name found */
18605 return 0;
18606
18607 len = (int)(p - *arg);
18608 *arg = skipwhite(p);
18609
18610 return len;
18611}
18612
18613/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018614 * Get the length of the name of a variable or function.
18615 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018617 * Return -1 if curly braces expansion failed.
18618 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 * If the name contains 'magic' {}'s, expand them and return the
18620 * expanded name in an allocated string via 'alias' - caller must free.
18621 */
18622 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018623get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 char_u **arg;
18625 char_u **alias;
18626 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018627 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628{
18629 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 char_u *p;
18631 char_u *expr_start;
18632 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633
18634 *alias = NULL; /* default to no alias */
18635
18636 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18637 && (*arg)[2] == (int)KE_SNR)
18638 {
18639 /* hard coded <SNR>, already translated */
18640 *arg += 3;
18641 return get_id_len(arg) + 3;
18642 }
18643 len = eval_fname_script(*arg);
18644 if (len > 0)
18645 {
18646 /* literal "<SID>", "s:" or "<SNR>" */
18647 *arg += len;
18648 }
18649
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018651 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018653 p = find_name_end(*arg, &expr_start, &expr_end,
18654 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 if (expr_start != NULL)
18656 {
18657 char_u *temp_string;
18658
18659 if (!evaluate)
18660 {
18661 len += (int)(p - *arg);
18662 *arg = skipwhite(p);
18663 return len;
18664 }
18665
18666 /*
18667 * Include any <SID> etc in the expanded string:
18668 * Thus the -len here.
18669 */
18670 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18671 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018672 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 *alias = temp_string;
18674 *arg = skipwhite(p);
18675 return (int)STRLEN(temp_string);
18676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677
18678 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018679 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 EMSG2(_(e_invexpr2), *arg);
18681
18682 return len;
18683}
18684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018685/*
18686 * Find the end of a variable or function name, taking care of magic braces.
18687 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18688 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018689 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 * Return a pointer to just after the name. Equal to "arg" if there is no
18691 * valid name.
18692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018694find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 char_u *arg;
18696 char_u **expr_start;
18697 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018698 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018700 int mb_nest = 0;
18701 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 char_u *p;
18703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 *expr_start = NULL;
18707 *expr_end = NULL;
18708 }
18709
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018710 /* Quick check for valid starting character. */
18711 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18712 return arg;
18713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714 for (p = arg; *p != NUL
18715 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018716 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018717 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018718 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018719 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018720 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018721 if (*p == '\'')
18722 {
18723 /* skip over 'string' to avoid counting [ and ] inside it. */
18724 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18725 ;
18726 if (*p == NUL)
18727 break;
18728 }
18729 else if (*p == '"')
18730 {
18731 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18732 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18733 if (*p == '\\' && p[1] != NUL)
18734 ++p;
18735 if (*p == NUL)
18736 break;
18737 }
18738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018741 if (*p == '[')
18742 ++br_nest;
18743 else if (*p == ']')
18744 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018749 if (*p == '{')
18750 {
18751 mb_nest++;
18752 if (expr_start != NULL && *expr_start == NULL)
18753 *expr_start = p;
18754 }
18755 else if (*p == '}')
18756 {
18757 mb_nest--;
18758 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18759 *expr_end = p;
18760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 }
18763
18764 return p;
18765}
18766
18767/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018768 * Expands out the 'magic' {}'s in a variable/function name.
18769 * Note that this can call itself recursively, to deal with
18770 * constructs like foo{bar}{baz}{bam}
18771 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18772 * "in_start" ^
18773 * "expr_start" ^
18774 * "expr_end" ^
18775 * "in_end" ^
18776 *
18777 * Returns a new allocated string, which the caller must free.
18778 * Returns NULL for failure.
18779 */
18780 static char_u *
18781make_expanded_name(in_start, expr_start, expr_end, in_end)
18782 char_u *in_start;
18783 char_u *expr_start;
18784 char_u *expr_end;
18785 char_u *in_end;
18786{
18787 char_u c1;
18788 char_u *retval = NULL;
18789 char_u *temp_result;
18790 char_u *nextcmd = NULL;
18791
18792 if (expr_end == NULL || in_end == NULL)
18793 return NULL;
18794 *expr_start = NUL;
18795 *expr_end = NUL;
18796 c1 = *in_end;
18797 *in_end = NUL;
18798
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018799 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018800 if (temp_result != NULL && nextcmd == NULL)
18801 {
18802 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18803 + (in_end - expr_end) + 1));
18804 if (retval != NULL)
18805 {
18806 STRCPY(retval, in_start);
18807 STRCAT(retval, temp_result);
18808 STRCAT(retval, expr_end + 1);
18809 }
18810 }
18811 vim_free(temp_result);
18812
18813 *in_end = c1; /* put char back for error messages */
18814 *expr_start = '{';
18815 *expr_end = '}';
18816
18817 if (retval != NULL)
18818 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018819 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018820 if (expr_start != NULL)
18821 {
18822 /* Further expansion! */
18823 temp_result = make_expanded_name(retval, expr_start,
18824 expr_end, temp_result);
18825 vim_free(retval);
18826 retval = temp_result;
18827 }
18828 }
18829
18830 return retval;
18831}
18832
18833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018835 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 */
18837 static int
18838eval_isnamec(c)
18839 int c;
18840{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018841 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18842}
18843
18844/*
18845 * Return TRUE if character "c" can be used as the first character in a
18846 * variable or function name (excluding '{' and '}').
18847 */
18848 static int
18849eval_isnamec1(c)
18850 int c;
18851{
18852 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853}
18854
18855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 * Set number v: variable to "val".
18857 */
18858 void
18859set_vim_var_nr(idx, val)
18860 int idx;
18861 long val;
18862{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018863 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864}
18865
18866/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018867 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 */
18869 long
18870get_vim_var_nr(idx)
18871 int idx;
18872{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018873 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874}
18875
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018876/*
18877 * Get string v: variable value. Uses a static buffer, can only be used once.
18878 */
18879 char_u *
18880get_vim_var_str(idx)
18881 int idx;
18882{
18883 return get_tv_string(&vimvars[idx].vv_tv);
18884}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018885
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018887 * Get List v: variable value. Caller must take care of reference count when
18888 * needed.
18889 */
18890 list_T *
18891get_vim_var_list(idx)
18892 int idx;
18893{
18894 return vimvars[idx].vv_list;
18895}
18896
18897/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018898 * Set v:char to character "c".
18899 */
18900 void
18901set_vim_var_char(c)
18902 int c;
18903{
18904#ifdef FEAT_MBYTE
18905 char_u buf[MB_MAXBYTES];
18906#else
18907 char_u buf[2];
18908#endif
18909
18910#ifdef FEAT_MBYTE
18911 if (has_mbyte)
18912 buf[(*mb_char2bytes)(c, buf)] = NUL;
18913 else
18914#endif
18915 {
18916 buf[0] = c;
18917 buf[1] = NUL;
18918 }
18919 set_vim_var_string(VV_CHAR, buf, -1);
18920}
18921
18922/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018923 * Set v:count to "count" and v:count1 to "count1".
18924 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 */
18926 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018927set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 long count;
18929 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018930 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018932 if (set_prevcount)
18933 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018934 vimvars[VV_COUNT].vv_nr = count;
18935 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936}
18937
18938/*
18939 * Set string v: variable to a copy of "val".
18940 */
18941 void
18942set_vim_var_string(idx, val, len)
18943 int idx;
18944 char_u *val;
18945 int len; /* length of "val" to use or -1 (whole string) */
18946{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018947 /* Need to do this (at least) once, since we can't initialize a union.
18948 * Will always be invoked when "v:progname" is set. */
18949 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18950
Bram Moolenaare9a41262005-01-15 22:18:47 +000018951 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018953 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018955 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018957 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958}
18959
18960/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018961 * Set List v: variable to "val".
18962 */
18963 void
18964set_vim_var_list(idx, val)
18965 int idx;
18966 list_T *val;
18967{
18968 list_unref(vimvars[idx].vv_list);
18969 vimvars[idx].vv_list = val;
18970 if (val != NULL)
18971 ++val->lv_refcount;
18972}
18973
18974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 * Set v:register if needed.
18976 */
18977 void
18978set_reg_var(c)
18979 int c;
18980{
18981 char_u regname;
18982
18983 if (c == 0 || c == ' ')
18984 regname = '"';
18985 else
18986 regname = c;
18987 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018988 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 set_vim_var_string(VV_REG, &regname, 1);
18990}
18991
18992/*
18993 * Get or set v:exception. If "oldval" == NULL, return the current value.
18994 * Otherwise, restore the value to "oldval" and return NULL.
18995 * Must always be called in pairs to save and restore v:exception! Does not
18996 * take care of memory allocations.
18997 */
18998 char_u *
18999v_exception(oldval)
19000 char_u *oldval;
19001{
19002 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019003 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004
Bram Moolenaare9a41262005-01-15 22:18:47 +000019005 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 return NULL;
19007}
19008
19009/*
19010 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19011 * Otherwise, restore the value to "oldval" and return NULL.
19012 * Must always be called in pairs to save and restore v:throwpoint! Does not
19013 * take care of memory allocations.
19014 */
19015 char_u *
19016v_throwpoint(oldval)
19017 char_u *oldval;
19018{
19019 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019020 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021
Bram Moolenaare9a41262005-01-15 22:18:47 +000019022 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 return NULL;
19024}
19025
19026#if defined(FEAT_AUTOCMD) || defined(PROTO)
19027/*
19028 * Set v:cmdarg.
19029 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19030 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19031 * Must always be called in pairs!
19032 */
19033 char_u *
19034set_cmdarg(eap, oldarg)
19035 exarg_T *eap;
19036 char_u *oldarg;
19037{
19038 char_u *oldval;
19039 char_u *newval;
19040 unsigned len;
19041
Bram Moolenaare9a41262005-01-15 22:18:47 +000019042 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019043 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019045 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019046 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019047 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 }
19049
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019050 if (eap->force_bin == FORCE_BIN)
19051 len = 6;
19052 else if (eap->force_bin == FORCE_NOBIN)
19053 len = 8;
19054 else
19055 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019056
19057 if (eap->read_edit)
19058 len += 7;
19059
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019060 if (eap->force_ff != 0)
19061 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19062# ifdef FEAT_MBYTE
19063 if (eap->force_enc != 0)
19064 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019065 if (eap->bad_char != 0)
19066 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019067# endif
19068
19069 newval = alloc(len + 1);
19070 if (newval == NULL)
19071 return NULL;
19072
19073 if (eap->force_bin == FORCE_BIN)
19074 sprintf((char *)newval, " ++bin");
19075 else if (eap->force_bin == FORCE_NOBIN)
19076 sprintf((char *)newval, " ++nobin");
19077 else
19078 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019079
19080 if (eap->read_edit)
19081 STRCAT(newval, " ++edit");
19082
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019083 if (eap->force_ff != 0)
19084 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19085 eap->cmd + eap->force_ff);
19086# ifdef FEAT_MBYTE
19087 if (eap->force_enc != 0)
19088 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19089 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019090 if (eap->bad_char == BAD_KEEP)
19091 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19092 else if (eap->bad_char == BAD_DROP)
19093 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19094 else if (eap->bad_char != 0)
19095 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019096# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019097 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019098 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099}
19100#endif
19101
19102/*
19103 * Get the value of internal variable "name".
19104 * Return OK or FAIL.
19105 */
19106 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019107get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 char_u *name;
19109 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019111 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112{
19113 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019114 typval_T *tv = NULL;
19115 typval_T atv;
19116 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118
19119 /* truncate the name, so that we can use strcmp() */
19120 cc = name[len];
19121 name[len] = NUL;
19122
19123 /*
19124 * Check for "b:changedtick".
19125 */
19126 if (STRCMP(name, "b:changedtick") == 0)
19127 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019128 atv.v_type = VAR_NUMBER;
19129 atv.vval.v_number = curbuf->b_changedtick;
19130 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 }
19132
19133 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 * Check for user-defined variables.
19135 */
19136 else
19137 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019138 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019140 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 }
19142
Bram Moolenaare9a41262005-01-15 22:18:47 +000019143 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019145 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019146 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 ret = FAIL;
19148 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019149 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019150 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151
19152 name[len] = cc;
19153
19154 return ret;
19155}
19156
19157/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019158 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19159 * Also handle function call with Funcref variable: func(expr)
19160 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19161 */
19162 static int
19163handle_subscript(arg, rettv, evaluate, verbose)
19164 char_u **arg;
19165 typval_T *rettv;
19166 int evaluate; /* do more than finding the end */
19167 int verbose; /* give error messages */
19168{
19169 int ret = OK;
19170 dict_T *selfdict = NULL;
19171 char_u *s;
19172 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019173 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019174
19175 while (ret == OK
19176 && (**arg == '['
19177 || (**arg == '.' && rettv->v_type == VAR_DICT)
19178 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19179 && !vim_iswhite(*(*arg - 1)))
19180 {
19181 if (**arg == '(')
19182 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019183 /* need to copy the funcref so that we can clear rettv */
19184 functv = *rettv;
19185 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019186
19187 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019188 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019189 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019190 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19191 &len, evaluate, selfdict);
19192
19193 /* Clear the funcref afterwards, so that deleting it while
19194 * evaluating the arguments is possible (see test55). */
19195 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019196
19197 /* Stop the expression evaluation when immediately aborting on
19198 * error, or when an interrupt occurred or an exception was thrown
19199 * but not caught. */
19200 if (aborting())
19201 {
19202 if (ret == OK)
19203 clear_tv(rettv);
19204 ret = FAIL;
19205 }
19206 dict_unref(selfdict);
19207 selfdict = NULL;
19208 }
19209 else /* **arg == '[' || **arg == '.' */
19210 {
19211 dict_unref(selfdict);
19212 if (rettv->v_type == VAR_DICT)
19213 {
19214 selfdict = rettv->vval.v_dict;
19215 if (selfdict != NULL)
19216 ++selfdict->dv_refcount;
19217 }
19218 else
19219 selfdict = NULL;
19220 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19221 {
19222 clear_tv(rettv);
19223 ret = FAIL;
19224 }
19225 }
19226 }
19227 dict_unref(selfdict);
19228 return ret;
19229}
19230
19231/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019232 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019233 * value).
19234 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019237{
Bram Moolenaar33570922005-01-25 22:26:29 +000019238 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019239}
19240
19241/*
19242 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 * The string "s" must have been allocated, it is consumed.
19244 * Return NULL for out of memory, the variable otherwise.
19245 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019246 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019247alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 char_u *s;
19249{
Bram Moolenaar33570922005-01-25 22:26:29 +000019250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 rettv = alloc_tv();
19253 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019255 rettv->v_type = VAR_STRING;
19256 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 }
19258 else
19259 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261}
19262
19263/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019264 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019266 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019267free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019268 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269{
19270 if (varp != NULL)
19271 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019272 switch (varp->v_type)
19273 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019274 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019275 func_unref(varp->vval.v_string);
19276 /*FALLTHROUGH*/
19277 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019278 vim_free(varp->vval.v_string);
19279 break;
19280 case VAR_LIST:
19281 list_unref(varp->vval.v_list);
19282 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019283 case VAR_DICT:
19284 dict_unref(varp->vval.v_dict);
19285 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019286 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019287#ifdef FEAT_FLOAT
19288 case VAR_FLOAT:
19289#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019290 case VAR_UNKNOWN:
19291 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019292 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019293 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019294 break;
19295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 vim_free(varp);
19297 }
19298}
19299
19300/*
19301 * Free the memory for a variable value and set the value to NULL or 0.
19302 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019303 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019304clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019305 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306{
19307 if (varp != NULL)
19308 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019309 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019311 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019312 func_unref(varp->vval.v_string);
19313 /*FALLTHROUGH*/
19314 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019315 vim_free(varp->vval.v_string);
19316 varp->vval.v_string = NULL;
19317 break;
19318 case VAR_LIST:
19319 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019320 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019321 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019322 case VAR_DICT:
19323 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019324 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019325 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019326 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019327 varp->vval.v_number = 0;
19328 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019329#ifdef FEAT_FLOAT
19330 case VAR_FLOAT:
19331 varp->vval.v_float = 0.0;
19332 break;
19333#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019334 case VAR_UNKNOWN:
19335 break;
19336 default:
19337 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019339 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 }
19341}
19342
19343/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019344 * Set the value of a variable to NULL without freeing items.
19345 */
19346 static void
19347init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019348 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019349{
19350 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019351 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019352}
19353
19354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 * Get the number value of a variable.
19356 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019357 * For incompatible types, return 0.
19358 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19359 * caller of incompatible types: it sets *denote to TRUE if "denote"
19360 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361 */
19362 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019363get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019364 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019366 int error = FALSE;
19367
19368 return get_tv_number_chk(varp, &error); /* return 0L on error */
19369}
19370
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019371 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019372get_tv_number_chk(varp, denote)
19373 typval_T *varp;
19374 int *denote;
19375{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019376 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019378 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019380 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019381 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019382#ifdef FEAT_FLOAT
19383 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019384 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019385 break;
19386#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019387 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019388 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019389 break;
19390 case VAR_STRING:
19391 if (varp->vval.v_string != NULL)
19392 vim_str2nr(varp->vval.v_string, NULL, NULL,
19393 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019394 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019395 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019396 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019397 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019398 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019399 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019400 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019401 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019402 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019405 if (denote == NULL) /* useful for values that must be unsigned */
19406 n = -1;
19407 else
19408 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019409 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410}
19411
19412/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019413 * Get the lnum from the first argument.
19414 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019415 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 */
19417 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019418get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019419 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420{
Bram Moolenaar33570922005-01-25 22:26:29 +000019421 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 linenr_T lnum;
19423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019424 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 if (lnum == 0) /* no valid number, try using line() */
19426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019427 rettv.v_type = VAR_NUMBER;
19428 f_line(argvars, &rettv);
19429 lnum = rettv.vval.v_number;
19430 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 }
19432 return lnum;
19433}
19434
19435/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019436 * Get the lnum from the first argument.
19437 * Also accepts "$", then "buf" is used.
19438 * Returns 0 on error.
19439 */
19440 static linenr_T
19441get_tv_lnum_buf(argvars, buf)
19442 typval_T *argvars;
19443 buf_T *buf;
19444{
19445 if (argvars[0].v_type == VAR_STRING
19446 && argvars[0].vval.v_string != NULL
19447 && argvars[0].vval.v_string[0] == '$'
19448 && buf != NULL)
19449 return buf->b_ml.ml_line_count;
19450 return get_tv_number_chk(&argvars[0], NULL);
19451}
19452
19453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 * Get the string value of a variable.
19455 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019456 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19457 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 * If the String variable has never been set, return an empty string.
19459 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019460 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19461 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 */
19463 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019464get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019465 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466{
19467 static char_u mybuf[NUMBUFLEN];
19468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019469 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470}
19471
19472 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019473get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019474 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019475 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019477 char_u *res = get_tv_string_buf_chk(varp, buf);
19478
19479 return res != NULL ? res : (char_u *)"";
19480}
19481
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019482 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019483get_tv_string_chk(varp)
19484 typval_T *varp;
19485{
19486 static char_u mybuf[NUMBUFLEN];
19487
19488 return get_tv_string_buf_chk(varp, mybuf);
19489}
19490
19491 static char_u *
19492get_tv_string_buf_chk(varp, buf)
19493 typval_T *varp;
19494 char_u *buf;
19495{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019496 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019498 case VAR_NUMBER:
19499 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19500 return buf;
19501 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019502 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019503 break;
19504 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019505 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019506 break;
19507 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019508 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019509 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019510#ifdef FEAT_FLOAT
19511 case VAR_FLOAT:
19512 EMSG(_("E806: using Float as a String"));
19513 break;
19514#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019515 case VAR_STRING:
19516 if (varp->vval.v_string != NULL)
19517 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019518 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019519 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019520 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019523 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524}
19525
19526/*
19527 * Find variable "name" in the list of variables.
19528 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019529 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019530 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019531 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019533 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019534find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019536 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019539 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540
Bram Moolenaara7043832005-01-21 11:56:39 +000019541 ht = find_var_ht(name, &varname);
19542 if (htp != NULL)
19543 *htp = ht;
19544 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019546 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547}
19548
19549/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019550 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019551 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019553 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019554find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019555 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019556 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019557 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019558{
Bram Moolenaar33570922005-01-25 22:26:29 +000019559 hashitem_T *hi;
19560
19561 if (*varname == NUL)
19562 {
19563 /* Must be something like "s:", otherwise "ht" would be NULL. */
19564 switch (varname[-2])
19565 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019566 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019567 case 'g': return &globvars_var;
19568 case 'v': return &vimvars_var;
19569 case 'b': return &curbuf->b_bufvar;
19570 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019571#ifdef FEAT_WINDOWS
19572 case 't': return &curtab->tp_winvar;
19573#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019574 case 'l': return current_funccal == NULL
19575 ? NULL : &current_funccal->l_vars_var;
19576 case 'a': return current_funccal == NULL
19577 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019578 }
19579 return NULL;
19580 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019581
19582 hi = hash_find(ht, varname);
19583 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019584 {
19585 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019586 * worked find the variable again. Don't auto-load a script if it was
19587 * loaded already, otherwise it would be loaded every time when
19588 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019589 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019590 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019591 hi = hash_find(ht, varname);
19592 if (HASHITEM_EMPTY(hi))
19593 return NULL;
19594 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019596}
19597
19598/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019600 * Set "varname" to the start of name without ':'.
19601 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019602 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019603find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 char_u *name;
19605 char_u **varname;
19606{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019607 hashitem_T *hi;
19608
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 if (name[1] != ':')
19610 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019611 /* The name must not start with a colon or #. */
19612 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 return NULL;
19614 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019615
19616 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019617 hi = hash_find(&compat_hashtab, name);
19618 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019619 return &compat_hashtab;
19620
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 return &globvarht; /* global variable */
19623 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 }
19625 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019626 if (*name == 'g') /* global variable */
19627 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019628 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19629 */
19630 if (vim_strchr(name + 2, ':') != NULL
19631 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019632 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019634 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019637#ifdef FEAT_WINDOWS
19638 if (*name == 't') /* tab page variable */
19639 return &curtab->tp_vars.dv_hashtab;
19640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 if (*name == 'v') /* v: variable */
19642 return &vimvarht;
19643 if (*name == 'a' && current_funccal != NULL) /* function argument */
19644 return &current_funccal->l_avars.dv_hashtab;
19645 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19646 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 if (*name == 's' /* script variable */
19648 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19649 return &SCRIPT_VARS(current_SID);
19650 return NULL;
19651}
19652
19653/*
19654 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019655 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 * Returns NULL when it doesn't exist.
19657 */
19658 char_u *
19659get_var_value(name)
19660 char_u *name;
19661{
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663
Bram Moolenaara7043832005-01-21 11:56:39 +000019664 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 if (v == NULL)
19666 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019667 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668}
19669
19670/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 * sourcing this script and when executing functions defined in the script.
19673 */
19674 void
19675new_script_vars(id)
19676 scid_T id;
19677{
Bram Moolenaara7043832005-01-21 11:56:39 +000019678 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 hashtab_T *ht;
19680 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019681
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19683 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019684 /* Re-allocating ga_data means that an ht_array pointing to
19685 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019686 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019687 for (i = 1; i <= ga_scripts.ga_len; ++i)
19688 {
19689 ht = &SCRIPT_VARS(i);
19690 if (ht->ht_mask == HT_INIT_SIZE - 1)
19691 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019692 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019693 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019694 }
19695
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 while (ga_scripts.ga_len < id)
19697 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019698 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019699 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019700 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 }
19703 }
19704}
19705
19706/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019707 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19708 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 */
19710 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019711init_var_dict(dict, dict_var)
19712 dict_T *dict;
19713 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714{
Bram Moolenaar33570922005-01-25 22:26:29 +000019715 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019716 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019717 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019718 dict_var->di_tv.vval.v_dict = dict;
19719 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019720 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19722 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723}
19724
19725/*
19726 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 * Frees all allocated variables and the value they contain.
19728 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 */
19730 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019731vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 hashtab_T *ht;
19733{
19734 vars_clear_ext(ht, TRUE);
19735}
19736
19737/*
19738 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19739 */
19740 static void
19741vars_clear_ext(ht, free_val)
19742 hashtab_T *ht;
19743 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744{
Bram Moolenaara7043832005-01-21 11:56:39 +000019745 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 hashitem_T *hi;
19747 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748
Bram Moolenaar33570922005-01-25 22:26:29 +000019749 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019750 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019751 for (hi = ht->ht_array; todo > 0; ++hi)
19752 {
19753 if (!HASHITEM_EMPTY(hi))
19754 {
19755 --todo;
19756
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019758 * ht_array might change then. hash_clear() takes care of it
19759 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019760 v = HI2DI(hi);
19761 if (free_val)
19762 clear_tv(&v->di_tv);
19763 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19764 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019765 }
19766 }
19767 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019768 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769}
19770
Bram Moolenaara7043832005-01-21 11:56:39 +000019771/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019772 * Delete a variable from hashtab "ht" at item "hi".
19773 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019776delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019777 hashtab_T *ht;
19778 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779{
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019781
19782 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 clear_tv(&di->di_tv);
19784 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785}
19786
19787/*
19788 * List the value of one internal variable.
19789 */
19790 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019791list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019792 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019794 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019796 char_u *tofree;
19797 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019798 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019799
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019800 current_copyID += COPYID_INC;
19801 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019802 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019803 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019804 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805}
19806
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019808list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 char_u *prefix;
19810 char_u *name;
19811 int type;
19812 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019813 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814{
Bram Moolenaar31859182007-08-14 20:41:13 +000019815 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19816 msg_start();
19817 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 if (name != NULL) /* "a:" vars don't have a name stored */
19819 msg_puts(name);
19820 msg_putchar(' ');
19821 msg_advance(22);
19822 if (type == VAR_NUMBER)
19823 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019824 else if (type == VAR_FUNC)
19825 msg_putchar('*');
19826 else if (type == VAR_LIST)
19827 {
19828 msg_putchar('[');
19829 if (*string == '[')
19830 ++string;
19831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019832 else if (type == VAR_DICT)
19833 {
19834 msg_putchar('{');
19835 if (*string == '{')
19836 ++string;
19837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 else
19839 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019840
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019842
19843 if (type == VAR_FUNC)
19844 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019845 if (*first)
19846 {
19847 msg_clr_eos();
19848 *first = FALSE;
19849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850}
19851
19852/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019853 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 * If the variable already exists, the value is updated.
19855 * Otherwise the variable is created.
19856 */
19857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019858set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019860 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019861 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862{
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019865 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019867 ht = find_var_ht(name, &varname);
19868 if (ht == NULL || *varname == NUL)
19869 {
19870 EMSG2(_(e_illvar), name);
19871 return;
19872 }
19873 v = find_var_in_ht(ht, varname, TRUE);
19874
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019875 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19876 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019877
Bram Moolenaar33570922005-01-25 22:26:29 +000019878 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019880 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019881 if (var_check_ro(v->di_flags, name)
19882 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019883 return;
19884 if (v->di_tv.v_type != tv->v_type
19885 && !((v->di_tv.v_type == VAR_STRING
19886 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019887 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019888 || tv->v_type == VAR_NUMBER))
19889#ifdef FEAT_FLOAT
19890 && !((v->di_tv.v_type == VAR_NUMBER
19891 || v->di_tv.v_type == VAR_FLOAT)
19892 && (tv->v_type == VAR_NUMBER
19893 || tv->v_type == VAR_FLOAT))
19894#endif
19895 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019896 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019897 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019898 return;
19899 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019900
19901 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019902 * Handle setting internal v: variables separately: we don't change
19903 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019904 */
19905 if (ht == &vimvarht)
19906 {
19907 if (v->di_tv.v_type == VAR_STRING)
19908 {
19909 vim_free(v->di_tv.vval.v_string);
19910 if (copy || tv->v_type != VAR_STRING)
19911 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19912 else
19913 {
19914 /* Take over the string to avoid an extra alloc/free. */
19915 v->di_tv.vval.v_string = tv->vval.v_string;
19916 tv->vval.v_string = NULL;
19917 }
19918 }
19919 else if (v->di_tv.v_type != VAR_NUMBER)
19920 EMSG2(_(e_intern2), "set_var()");
19921 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019922 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019923 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019924 if (STRCMP(varname, "searchforward") == 0)
19925 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19926 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 return;
19928 }
19929
19930 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 }
19932 else /* add a new variable */
19933 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019934 /* Can't add "v:" variable. */
19935 if (ht == &vimvarht)
19936 {
19937 EMSG2(_(e_illvar), name);
19938 return;
19939 }
19940
Bram Moolenaar92124a32005-06-17 22:03:40 +000019941 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019942 if (!valid_varname(varname))
19943 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019944
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019945 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19946 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019947 if (v == NULL)
19948 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019949 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019950 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019952 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 return;
19954 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019955 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019957
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019958 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019959 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019960 else
19961 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019962 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019963 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966}
19967
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019968/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019969 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019970 * Also give an error message.
19971 */
19972 static int
19973var_check_ro(flags, name)
19974 int flags;
19975 char_u *name;
19976{
19977 if (flags & DI_FLAGS_RO)
19978 {
19979 EMSG2(_(e_readonlyvar), name);
19980 return TRUE;
19981 }
19982 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19983 {
19984 EMSG2(_(e_readonlysbx), name);
19985 return TRUE;
19986 }
19987 return FALSE;
19988}
19989
19990/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019991 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19992 * Also give an error message.
19993 */
19994 static int
19995var_check_fixed(flags, name)
19996 int flags;
19997 char_u *name;
19998{
19999 if (flags & DI_FLAGS_FIX)
20000 {
20001 EMSG2(_("E795: Cannot delete variable %s"), name);
20002 return TRUE;
20003 }
20004 return FALSE;
20005}
20006
20007/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020008 * Check if a funcref is assigned to a valid variable name.
20009 * Return TRUE and give an error if not.
20010 */
20011 static int
20012var_check_func_name(name, new_var)
20013 char_u *name; /* points to start of variable name */
20014 int new_var; /* TRUE when creating the variable */
20015{
20016 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20017 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20018 ? name[2] : name[0]))
20019 {
20020 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20021 name);
20022 return TRUE;
20023 }
20024 /* Don't allow hiding a function. When "v" is not NULL we might be
20025 * assigning another function to the same var, the type is checked
20026 * below. */
20027 if (new_var && function_exists(name))
20028 {
20029 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20030 name);
20031 return TRUE;
20032 }
20033 return FALSE;
20034}
20035
20036/*
20037 * Check if a variable name is valid.
20038 * Return FALSE and give an error if not.
20039 */
20040 static int
20041valid_varname(varname)
20042 char_u *varname;
20043{
20044 char_u *p;
20045
20046 for (p = varname; *p != NUL; ++p)
20047 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20048 && *p != AUTOLOAD_CHAR)
20049 {
20050 EMSG2(_(e_illvar), varname);
20051 return FALSE;
20052 }
20053 return TRUE;
20054}
20055
20056/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020057 * Return TRUE if typeval "tv" is set to be locked (immutable).
20058 * Also give an error message, using "name".
20059 */
20060 static int
20061tv_check_lock(lock, name)
20062 int lock;
20063 char_u *name;
20064{
20065 if (lock & VAR_LOCKED)
20066 {
20067 EMSG2(_("E741: Value is locked: %s"),
20068 name == NULL ? (char_u *)_("Unknown") : name);
20069 return TRUE;
20070 }
20071 if (lock & VAR_FIXED)
20072 {
20073 EMSG2(_("E742: Cannot change value of %s"),
20074 name == NULL ? (char_u *)_("Unknown") : name);
20075 return TRUE;
20076 }
20077 return FALSE;
20078}
20079
20080/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020081 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020082 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020083 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020084 * It is OK for "from" and "to" to point to the same item. This is used to
20085 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020086 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020087 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020089 typval_T *from;
20090 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020092 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020093 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 switch (from->v_type)
20095 {
20096 case VAR_NUMBER:
20097 to->vval.v_number = from->vval.v_number;
20098 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020099#ifdef FEAT_FLOAT
20100 case VAR_FLOAT:
20101 to->vval.v_float = from->vval.v_float;
20102 break;
20103#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020104 case VAR_STRING:
20105 case VAR_FUNC:
20106 if (from->vval.v_string == NULL)
20107 to->vval.v_string = NULL;
20108 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020109 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020110 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020111 if (from->v_type == VAR_FUNC)
20112 func_ref(to->vval.v_string);
20113 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020114 break;
20115 case VAR_LIST:
20116 if (from->vval.v_list == NULL)
20117 to->vval.v_list = NULL;
20118 else
20119 {
20120 to->vval.v_list = from->vval.v_list;
20121 ++to->vval.v_list->lv_refcount;
20122 }
20123 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020124 case VAR_DICT:
20125 if (from->vval.v_dict == NULL)
20126 to->vval.v_dict = NULL;
20127 else
20128 {
20129 to->vval.v_dict = from->vval.v_dict;
20130 ++to->vval.v_dict->dv_refcount;
20131 }
20132 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020133 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020134 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020135 break;
20136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137}
20138
20139/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020140 * Make a copy of an item.
20141 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020142 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20143 * reference to an already copied list/dict can be used.
20144 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020145 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020146 static int
20147item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 typval_T *from;
20149 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020150 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020151 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020152{
20153 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020154 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020155
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020157 {
20158 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020159 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020160 }
20161 ++recurse;
20162
20163 switch (from->v_type)
20164 {
20165 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020166#ifdef FEAT_FLOAT
20167 case VAR_FLOAT:
20168#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020169 case VAR_STRING:
20170 case VAR_FUNC:
20171 copy_tv(from, to);
20172 break;
20173 case VAR_LIST:
20174 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020175 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020176 if (from->vval.v_list == NULL)
20177 to->vval.v_list = NULL;
20178 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20179 {
20180 /* use the copy made earlier */
20181 to->vval.v_list = from->vval.v_list->lv_copylist;
20182 ++to->vval.v_list->lv_refcount;
20183 }
20184 else
20185 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20186 if (to->vval.v_list == NULL)
20187 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020188 break;
20189 case VAR_DICT:
20190 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020191 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020192 if (from->vval.v_dict == NULL)
20193 to->vval.v_dict = NULL;
20194 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20195 {
20196 /* use the copy made earlier */
20197 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20198 ++to->vval.v_dict->dv_refcount;
20199 }
20200 else
20201 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20202 if (to->vval.v_dict == NULL)
20203 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020204 break;
20205 default:
20206 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020207 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020208 }
20209 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020210 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020211}
20212
20213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 * ":echo expr1 ..." print each argument separated with a space, add a
20215 * newline at the end.
20216 * ":echon expr1 ..." print each argument plain.
20217 */
20218 void
20219ex_echo(eap)
20220 exarg_T *eap;
20221{
20222 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020224 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 char_u *p;
20226 int needclr = TRUE;
20227 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020228 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229
20230 if (eap->skip)
20231 ++emsg_skip;
20232 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20233 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020234 /* If eval1() causes an error message the text from the command may
20235 * still need to be cleared. E.g., "echo 22,44". */
20236 need_clr_eos = needclr;
20237
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020239 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 {
20241 /*
20242 * Report the invalid expression unless the expression evaluation
20243 * has been cancelled due to an aborting error, an interrupt, or an
20244 * exception.
20245 */
20246 if (!aborting())
20247 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020248 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 break;
20250 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020251 need_clr_eos = FALSE;
20252
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 if (!eap->skip)
20254 {
20255 if (atstart)
20256 {
20257 atstart = FALSE;
20258 /* Call msg_start() after eval1(), evaluating the expression
20259 * may cause a message to appear. */
20260 if (eap->cmdidx == CMD_echo)
20261 msg_start();
20262 }
20263 else if (eap->cmdidx == CMD_echo)
20264 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020265 current_copyID += COPYID_INC;
20266 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020267 if (p != NULL)
20268 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020270 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020272 if (*p != TAB && needclr)
20273 {
20274 /* remove any text still there from the command */
20275 msg_clr_eos();
20276 needclr = FALSE;
20277 }
20278 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 }
20280 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020281 {
20282#ifdef FEAT_MBYTE
20283 if (has_mbyte)
20284 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020285 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020286
20287 (void)msg_outtrans_len_attr(p, i, echo_attr);
20288 p += i - 1;
20289 }
20290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020292 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020295 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020297 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 arg = skipwhite(arg);
20299 }
20300 eap->nextcmd = check_nextcmd(arg);
20301
20302 if (eap->skip)
20303 --emsg_skip;
20304 else
20305 {
20306 /* remove text that may still be there from the command */
20307 if (needclr)
20308 msg_clr_eos();
20309 if (eap->cmdidx == CMD_echo)
20310 msg_end();
20311 }
20312}
20313
20314/*
20315 * ":echohl {name}".
20316 */
20317 void
20318ex_echohl(eap)
20319 exarg_T *eap;
20320{
20321 int id;
20322
20323 id = syn_name2id(eap->arg);
20324 if (id == 0)
20325 echo_attr = 0;
20326 else
20327 echo_attr = syn_id2attr(id);
20328}
20329
20330/*
20331 * ":execute expr1 ..." execute the result of an expression.
20332 * ":echomsg expr1 ..." Print a message
20333 * ":echoerr expr1 ..." Print an error
20334 * Each gets spaces around each argument and a newline at the end for
20335 * echo commands
20336 */
20337 void
20338ex_execute(eap)
20339 exarg_T *eap;
20340{
20341 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020342 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 int ret = OK;
20344 char_u *p;
20345 garray_T ga;
20346 int len;
20347 int save_did_emsg;
20348
20349 ga_init2(&ga, 1, 80);
20350
20351 if (eap->skip)
20352 ++emsg_skip;
20353 while (*arg != NUL && *arg != '|' && *arg != '\n')
20354 {
20355 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 {
20358 /*
20359 * Report the invalid expression unless the expression evaluation
20360 * has been cancelled due to an aborting error, an interrupt, or an
20361 * exception.
20362 */
20363 if (!aborting())
20364 EMSG2(_(e_invexpr2), p);
20365 ret = FAIL;
20366 break;
20367 }
20368
20369 if (!eap->skip)
20370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020371 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 len = (int)STRLEN(p);
20373 if (ga_grow(&ga, len + 2) == FAIL)
20374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020375 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 ret = FAIL;
20377 break;
20378 }
20379 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 ga.ga_len += len;
20383 }
20384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020385 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 arg = skipwhite(arg);
20387 }
20388
20389 if (ret != FAIL && ga.ga_data != NULL)
20390 {
20391 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020394 out_flush();
20395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 else if (eap->cmdidx == CMD_echoerr)
20397 {
20398 /* We don't want to abort following commands, restore did_emsg. */
20399 save_did_emsg = did_emsg;
20400 EMSG((char_u *)ga.ga_data);
20401 if (!force_abort)
20402 did_emsg = save_did_emsg;
20403 }
20404 else if (eap->cmdidx == CMD_execute)
20405 do_cmdline((char_u *)ga.ga_data,
20406 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20407 }
20408
20409 ga_clear(&ga);
20410
20411 if (eap->skip)
20412 --emsg_skip;
20413
20414 eap->nextcmd = check_nextcmd(arg);
20415}
20416
20417/*
20418 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20419 * "arg" points to the "&" or '+' when called, to "option" when returning.
20420 * Returns NULL when no option name found. Otherwise pointer to the char
20421 * after the option name.
20422 */
20423 static char_u *
20424find_option_end(arg, opt_flags)
20425 char_u **arg;
20426 int *opt_flags;
20427{
20428 char_u *p = *arg;
20429
20430 ++p;
20431 if (*p == 'g' && p[1] == ':')
20432 {
20433 *opt_flags = OPT_GLOBAL;
20434 p += 2;
20435 }
20436 else if (*p == 'l' && p[1] == ':')
20437 {
20438 *opt_flags = OPT_LOCAL;
20439 p += 2;
20440 }
20441 else
20442 *opt_flags = 0;
20443
20444 if (!ASCII_ISALPHA(*p))
20445 return NULL;
20446 *arg = p;
20447
20448 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20449 p += 4; /* termcap option */
20450 else
20451 while (ASCII_ISALPHA(*p))
20452 ++p;
20453 return p;
20454}
20455
20456/*
20457 * ":function"
20458 */
20459 void
20460ex_function(eap)
20461 exarg_T *eap;
20462{
20463 char_u *theline;
20464 int j;
20465 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 char_u *name = NULL;
20468 char_u *p;
20469 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020470 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 garray_T newargs;
20472 garray_T newlines;
20473 int varargs = FALSE;
20474 int mustend = FALSE;
20475 int flags = 0;
20476 ufunc_T *fp;
20477 int indent;
20478 int nesting;
20479 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020480 dictitem_T *v;
20481 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020482 static int func_nr = 0; /* number for nameless function */
20483 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020484 hashtab_T *ht;
20485 int todo;
20486 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020487 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488
20489 /*
20490 * ":function" without argument: list functions.
20491 */
20492 if (ends_excmd(*eap->arg))
20493 {
20494 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020495 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020496 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020497 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020498 {
20499 if (!HASHITEM_EMPTY(hi))
20500 {
20501 --todo;
20502 fp = HI2UF(hi);
20503 if (!isdigit(*fp->uf_name))
20504 list_func_head(fp, FALSE);
20505 }
20506 }
20507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 eap->nextcmd = check_nextcmd(eap->arg);
20509 return;
20510 }
20511
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020512 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020513 * ":function /pat": list functions matching pattern.
20514 */
20515 if (*eap->arg == '/')
20516 {
20517 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20518 if (!eap->skip)
20519 {
20520 regmatch_T regmatch;
20521
20522 c = *p;
20523 *p = NUL;
20524 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20525 *p = c;
20526 if (regmatch.regprog != NULL)
20527 {
20528 regmatch.rm_ic = p_ic;
20529
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020530 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020531 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20532 {
20533 if (!HASHITEM_EMPTY(hi))
20534 {
20535 --todo;
20536 fp = HI2UF(hi);
20537 if (!isdigit(*fp->uf_name)
20538 && vim_regexec(&regmatch, fp->uf_name, 0))
20539 list_func_head(fp, FALSE);
20540 }
20541 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020542 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020543 }
20544 }
20545 if (*p == '/')
20546 ++p;
20547 eap->nextcmd = check_nextcmd(p);
20548 return;
20549 }
20550
20551 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020552 * Get the function name. There are these situations:
20553 * func normal function name
20554 * "name" == func, "fudi.fd_dict" == NULL
20555 * dict.func new dictionary entry
20556 * "name" == NULL, "fudi.fd_dict" set,
20557 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20558 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020559 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020560 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20561 * dict.func existing dict entry that's not a Funcref
20562 * "name" == NULL, "fudi.fd_dict" set,
20563 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020566 name = trans_function_name(&p, eap->skip, 0, &fudi);
20567 paren = (vim_strchr(p, '(') != NULL);
20568 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 {
20570 /*
20571 * Return on an invalid expression in braces, unless the expression
20572 * evaluation has been cancelled due to an aborting error, an
20573 * interrupt, or an exception.
20574 */
20575 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020577 if (!eap->skip && fudi.fd_newkey != NULL)
20578 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020579 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 else
20583 eap->skip = TRUE;
20584 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020585
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 /* An error in a function call during evaluation of an expression in magic
20587 * braces should not cause the function not to be defined. */
20588 saved_did_emsg = did_emsg;
20589 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590
20591 /*
20592 * ":function func" with only function name: list function.
20593 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020594 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 {
20596 if (!ends_excmd(*skipwhite(p)))
20597 {
20598 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020599 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 }
20601 eap->nextcmd = check_nextcmd(p);
20602 if (eap->nextcmd != NULL)
20603 *p = NUL;
20604 if (!eap->skip && !got_int)
20605 {
20606 fp = find_func(name);
20607 if (fp != NULL)
20608 {
20609 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020610 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020612 if (FUNCLINE(fp, j) == NULL)
20613 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 msg_putchar('\n');
20615 msg_outnum((long)(j + 1));
20616 if (j < 9)
20617 msg_putchar(' ');
20618 if (j < 99)
20619 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020620 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 out_flush(); /* show a line at a time */
20622 ui_breakcheck();
20623 }
20624 if (!got_int)
20625 {
20626 msg_putchar('\n');
20627 msg_puts((char_u *)" endfunction");
20628 }
20629 }
20630 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020631 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020633 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 }
20635
20636 /*
20637 * ":function name(arg1, arg2)" Define function.
20638 */
20639 p = skipwhite(p);
20640 if (*p != '(')
20641 {
20642 if (!eap->skip)
20643 {
20644 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020645 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 }
20647 /* attempt to continue by skipping some text */
20648 if (vim_strchr(p, '(') != NULL)
20649 p = vim_strchr(p, '(');
20650 }
20651 p = skipwhite(p + 1);
20652
20653 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20654 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20655
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020656 if (!eap->skip)
20657 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020658 /* Check the name of the function. Unless it's a dictionary function
20659 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020660 if (name != NULL)
20661 arg = name;
20662 else
20663 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020664 if (arg != NULL && (fudi.fd_di == NULL
20665 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020666 {
20667 if (*arg == K_SPECIAL)
20668 j = 3;
20669 else
20670 j = 0;
20671 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20672 : eval_isnamec(arg[j])))
20673 ++j;
20674 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020675 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020676 }
20677 }
20678
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 /*
20680 * Isolate the arguments: "arg1, arg2, ...)"
20681 */
20682 while (*p != ')')
20683 {
20684 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20685 {
20686 varargs = TRUE;
20687 p += 3;
20688 mustend = TRUE;
20689 }
20690 else
20691 {
20692 arg = p;
20693 while (ASCII_ISALNUM(*p) || *p == '_')
20694 ++p;
20695 if (arg == p || isdigit(*arg)
20696 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20697 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20698 {
20699 if (!eap->skip)
20700 EMSG2(_("E125: Illegal argument: %s"), arg);
20701 break;
20702 }
20703 if (ga_grow(&newargs, 1) == FAIL)
20704 goto erret;
20705 c = *p;
20706 *p = NUL;
20707 arg = vim_strsave(arg);
20708 if (arg == NULL)
20709 goto erret;
20710 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20711 *p = c;
20712 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 if (*p == ',')
20714 ++p;
20715 else
20716 mustend = TRUE;
20717 }
20718 p = skipwhite(p);
20719 if (mustend && *p != ')')
20720 {
20721 if (!eap->skip)
20722 EMSG2(_(e_invarg2), eap->arg);
20723 break;
20724 }
20725 }
20726 ++p; /* skip the ')' */
20727
Bram Moolenaare9a41262005-01-15 22:18:47 +000020728 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 for (;;)
20730 {
20731 p = skipwhite(p);
20732 if (STRNCMP(p, "range", 5) == 0)
20733 {
20734 flags |= FC_RANGE;
20735 p += 5;
20736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020737 else if (STRNCMP(p, "dict", 4) == 0)
20738 {
20739 flags |= FC_DICT;
20740 p += 4;
20741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742 else if (STRNCMP(p, "abort", 5) == 0)
20743 {
20744 flags |= FC_ABORT;
20745 p += 5;
20746 }
20747 else
20748 break;
20749 }
20750
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020751 /* When there is a line break use what follows for the function body.
20752 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20753 if (*p == '\n')
20754 line_arg = p + 1;
20755 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 EMSG(_(e_trailing));
20757
20758 /*
20759 * Read the body of the function, until ":endfunction" is found.
20760 */
20761 if (KeyTyped)
20762 {
20763 /* Check if the function already exists, don't let the user type the
20764 * whole function before telling him it doesn't work! For a script we
20765 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020766 if (!eap->skip && !eap->forceit)
20767 {
20768 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20769 EMSG(_(e_funcdict));
20770 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020771 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020774 if (!eap->skip && did_emsg)
20775 goto erret;
20776
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 msg_putchar('\n'); /* don't overwrite the function name */
20778 cmdline_row = msg_row;
20779 }
20780
20781 indent = 2;
20782 nesting = 0;
20783 for (;;)
20784 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020020785 if (KeyTyped)
20786 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020788 sourcing_lnum_off = sourcing_lnum;
20789
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020790 if (line_arg != NULL)
20791 {
20792 /* Use eap->arg, split up in parts by line breaks. */
20793 theline = line_arg;
20794 p = vim_strchr(theline, '\n');
20795 if (p == NULL)
20796 line_arg += STRLEN(line_arg);
20797 else
20798 {
20799 *p = NUL;
20800 line_arg = p + 1;
20801 }
20802 }
20803 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 theline = getcmdline(':', 0L, indent);
20805 else
20806 theline = eap->getline(':', eap->cookie, indent);
20807 if (KeyTyped)
20808 lines_left = Rows - 1;
20809 if (theline == NULL)
20810 {
20811 EMSG(_("E126: Missing :endfunction"));
20812 goto erret;
20813 }
20814
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020815 /* Detect line continuation: sourcing_lnum increased more than one. */
20816 if (sourcing_lnum > sourcing_lnum_off + 1)
20817 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20818 else
20819 sourcing_lnum_off = 0;
20820
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 if (skip_until != NULL)
20822 {
20823 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20824 * don't check for ":endfunc". */
20825 if (STRCMP(theline, skip_until) == 0)
20826 {
20827 vim_free(skip_until);
20828 skip_until = NULL;
20829 }
20830 }
20831 else
20832 {
20833 /* skip ':' and blanks*/
20834 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20835 ;
20836
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020837 /* Check for "endfunction". */
20838 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020840 if (line_arg == NULL)
20841 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 break;
20843 }
20844
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020845 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 * at "end". */
20847 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20848 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020849 else if (STRNCMP(p, "if", 2) == 0
20850 || STRNCMP(p, "wh", 2) == 0
20851 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 || STRNCMP(p, "try", 3) == 0)
20853 indent += 2;
20854
20855 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020856 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020858 if (*p == '!')
20859 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 p += eval_fname_script(p);
20861 if (ASCII_ISALPHA(*p))
20862 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020863 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 if (*skipwhite(p) == '(')
20865 {
20866 ++nesting;
20867 indent += 2;
20868 }
20869 }
20870 }
20871
20872 /* Check for ":append" or ":insert". */
20873 p = skip_range(p, NULL);
20874 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20875 || (p[0] == 'i'
20876 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20877 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20878 skip_until = vim_strsave((char_u *)".");
20879
20880 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20881 arg = skipwhite(skiptowhite(p));
20882 if (arg[0] == '<' && arg[1] =='<'
20883 && ((p[0] == 'p' && p[1] == 'y'
20884 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20885 || (p[0] == 'p' && p[1] == 'e'
20886 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20887 || (p[0] == 't' && p[1] == 'c'
20888 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20889 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20890 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020891 || (p[0] == 'm' && p[1] == 'z'
20892 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 ))
20894 {
20895 /* ":python <<" continues until a dot, like ":append" */
20896 p = skipwhite(arg + 2);
20897 if (*p == NUL)
20898 skip_until = vim_strsave((char_u *)".");
20899 else
20900 skip_until = vim_strsave(p);
20901 }
20902 }
20903
20904 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020905 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020906 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020907 if (line_arg == NULL)
20908 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020910 }
20911
20912 /* Copy the line to newly allocated memory. get_one_sourceline()
20913 * allocates 250 bytes per line, this saves 80% on average. The cost
20914 * is an extra alloc/free. */
20915 p = vim_strsave(theline);
20916 if (p != NULL)
20917 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020918 if (line_arg == NULL)
20919 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020920 theline = p;
20921 }
20922
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020923 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20924
20925 /* Add NULL lines for continuation lines, so that the line count is
20926 * equal to the index in the growarray. */
20927 while (sourcing_lnum_off-- > 0)
20928 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020929
20930 /* Check for end of eap->arg. */
20931 if (line_arg != NULL && *line_arg == NUL)
20932 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 }
20934
20935 /* Don't define the function when skipping commands or when an error was
20936 * detected. */
20937 if (eap->skip || did_emsg)
20938 goto erret;
20939
20940 /*
20941 * If there are no errors, add the function
20942 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020943 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020944 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020945 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020946 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020947 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020948 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020949 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020950 goto erret;
20951 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020952
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020953 fp = find_func(name);
20954 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020956 if (!eap->forceit)
20957 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020958 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020959 goto erret;
20960 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020961 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020962 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020963 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020964 name);
20965 goto erret;
20966 }
20967 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020968 ga_clear_strings(&(fp->uf_args));
20969 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020970 vim_free(name);
20971 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 }
20974 else
20975 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020976 char numbuf[20];
20977
20978 fp = NULL;
20979 if (fudi.fd_newkey == NULL && !eap->forceit)
20980 {
20981 EMSG(_(e_funcdict));
20982 goto erret;
20983 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020984 if (fudi.fd_di == NULL)
20985 {
20986 /* Can't add a function to a locked dictionary */
20987 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20988 goto erret;
20989 }
20990 /* Can't change an existing function if it is locked */
20991 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20992 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020993
20994 /* Give the function a sequential number. Can only be used with a
20995 * Funcref! */
20996 vim_free(name);
20997 sprintf(numbuf, "%d", ++func_nr);
20998 name = vim_strsave((char_u *)numbuf);
20999 if (name == NULL)
21000 goto erret;
21001 }
21002
21003 if (fp == NULL)
21004 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021005 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021006 {
21007 int slen, plen;
21008 char_u *scriptname;
21009
21010 /* Check that the autoload name matches the script name. */
21011 j = FAIL;
21012 if (sourcing_name != NULL)
21013 {
21014 scriptname = autoload_name(name);
21015 if (scriptname != NULL)
21016 {
21017 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021018 plen = (int)STRLEN(p);
21019 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021020 if (slen > plen && fnamecmp(p,
21021 sourcing_name + slen - plen) == 0)
21022 j = OK;
21023 vim_free(scriptname);
21024 }
21025 }
21026 if (j == FAIL)
21027 {
21028 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21029 goto erret;
21030 }
21031 }
21032
21033 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 if (fp == NULL)
21035 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021036
21037 if (fudi.fd_dict != NULL)
21038 {
21039 if (fudi.fd_di == NULL)
21040 {
21041 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021042 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021043 if (fudi.fd_di == NULL)
21044 {
21045 vim_free(fp);
21046 goto erret;
21047 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021048 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21049 {
21050 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021051 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021052 goto erret;
21053 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021054 }
21055 else
21056 /* overwrite existing dict entry */
21057 clear_tv(&fudi.fd_di->di_tv);
21058 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021059 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021060 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021061 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021062
21063 /* behave like "dict" was used */
21064 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021065 }
21066
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021068 STRCPY(fp->uf_name, name);
21069 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021071 fp->uf_args = newargs;
21072 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021073#ifdef FEAT_PROFILE
21074 fp->uf_tml_count = NULL;
21075 fp->uf_tml_total = NULL;
21076 fp->uf_tml_self = NULL;
21077 fp->uf_profiling = FALSE;
21078 if (prof_def_func())
21079 func_do_profile(fp);
21080#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021081 fp->uf_varargs = varargs;
21082 fp->uf_flags = flags;
21083 fp->uf_calls = 0;
21084 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021085 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086
21087erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 ga_clear_strings(&newargs);
21089 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021090ret_free:
21091 vim_free(skip_until);
21092 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095}
21096
21097/*
21098 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021099 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021101 * flags:
21102 * TFN_INT: internal function name OK
21103 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 * Advances "pp" to just after the function name (if no error).
21105 */
21106 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021107trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 char_u **pp;
21109 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021111 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021113 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 char_u *start;
21115 char_u *end;
21116 int lead;
21117 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021119 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021120
21121 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021124
21125 /* Check for hard coded <SNR>: already translated function ID (from a user
21126 * command). */
21127 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21128 && (*pp)[2] == (int)KE_SNR)
21129 {
21130 *pp += 3;
21131 len = get_id_len(pp) + 3;
21132 return vim_strnsave(start, len);
21133 }
21134
21135 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21136 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021138 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021140
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021141 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21142 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021143 if (end == start)
21144 {
21145 if (!skip)
21146 EMSG(_("E129: Function name required"));
21147 goto theend;
21148 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021149 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021150 {
21151 /*
21152 * Report an invalid expression in braces, unless the expression
21153 * evaluation has been cancelled due to an aborting error, an
21154 * interrupt, or an exception.
21155 */
21156 if (!aborting())
21157 {
21158 if (end != NULL)
21159 EMSG2(_(e_invarg2), start);
21160 }
21161 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021162 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021163 goto theend;
21164 }
21165
21166 if (lv.ll_tv != NULL)
21167 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021168 if (fdp != NULL)
21169 {
21170 fdp->fd_dict = lv.ll_dict;
21171 fdp->fd_newkey = lv.ll_newkey;
21172 lv.ll_newkey = NULL;
21173 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021174 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021175 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21176 {
21177 name = vim_strsave(lv.ll_tv->vval.v_string);
21178 *pp = end;
21179 }
21180 else
21181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021182 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21183 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021184 EMSG(_(e_funcref));
21185 else
21186 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021187 name = NULL;
21188 }
21189 goto theend;
21190 }
21191
21192 if (lv.ll_name == NULL)
21193 {
21194 /* Error found, but continue after the function name. */
21195 *pp = end;
21196 goto theend;
21197 }
21198
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021199 /* Check if the name is a Funcref. If so, use the value. */
21200 if (lv.ll_exp_name != NULL)
21201 {
21202 len = (int)STRLEN(lv.ll_exp_name);
21203 name = deref_func_name(lv.ll_exp_name, &len);
21204 if (name == lv.ll_exp_name)
21205 name = NULL;
21206 }
21207 else
21208 {
21209 len = (int)(end - *pp);
21210 name = deref_func_name(*pp, &len);
21211 if (name == *pp)
21212 name = NULL;
21213 }
21214 if (name != NULL)
21215 {
21216 name = vim_strsave(name);
21217 *pp = end;
21218 goto theend;
21219 }
21220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021221 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021222 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021223 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021224 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21225 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21226 {
21227 /* When there was "s:" already or the name expanded to get a
21228 * leading "s:" then remove it. */
21229 lv.ll_name += 2;
21230 len -= 2;
21231 lead = 2;
21232 }
21233 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021234 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021235 {
21236 if (lead == 2) /* skip over "s:" */
21237 lv.ll_name += 2;
21238 len = (int)(end - lv.ll_name);
21239 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021240
21241 /*
21242 * Copy the function name to allocated memory.
21243 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21244 * Accept <SNR>123_name() outside a script.
21245 */
21246 if (skip)
21247 lead = 0; /* do nothing */
21248 else if (lead > 0)
21249 {
21250 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021251 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21252 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021253 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021254 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021255 if (current_SID <= 0)
21256 {
21257 EMSG(_(e_usingsid));
21258 goto theend;
21259 }
21260 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21261 lead += (int)STRLEN(sid_buf);
21262 }
21263 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021264 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021265 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021266 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021267 goto theend;
21268 }
21269 name = alloc((unsigned)(len + lead + 1));
21270 if (name != NULL)
21271 {
21272 if (lead > 0)
21273 {
21274 name[0] = K_SPECIAL;
21275 name[1] = KS_EXTRA;
21276 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021277 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021278 STRCPY(name + 3, sid_buf);
21279 }
21280 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21281 name[len + lead] = NUL;
21282 }
21283 *pp = end;
21284
21285theend:
21286 clear_lval(&lv);
21287 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288}
21289
21290/*
21291 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21292 * Return 2 if "p" starts with "s:".
21293 * Return 0 otherwise.
21294 */
21295 static int
21296eval_fname_script(p)
21297 char_u *p;
21298{
21299 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21300 || STRNICMP(p + 1, "SNR>", 4) == 0))
21301 return 5;
21302 if (p[0] == 's' && p[1] == ':')
21303 return 2;
21304 return 0;
21305}
21306
21307/*
21308 * Return TRUE if "p" starts with "<SID>" or "s:".
21309 * Only works if eval_fname_script() returned non-zero for "p"!
21310 */
21311 static int
21312eval_fname_sid(p)
21313 char_u *p;
21314{
21315 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21316}
21317
21318/*
21319 * List the head of the function: "name(arg1, arg2)".
21320 */
21321 static void
21322list_func_head(fp, indent)
21323 ufunc_T *fp;
21324 int indent;
21325{
21326 int j;
21327
21328 msg_start();
21329 if (indent)
21330 MSG_PUTS(" ");
21331 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021332 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 {
21334 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021335 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 }
21337 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021338 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021340 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 {
21342 if (j)
21343 MSG_PUTS(", ");
21344 msg_puts(FUNCARG(fp, j));
21345 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021346 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 {
21348 if (j)
21349 MSG_PUTS(", ");
21350 MSG_PUTS("...");
21351 }
21352 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021353 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021354 if (p_verbose > 0)
21355 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356}
21357
21358/*
21359 * Find a function by name, return pointer to it in ufuncs.
21360 * Return NULL for unknown function.
21361 */
21362 static ufunc_T *
21363find_func(name)
21364 char_u *name;
21365{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021366 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021368 hi = hash_find(&func_hashtab, name);
21369 if (!HASHITEM_EMPTY(hi))
21370 return HI2UF(hi);
21371 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372}
21373
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021374#if defined(EXITFREE) || defined(PROTO)
21375 void
21376free_all_functions()
21377{
21378 hashitem_T *hi;
21379
21380 /* Need to start all over every time, because func_free() may change the
21381 * hash table. */
21382 while (func_hashtab.ht_used > 0)
21383 for (hi = func_hashtab.ht_array; ; ++hi)
21384 if (!HASHITEM_EMPTY(hi))
21385 {
21386 func_free(HI2UF(hi));
21387 break;
21388 }
21389}
21390#endif
21391
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021392/*
21393 * Return TRUE if a function "name" exists.
21394 */
21395 static int
21396function_exists(name)
21397 char_u *name;
21398{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021399 char_u *nm = name;
21400 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021401 int n = FALSE;
21402
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021403 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021404 nm = skipwhite(nm);
21405
21406 /* Only accept "funcname", "funcname ", "funcname (..." and
21407 * "funcname(...", not "funcname!...". */
21408 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021409 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021410 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021411 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021412 else
21413 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021414 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021415 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021416 return n;
21417}
21418
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021419/*
21420 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021421 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021422 */
21423 static int
21424builtin_function(name)
21425 char_u *name;
21426{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021427 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21428 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021429}
21430
Bram Moolenaar05159a02005-02-26 23:04:13 +000021431#if defined(FEAT_PROFILE) || defined(PROTO)
21432/*
21433 * Start profiling function "fp".
21434 */
21435 static void
21436func_do_profile(fp)
21437 ufunc_T *fp;
21438{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021439 int len = fp->uf_lines.ga_len;
21440
21441 if (len == 0)
21442 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021443 fp->uf_tm_count = 0;
21444 profile_zero(&fp->uf_tm_self);
21445 profile_zero(&fp->uf_tm_total);
21446 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021447 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021448 if (fp->uf_tml_total == NULL)
21449 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021450 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021451 if (fp->uf_tml_self == NULL)
21452 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021453 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021454 fp->uf_tml_idx = -1;
21455 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21456 || fp->uf_tml_self == NULL)
21457 return; /* out of memory */
21458
21459 fp->uf_profiling = TRUE;
21460}
21461
21462/*
21463 * Dump the profiling results for all functions in file "fd".
21464 */
21465 void
21466func_dump_profile(fd)
21467 FILE *fd;
21468{
21469 hashitem_T *hi;
21470 int todo;
21471 ufunc_T *fp;
21472 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021473 ufunc_T **sorttab;
21474 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021475
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021476 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021477 if (todo == 0)
21478 return; /* nothing to dump */
21479
Bram Moolenaar73830342005-02-28 22:48:19 +000021480 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21481
Bram Moolenaar05159a02005-02-26 23:04:13 +000021482 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21483 {
21484 if (!HASHITEM_EMPTY(hi))
21485 {
21486 --todo;
21487 fp = HI2UF(hi);
21488 if (fp->uf_profiling)
21489 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021490 if (sorttab != NULL)
21491 sorttab[st_len++] = fp;
21492
Bram Moolenaar05159a02005-02-26 23:04:13 +000021493 if (fp->uf_name[0] == K_SPECIAL)
21494 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21495 else
21496 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21497 if (fp->uf_tm_count == 1)
21498 fprintf(fd, "Called 1 time\n");
21499 else
21500 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21501 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21502 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21503 fprintf(fd, "\n");
21504 fprintf(fd, "count total (s) self (s)\n");
21505
21506 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21507 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021508 if (FUNCLINE(fp, i) == NULL)
21509 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021510 prof_func_line(fd, fp->uf_tml_count[i],
21511 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021512 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21513 }
21514 fprintf(fd, "\n");
21515 }
21516 }
21517 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021518
21519 if (sorttab != NULL && st_len > 0)
21520 {
21521 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21522 prof_total_cmp);
21523 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21524 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21525 prof_self_cmp);
21526 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21527 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021528
21529 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021530}
Bram Moolenaar73830342005-02-28 22:48:19 +000021531
21532 static void
21533prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21534 FILE *fd;
21535 ufunc_T **sorttab;
21536 int st_len;
21537 char *title;
21538 int prefer_self; /* when equal print only self time */
21539{
21540 int i;
21541 ufunc_T *fp;
21542
21543 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21544 fprintf(fd, "count total (s) self (s) function\n");
21545 for (i = 0; i < 20 && i < st_len; ++i)
21546 {
21547 fp = sorttab[i];
21548 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21549 prefer_self);
21550 if (fp->uf_name[0] == K_SPECIAL)
21551 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21552 else
21553 fprintf(fd, " %s()\n", fp->uf_name);
21554 }
21555 fprintf(fd, "\n");
21556}
21557
21558/*
21559 * Print the count and times for one function or function line.
21560 */
21561 static void
21562prof_func_line(fd, count, total, self, prefer_self)
21563 FILE *fd;
21564 int count;
21565 proftime_T *total;
21566 proftime_T *self;
21567 int prefer_self; /* when equal print only self time */
21568{
21569 if (count > 0)
21570 {
21571 fprintf(fd, "%5d ", count);
21572 if (prefer_self && profile_equal(total, self))
21573 fprintf(fd, " ");
21574 else
21575 fprintf(fd, "%s ", profile_msg(total));
21576 if (!prefer_self && profile_equal(total, self))
21577 fprintf(fd, " ");
21578 else
21579 fprintf(fd, "%s ", profile_msg(self));
21580 }
21581 else
21582 fprintf(fd, " ");
21583}
21584
21585/*
21586 * Compare function for total time sorting.
21587 */
21588 static int
21589#ifdef __BORLANDC__
21590_RTLENTRYF
21591#endif
21592prof_total_cmp(s1, s2)
21593 const void *s1;
21594 const void *s2;
21595{
21596 ufunc_T *p1, *p2;
21597
21598 p1 = *(ufunc_T **)s1;
21599 p2 = *(ufunc_T **)s2;
21600 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21601}
21602
21603/*
21604 * Compare function for self time sorting.
21605 */
21606 static int
21607#ifdef __BORLANDC__
21608_RTLENTRYF
21609#endif
21610prof_self_cmp(s1, s2)
21611 const void *s1;
21612 const void *s2;
21613{
21614 ufunc_T *p1, *p2;
21615
21616 p1 = *(ufunc_T **)s1;
21617 p2 = *(ufunc_T **)s2;
21618 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21619}
21620
Bram Moolenaar05159a02005-02-26 23:04:13 +000021621#endif
21622
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021623/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021624 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021625 * Return TRUE if a package was loaded.
21626 */
21627 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021628script_autoload(name, reload)
21629 char_u *name;
21630 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021631{
21632 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021633 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021634 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021635 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021636
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021637 /* Return quickly when autoload disabled. */
21638 if (no_autoload)
21639 return FALSE;
21640
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021641 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021642 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021643 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021644 return FALSE;
21645
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021646 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021647
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021648 /* Find the name in the list of previously loaded package names. Skip
21649 * "autoload/", it's always the same. */
21650 for (i = 0; i < ga_loaded.ga_len; ++i)
21651 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21652 break;
21653 if (!reload && i < ga_loaded.ga_len)
21654 ret = FALSE; /* was loaded already */
21655 else
21656 {
21657 /* Remember the name if it wasn't loaded already. */
21658 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21659 {
21660 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21661 tofree = NULL;
21662 }
21663
21664 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021665 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021666 ret = TRUE;
21667 }
21668
21669 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021670 return ret;
21671}
21672
21673/*
21674 * Return the autoload script name for a function or variable name.
21675 * Returns NULL when out of memory.
21676 */
21677 static char_u *
21678autoload_name(name)
21679 char_u *name;
21680{
21681 char_u *p;
21682 char_u *scriptname;
21683
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021684 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021685 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21686 if (scriptname == NULL)
21687 return FALSE;
21688 STRCPY(scriptname, "autoload/");
21689 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021690 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021691 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021692 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021693 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021694 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021695}
21696
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21698
21699/*
21700 * Function given to ExpandGeneric() to obtain the list of user defined
21701 * function names.
21702 */
21703 char_u *
21704get_user_func_name(xp, idx)
21705 expand_T *xp;
21706 int idx;
21707{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021708 static long_u done;
21709 static hashitem_T *hi;
21710 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711
21712 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021714 done = 0;
21715 hi = func_hashtab.ht_array;
21716 }
21717 if (done < func_hashtab.ht_used)
21718 {
21719 if (done++ > 0)
21720 ++hi;
21721 while (HASHITEM_EMPTY(hi))
21722 ++hi;
21723 fp = HI2UF(hi);
21724
21725 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21726 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727
21728 cat_func_name(IObuff, fp);
21729 if (xp->xp_context != EXPAND_USER_FUNC)
21730 {
21731 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021732 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 STRCAT(IObuff, ")");
21734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 return IObuff;
21736 }
21737 return NULL;
21738}
21739
21740#endif /* FEAT_CMDL_COMPL */
21741
21742/*
21743 * Copy the function name of "fp" to buffer "buf".
21744 * "buf" must be able to hold the function name plus three bytes.
21745 * Takes care of script-local function names.
21746 */
21747 static void
21748cat_func_name(buf, fp)
21749 char_u *buf;
21750 ufunc_T *fp;
21751{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021752 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 {
21754 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021755 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 }
21757 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021758 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759}
21760
21761/*
21762 * ":delfunction {name}"
21763 */
21764 void
21765ex_delfunction(eap)
21766 exarg_T *eap;
21767{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021768 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 char_u *p;
21770 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772
21773 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021774 name = trans_function_name(&p, eap->skip, 0, &fudi);
21775 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021777 {
21778 if (fudi.fd_dict != NULL && !eap->skip)
21779 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 if (!ends_excmd(*skipwhite(p)))
21783 {
21784 vim_free(name);
21785 EMSG(_(e_trailing));
21786 return;
21787 }
21788 eap->nextcmd = check_nextcmd(p);
21789 if (eap->nextcmd != NULL)
21790 *p = NUL;
21791
21792 if (!eap->skip)
21793 fp = find_func(name);
21794 vim_free(name);
21795
21796 if (!eap->skip)
21797 {
21798 if (fp == NULL)
21799 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021800 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 return;
21802 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021803 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 {
21805 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21806 return;
21807 }
21808
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021809 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021811 /* Delete the dict item that refers to the function, it will
21812 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021813 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021815 else
21816 func_free(fp);
21817 }
21818}
21819
21820/*
21821 * Free a function and remove it from the list of functions.
21822 */
21823 static void
21824func_free(fp)
21825 ufunc_T *fp;
21826{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021827 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021828
21829 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021830 ga_clear_strings(&(fp->uf_args));
21831 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021832#ifdef FEAT_PROFILE
21833 vim_free(fp->uf_tml_count);
21834 vim_free(fp->uf_tml_total);
21835 vim_free(fp->uf_tml_self);
21836#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021837
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021838 /* remove the function from the function hashtable */
21839 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21840 if (HASHITEM_EMPTY(hi))
21841 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021842 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021843 hash_remove(&func_hashtab, hi);
21844
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021845 vim_free(fp);
21846}
21847
21848/*
21849 * Unreference a Function: decrement the reference count and free it when it
21850 * becomes zero. Only for numbered functions.
21851 */
21852 static void
21853func_unref(name)
21854 char_u *name;
21855{
21856 ufunc_T *fp;
21857
21858 if (name != NULL && isdigit(*name))
21859 {
21860 fp = find_func(name);
21861 if (fp == NULL)
21862 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021863 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021864 {
21865 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021866 * when "uf_calls" becomes zero. */
21867 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021868 func_free(fp);
21869 }
21870 }
21871}
21872
21873/*
21874 * Count a reference to a Function.
21875 */
21876 static void
21877func_ref(name)
21878 char_u *name;
21879{
21880 ufunc_T *fp;
21881
21882 if (name != NULL && isdigit(*name))
21883 {
21884 fp = find_func(name);
21885 if (fp == NULL)
21886 EMSG2(_(e_intern2), "func_ref()");
21887 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021888 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 }
21890}
21891
21892/*
21893 * Call a user function.
21894 */
21895 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021896call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 ufunc_T *fp; /* pointer to function */
21898 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 typval_T *argvars; /* arguments */
21900 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 linenr_T firstline; /* first line of range */
21902 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904{
Bram Moolenaar33570922005-01-25 22:26:29 +000021905 char_u *save_sourcing_name;
21906 linenr_T save_sourcing_lnum;
21907 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021908 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021909 int save_did_emsg;
21910 static int depth = 0;
21911 dictitem_T *v;
21912 int fixvar_idx = 0; /* index in fixvar[] */
21913 int i;
21914 int ai;
21915 char_u numbuf[NUMBUFLEN];
21916 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021917#ifdef FEAT_PROFILE
21918 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021919 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921
21922 /* If depth of calling is getting too high, don't execute the function */
21923 if (depth >= p_mfd)
21924 {
21925 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021926 rettv->v_type = VAR_NUMBER;
21927 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 return;
21929 }
21930 ++depth;
21931
21932 line_breakcheck(); /* check for CTRL-C hit */
21933
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021934 fc = (funccall_T *)alloc(sizeof(funccall_T));
21935 fc->caller = current_funccal;
21936 current_funccal = fc;
21937 fc->func = fp;
21938 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021939 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021940 fc->linenr = 0;
21941 fc->returned = FALSE;
21942 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021944 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21945 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946
Bram Moolenaar33570922005-01-25 22:26:29 +000021947 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021948 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021949 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21950 * each argument variable and saves a lot of time.
21951 */
21952 /*
21953 * Init l: variables.
21954 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021955 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021956 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021957 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021958 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21959 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021960 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021961 name = v->di_key;
21962 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021964 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021965 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021966 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021967 v->di_tv.vval.v_dict = selfdict;
21968 ++selfdict->dv_refcount;
21969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021970
Bram Moolenaar33570922005-01-25 22:26:29 +000021971 /*
21972 * Init a: variables.
21973 * Set a:0 to "argcount".
21974 * Set a:000 to a list with room for the "..." arguments.
21975 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021976 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21977 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021978 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021979 /* Use "name" to avoid a warning from some compiler that checks the
21980 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021981 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021982 name = v->di_key;
21983 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021984 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021985 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021986 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021987 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021988 v->di_tv.vval.v_list = &fc->l_varlist;
21989 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21990 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21991 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021992
21993 /*
21994 * Set a:firstline to "firstline" and a:lastline to "lastline".
21995 * Set a:name to named arguments.
21996 * Set a:N to the "..." arguments.
21997 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021998 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022000 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022001 (varnumber_T)lastline);
22002 for (i = 0; i < argcount; ++i)
22003 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022004 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 if (ai < 0)
22006 /* named argument a:name */
22007 name = FUNCARG(fp, i);
22008 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022009 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 /* "..." argument a:1, a:2, etc. */
22011 sprintf((char *)numbuf, "%d", ai + 1);
22012 name = numbuf;
22013 }
22014 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22015 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022016 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22018 }
22019 else
22020 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022021 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22022 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022023 if (v == NULL)
22024 break;
22025 v->di_flags = DI_FLAGS_RO;
22026 }
22027 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022028 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022029
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022030 /* Note: the values are copied directly to avoid alloc/free.
22031 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022032 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022033 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022034
22035 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22036 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022037 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22038 fc->l_listitems[ai].li_tv = argvars[i];
22039 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022040 }
22041 }
22042
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 /* Don't redraw while executing the function. */
22044 ++RedrawingDisabled;
22045 save_sourcing_name = sourcing_name;
22046 save_sourcing_lnum = sourcing_lnum;
22047 sourcing_lnum = 1;
22048 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022049 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 if (sourcing_name != NULL)
22051 {
22052 if (save_sourcing_name != NULL
22053 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22054 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22055 else
22056 STRCPY(sourcing_name, "function ");
22057 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22058
22059 if (p_verbose >= 12)
22060 {
22061 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022062 verbose_enter_scroll();
22063
Bram Moolenaar555b2802005-05-19 21:08:39 +000022064 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 if (p_verbose >= 14)
22066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022068 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022069 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022070 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071
22072 msg_puts((char_u *)"(");
22073 for (i = 0; i < argcount; ++i)
22074 {
22075 if (i > 0)
22076 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022077 if (argvars[i].v_type == VAR_NUMBER)
22078 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 else
22080 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022081 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22082 if (s != NULL)
22083 {
22084 trunc_string(s, buf, MSG_BUF_CLEN);
22085 msg_puts(buf);
22086 vim_free(tofree);
22087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 }
22089 }
22090 msg_puts((char_u *)")");
22091 }
22092 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022093
22094 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 --no_wait_return;
22096 }
22097 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022098#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022099 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022100 {
22101 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22102 func_do_profile(fp);
22103 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022104 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022105 {
22106 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022107 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022108 profile_zero(&fp->uf_tm_children);
22109 }
22110 script_prof_save(&wait_start);
22111 }
22112#endif
22113
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022115 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116 save_did_emsg = did_emsg;
22117 did_emsg = FALSE;
22118
22119 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022120 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22122
22123 --RedrawingDisabled;
22124
22125 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022126 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022128 clear_tv(rettv);
22129 rettv->v_type = VAR_NUMBER;
22130 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 }
22132
Bram Moolenaar05159a02005-02-26 23:04:13 +000022133#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022134 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022135 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022136 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022137 profile_end(&call_start);
22138 profile_sub_wait(&wait_start, &call_start);
22139 profile_add(&fp->uf_tm_total, &call_start);
22140 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022141 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022142 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022143 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22144 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022145 }
22146 }
22147#endif
22148
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 /* when being verbose, mention the return value */
22150 if (p_verbose >= 12)
22151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022153 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022156 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022157 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022158 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022159 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022162 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022163 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022164 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022165 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022166
Bram Moolenaar555b2802005-05-19 21:08:39 +000022167 /* The value may be very long. Skip the middle part, so that we
22168 * have some idea how it starts and ends. smsg() would always
22169 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022170 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022171 if (s != NULL)
22172 {
22173 trunc_string(s, buf, MSG_BUF_CLEN);
22174 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22175 vim_free(tofree);
22176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 }
22178 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022179
22180 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 --no_wait_return;
22182 }
22183
22184 vim_free(sourcing_name);
22185 sourcing_name = save_sourcing_name;
22186 sourcing_lnum = save_sourcing_lnum;
22187 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022188#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022189 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022190 script_prof_restore(&wait_start);
22191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192
22193 if (p_verbose >= 12 && sourcing_name != NULL)
22194 {
22195 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022196 verbose_enter_scroll();
22197
Bram Moolenaar555b2802005-05-19 21:08:39 +000022198 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022200
22201 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 --no_wait_return;
22203 }
22204
22205 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022206 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022208
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022209 /* If the a:000 list and the l: and a: dicts are not referenced we can
22210 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022211 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22212 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22213 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22214 {
22215 free_funccal(fc, FALSE);
22216 }
22217 else
22218 {
22219 hashitem_T *hi;
22220 listitem_T *li;
22221 int todo;
22222
22223 /* "fc" is still in use. This can happen when returning "a:000" or
22224 * assigning "l:" to a global variable.
22225 * Link "fc" in the list for garbage collection later. */
22226 fc->caller = previous_funccal;
22227 previous_funccal = fc;
22228
22229 /* Make a copy of the a: variables, since we didn't do that above. */
22230 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22231 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22232 {
22233 if (!HASHITEM_EMPTY(hi))
22234 {
22235 --todo;
22236 v = HI2DI(hi);
22237 copy_tv(&v->di_tv, &v->di_tv);
22238 }
22239 }
22240
22241 /* Make a copy of the a:000 items, since we didn't do that above. */
22242 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22243 copy_tv(&li->li_tv, &li->li_tv);
22244 }
22245}
22246
22247/*
22248 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022249 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022250 */
22251 static int
22252can_free_funccal(fc, copyID)
22253 funccall_T *fc;
22254 int copyID;
22255{
22256 return (fc->l_varlist.lv_copyID != copyID
22257 && fc->l_vars.dv_copyID != copyID
22258 && fc->l_avars.dv_copyID != copyID);
22259}
22260
22261/*
22262 * Free "fc" and what it contains.
22263 */
22264 static void
22265free_funccal(fc, free_val)
22266 funccall_T *fc;
22267 int free_val; /* a: vars were allocated */
22268{
22269 listitem_T *li;
22270
22271 /* The a: variables typevals may not have been allocated, only free the
22272 * allocated variables. */
22273 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22274
22275 /* free all l: variables */
22276 vars_clear(&fc->l_vars.dv_hashtab);
22277
22278 /* Free the a:000 variables if they were allocated. */
22279 if (free_val)
22280 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22281 clear_tv(&li->li_tv);
22282
22283 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284}
22285
22286/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022287 * Add a number variable "name" to dict "dp" with value "nr".
22288 */
22289 static void
22290add_nr_var(dp, v, name, nr)
22291 dict_T *dp;
22292 dictitem_T *v;
22293 char *name;
22294 varnumber_T nr;
22295{
22296 STRCPY(v->di_key, name);
22297 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22298 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22299 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022300 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 v->di_tv.vval.v_number = nr;
22302}
22303
22304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 * ":return [expr]"
22306 */
22307 void
22308ex_return(eap)
22309 exarg_T *eap;
22310{
22311 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 int returning = FALSE;
22314
22315 if (current_funccal == NULL)
22316 {
22317 EMSG(_("E133: :return not inside a function"));
22318 return;
22319 }
22320
22321 if (eap->skip)
22322 ++emsg_skip;
22323
22324 eap->nextcmd = NULL;
22325 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022326 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 {
22328 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022329 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022331 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 }
22333 /* It's safer to return also on error. */
22334 else if (!eap->skip)
22335 {
22336 /*
22337 * Return unless the expression evaluation has been cancelled due to an
22338 * aborting error, an interrupt, or an exception.
22339 */
22340 if (!aborting())
22341 returning = do_return(eap, FALSE, TRUE, NULL);
22342 }
22343
22344 /* When skipping or the return gets pending, advance to the next command
22345 * in this line (!returning). Otherwise, ignore the rest of the line.
22346 * Following lines will be ignored by get_func_line(). */
22347 if (returning)
22348 eap->nextcmd = NULL;
22349 else if (eap->nextcmd == NULL) /* no argument */
22350 eap->nextcmd = check_nextcmd(arg);
22351
22352 if (eap->skip)
22353 --emsg_skip;
22354}
22355
22356/*
22357 * Return from a function. Possibly makes the return pending. Also called
22358 * for a pending return at the ":endtry" or after returning from an extra
22359 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022360 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022361 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 * FALSE when the return gets pending.
22363 */
22364 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022365do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 exarg_T *eap;
22367 int reanimate;
22368 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022369 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370{
22371 int idx;
22372 struct condstack *cstack = eap->cstack;
22373
22374 if (reanimate)
22375 /* Undo the return. */
22376 current_funccal->returned = FALSE;
22377
22378 /*
22379 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22380 * not in its finally clause (which then is to be executed next) is found.
22381 * In this case, make the ":return" pending for execution at the ":endtry".
22382 * Otherwise, return normally.
22383 */
22384 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22385 if (idx >= 0)
22386 {
22387 cstack->cs_pending[idx] = CSTP_RETURN;
22388
22389 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022390 /* A pending return again gets pending. "rettv" points to an
22391 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022393 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 else
22395 {
22396 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022397 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022399 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022401 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 {
22403 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022404 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022405 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 else
22407 EMSG(_(e_outofmem));
22408 }
22409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022410 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411
22412 if (reanimate)
22413 {
22414 /* The pending return value could be overwritten by a ":return"
22415 * without argument in a finally clause; reset the default
22416 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022417 current_funccal->rettv->v_type = VAR_NUMBER;
22418 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 }
22420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022421 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 }
22423 else
22424 {
22425 current_funccal->returned = TRUE;
22426
22427 /* If the return is carried out now, store the return value. For
22428 * a return immediately after reanimation, the value is already
22429 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022430 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022432 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022433 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022435 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 }
22437 }
22438
22439 return idx < 0;
22440}
22441
22442/*
22443 * Free the variable with a pending return value.
22444 */
22445 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022446discard_pending_return(rettv)
22447 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448{
Bram Moolenaar33570922005-01-25 22:26:29 +000022449 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450}
22451
22452/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022453 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 * is an allocated string. Used by report_pending() for verbose messages.
22455 */
22456 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022457get_return_cmd(rettv)
22458 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022460 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022461 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022462 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022464 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022465 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022466 if (s == NULL)
22467 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022468
22469 STRCPY(IObuff, ":return ");
22470 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22471 if (STRLEN(s) + 8 >= IOSIZE)
22472 STRCPY(IObuff + IOSIZE - 4, "...");
22473 vim_free(tofree);
22474 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475}
22476
22477/*
22478 * Get next function line.
22479 * Called by do_cmdline() to get the next line.
22480 * Returns allocated string, or NULL for end of function.
22481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 char_u *
22483get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022484 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022486 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487{
Bram Moolenaar33570922005-01-25 22:26:29 +000022488 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022489 ufunc_T *fp = fcp->func;
22490 char_u *retval;
22491 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492
22493 /* If breakpoints have been added/deleted need to check for it. */
22494 if (fcp->dbg_tick != debug_tick)
22495 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022496 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 sourcing_lnum);
22498 fcp->dbg_tick = debug_tick;
22499 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022500#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022501 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022502 func_line_end(cookie);
22503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504
Bram Moolenaar05159a02005-02-26 23:04:13 +000022505 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022506 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22507 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 retval = NULL;
22509 else
22510 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022511 /* Skip NULL lines (continuation lines). */
22512 while (fcp->linenr < gap->ga_len
22513 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22514 ++fcp->linenr;
22515 if (fcp->linenr >= gap->ga_len)
22516 retval = NULL;
22517 else
22518 {
22519 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22520 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022521#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022522 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022523 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022524#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 }
22527
22528 /* Did we encounter a breakpoint? */
22529 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22530 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022531 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022533 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 sourcing_lnum);
22535 fcp->dbg_tick = debug_tick;
22536 }
22537
22538 return retval;
22539}
22540
Bram Moolenaar05159a02005-02-26 23:04:13 +000022541#if defined(FEAT_PROFILE) || defined(PROTO)
22542/*
22543 * Called when starting to read a function line.
22544 * "sourcing_lnum" must be correct!
22545 * When skipping lines it may not actually be executed, but we won't find out
22546 * until later and we need to store the time now.
22547 */
22548 void
22549func_line_start(cookie)
22550 void *cookie;
22551{
22552 funccall_T *fcp = (funccall_T *)cookie;
22553 ufunc_T *fp = fcp->func;
22554
22555 if (fp->uf_profiling && sourcing_lnum >= 1
22556 && sourcing_lnum <= fp->uf_lines.ga_len)
22557 {
22558 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022559 /* Skip continuation lines. */
22560 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22561 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022562 fp->uf_tml_execed = FALSE;
22563 profile_start(&fp->uf_tml_start);
22564 profile_zero(&fp->uf_tml_children);
22565 profile_get_wait(&fp->uf_tml_wait);
22566 }
22567}
22568
22569/*
22570 * Called when actually executing a function line.
22571 */
22572 void
22573func_line_exec(cookie)
22574 void *cookie;
22575{
22576 funccall_T *fcp = (funccall_T *)cookie;
22577 ufunc_T *fp = fcp->func;
22578
22579 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22580 fp->uf_tml_execed = TRUE;
22581}
22582
22583/*
22584 * Called when done with a function line.
22585 */
22586 void
22587func_line_end(cookie)
22588 void *cookie;
22589{
22590 funccall_T *fcp = (funccall_T *)cookie;
22591 ufunc_T *fp = fcp->func;
22592
22593 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22594 {
22595 if (fp->uf_tml_execed)
22596 {
22597 ++fp->uf_tml_count[fp->uf_tml_idx];
22598 profile_end(&fp->uf_tml_start);
22599 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022600 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022601 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22602 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022603 }
22604 fp->uf_tml_idx = -1;
22605 }
22606}
22607#endif
22608
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609/*
22610 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022611 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 */
22613 int
22614func_has_ended(cookie)
22615 void *cookie;
22616{
Bram Moolenaar33570922005-01-25 22:26:29 +000022617 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618
22619 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22620 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022621 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 || fcp->returned);
22623}
22624
22625/*
22626 * return TRUE if cookie indicates a function which "abort"s on errors.
22627 */
22628 int
22629func_has_abort(cookie)
22630 void *cookie;
22631{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022632 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633}
22634
22635#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22636typedef enum
22637{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022638 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22639 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22640 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641} var_flavour_T;
22642
22643static var_flavour_T var_flavour __ARGS((char_u *varname));
22644
22645 static var_flavour_T
22646var_flavour(varname)
22647 char_u *varname;
22648{
22649 char_u *p = varname;
22650
22651 if (ASCII_ISUPPER(*p))
22652 {
22653 while (*(++p))
22654 if (ASCII_ISLOWER(*p))
22655 return VAR_FLAVOUR_SESSION;
22656 return VAR_FLAVOUR_VIMINFO;
22657 }
22658 else
22659 return VAR_FLAVOUR_DEFAULT;
22660}
22661#endif
22662
22663#if defined(FEAT_VIMINFO) || defined(PROTO)
22664/*
22665 * Restore global vars that start with a capital from the viminfo file
22666 */
22667 int
22668read_viminfo_varlist(virp, writing)
22669 vir_T *virp;
22670 int writing;
22671{
22672 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022673 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022674 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675
22676 if (!writing && (find_viminfo_parameter('!') != NULL))
22677 {
22678 tab = vim_strchr(virp->vir_line + 1, '\t');
22679 if (tab != NULL)
22680 {
22681 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022682 switch (*tab)
22683 {
22684 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022685#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022686 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022687#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022688 case 'D': type = VAR_DICT; break;
22689 case 'L': type = VAR_LIST; break;
22690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691
22692 tab = vim_strchr(tab, '\t');
22693 if (tab != NULL)
22694 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022695 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022696 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022697 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022699#ifdef FEAT_FLOAT
22700 else if (type == VAR_FLOAT)
22701 (void)string2float(tab + 1, &tv.vval.v_float);
22702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022704 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022705 if (type == VAR_DICT || type == VAR_LIST)
22706 {
22707 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22708
22709 if (etv == NULL)
22710 /* Failed to parse back the dict or list, use it as a
22711 * string. */
22712 tv.v_type = VAR_STRING;
22713 else
22714 {
22715 vim_free(tv.vval.v_string);
22716 tv = *etv;
22717 }
22718 }
22719
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022720 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022721
22722 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022723 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022724 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22725 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 }
22727 }
22728 }
22729
22730 return viminfo_readline(virp);
22731}
22732
22733/*
22734 * Write global vars that start with a capital to the viminfo file
22735 */
22736 void
22737write_viminfo_varlist(fp)
22738 FILE *fp;
22739{
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 hashitem_T *hi;
22741 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022742 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022743 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022744 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022745 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022746 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747
22748 if (find_viminfo_parameter('!') == NULL)
22749 return;
22750
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022751 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022752
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022753 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022756 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022758 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 this_var = HI2DI(hi);
22760 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022761 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022762 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022763 {
22764 case VAR_STRING: s = "STR"; break;
22765 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022766#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022767 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022768#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022769 case VAR_DICT: s = "DIC"; break;
22770 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022771 default: continue;
22772 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022773 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022774 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022775 if (p != NULL)
22776 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022777 vim_free(tofree);
22778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 }
22780 }
22781}
22782#endif
22783
22784#if defined(FEAT_SESSION) || defined(PROTO)
22785 int
22786store_session_globals(fd)
22787 FILE *fd;
22788{
Bram Moolenaar33570922005-01-25 22:26:29 +000022789 hashitem_T *hi;
22790 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022791 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 char_u *p, *t;
22793
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022794 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022795 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022797 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022799 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022800 this_var = HI2DI(hi);
22801 if ((this_var->di_tv.v_type == VAR_NUMBER
22802 || this_var->di_tv.v_type == VAR_STRING)
22803 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022804 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022805 /* Escape special characters with a backslash. Turn a LF and
22806 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022808 (char_u *)"\\\"\n\r");
22809 if (p == NULL) /* out of memory */
22810 break;
22811 for (t = p; *t != NUL; ++t)
22812 if (*t == '\n')
22813 *t = 'n';
22814 else if (*t == '\r')
22815 *t = 'r';
22816 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022817 this_var->di_key,
22818 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22819 : ' ',
22820 p,
22821 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22822 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022823 || put_eol(fd) == FAIL)
22824 {
22825 vim_free(p);
22826 return FAIL;
22827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 vim_free(p);
22829 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022830#ifdef FEAT_FLOAT
22831 else if (this_var->di_tv.v_type == VAR_FLOAT
22832 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22833 {
22834 float_T f = this_var->di_tv.vval.v_float;
22835 int sign = ' ';
22836
22837 if (f < 0)
22838 {
22839 f = -f;
22840 sign = '-';
22841 }
22842 if ((fprintf(fd, "let %s = %c&%f",
22843 this_var->di_key, sign, f) < 0)
22844 || put_eol(fd) == FAIL)
22845 return FAIL;
22846 }
22847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 }
22849 }
22850 return OK;
22851}
22852#endif
22853
Bram Moolenaar661b1822005-07-28 22:36:45 +000022854/*
22855 * Display script name where an item was last set.
22856 * Should only be invoked when 'verbose' is non-zero.
22857 */
22858 void
22859last_set_msg(scriptID)
22860 scid_T scriptID;
22861{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022862 char_u *p;
22863
Bram Moolenaar661b1822005-07-28 22:36:45 +000022864 if (scriptID != 0)
22865 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022866 p = home_replace_save(NULL, get_scriptname(scriptID));
22867 if (p != NULL)
22868 {
22869 verbose_enter();
22870 MSG_PUTS(_("\n\tLast set from "));
22871 MSG_PUTS(p);
22872 vim_free(p);
22873 verbose_leave();
22874 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022875 }
22876}
22877
Bram Moolenaard812df62008-11-09 12:46:09 +000022878/*
22879 * List v:oldfiles in a nice way.
22880 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022881 void
22882ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022883 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022884{
22885 list_T *l = vimvars[VV_OLDFILES].vv_list;
22886 listitem_T *li;
22887 int nr = 0;
22888
22889 if (l == NULL)
22890 msg((char_u *)_("No old files"));
22891 else
22892 {
22893 msg_start();
22894 msg_scroll = TRUE;
22895 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22896 {
22897 msg_outnum((long)++nr);
22898 MSG_PUTS(": ");
22899 msg_outtrans(get_tv_string(&li->li_tv));
22900 msg_putchar('\n');
22901 out_flush(); /* output one line at a time */
22902 ui_breakcheck();
22903 }
22904 /* Assume "got_int" was set to truncate the listing. */
22905 got_int = FALSE;
22906
22907#ifdef FEAT_BROWSE_CMD
22908 if (cmdmod.browse)
22909 {
22910 quit_more = FALSE;
22911 nr = prompt_for_number(FALSE);
22912 msg_starthere();
22913 if (nr > 0)
22914 {
22915 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22916 (long)nr);
22917
22918 if (p != NULL)
22919 {
22920 p = expand_env_save(p);
22921 eap->arg = p;
22922 eap->cmdidx = CMD_edit;
22923 cmdmod.browse = FALSE;
22924 do_exedit(eap, NULL);
22925 vim_free(p);
22926 }
22927 }
22928 }
22929#endif
22930 }
22931}
22932
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933#endif /* FEAT_EVAL */
22934
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022936#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937
22938#ifdef WIN3264
22939/*
22940 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22941 */
22942static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22943static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22944static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22945
22946/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022947 * Get the short path (8.3) for the filename in "fnamep".
22948 * Only works for a valid file name.
22949 * When the path gets longer "fnamep" is changed and the allocated buffer
22950 * is put in "bufp".
22951 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22952 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 */
22954 static int
22955get_short_pathname(fnamep, bufp, fnamelen)
22956 char_u **fnamep;
22957 char_u **bufp;
22958 int *fnamelen;
22959{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022960 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 char_u *newbuf;
22962
22963 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 l = GetShortPathName(*fnamep, *fnamep, len);
22965 if (l > len - 1)
22966 {
22967 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022968 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 newbuf = vim_strnsave(*fnamep, l);
22970 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022971 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972
22973 vim_free(*bufp);
22974 *fnamep = *bufp = newbuf;
22975
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022976 /* Really should always succeed, as the buffer is big enough. */
22977 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 }
22979
22980 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022981 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982}
22983
22984/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022985 * Get the short path (8.3) for the filename in "fname". The converted
22986 * path is returned in "bufp".
22987 *
22988 * Some of the directories specified in "fname" may not exist. This function
22989 * will shorten the existing directories at the beginning of the path and then
22990 * append the remaining non-existing path.
22991 *
22992 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022993 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022994 * bufp - Pointer to an allocated buffer for the filename.
22995 * fnamelen - Length of the filename pointed to by fname
22996 *
22997 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 */
22999 static int
23000shortpath_for_invalid_fname(fname, bufp, fnamelen)
23001 char_u **fname;
23002 char_u **bufp;
23003 int *fnamelen;
23004{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023005 char_u *short_fname, *save_fname, *pbuf_unused;
23006 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023008 int old_len, len;
23009 int new_len, sfx_len;
23010 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011
23012 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023013 old_len = *fnamelen;
23014 save_fname = vim_strnsave(*fname, old_len);
23015 pbuf_unused = NULL;
23016 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023018 endp = save_fname + old_len - 1; /* Find the end of the copy */
23019 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023021 /*
23022 * Try shortening the supplied path till it succeeds by removing one
23023 * directory at a time from the tail of the path.
23024 */
23025 len = 0;
23026 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023028 /* go back one path-separator */
23029 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23030 --endp;
23031 if (endp <= save_fname)
23032 break; /* processed the complete path */
23033
23034 /*
23035 * Replace the path separator with a NUL and try to shorten the
23036 * resulting path.
23037 */
23038 ch = *endp;
23039 *endp = 0;
23040 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023041 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023042 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23043 {
23044 retval = FAIL;
23045 goto theend;
23046 }
23047 *endp = ch; /* preserve the string */
23048
23049 if (len > 0)
23050 break; /* successfully shortened the path */
23051
23052 /* failed to shorten the path. Skip the path separator */
23053 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 }
23055
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023056 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023058 /*
23059 * Succeeded in shortening the path. Now concatenate the shortened
23060 * path with the remaining path at the tail.
23061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023063 /* Compute the length of the new path. */
23064 sfx_len = (int)(save_endp - endp) + 1;
23065 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023067 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023069 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023071 /* There is not enough space in the currently allocated string,
23072 * copy it to a buffer big enough. */
23073 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023075 {
23076 retval = FAIL;
23077 goto theend;
23078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 }
23080 else
23081 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023082 /* Transfer short_fname to the main buffer (it's big enough),
23083 * unless get_short_pathname() did its work in-place. */
23084 *fname = *bufp = save_fname;
23085 if (short_fname != save_fname)
23086 vim_strncpy(save_fname, short_fname, len);
23087 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023089
23090 /* concat the not-shortened part of the path */
23091 vim_strncpy(*fname + len, endp, sfx_len);
23092 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023094
23095theend:
23096 vim_free(pbuf_unused);
23097 vim_free(save_fname);
23098
23099 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100}
23101
23102/*
23103 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023104 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105 */
23106 static int
23107shortpath_for_partial(fnamep, bufp, fnamelen)
23108 char_u **fnamep;
23109 char_u **bufp;
23110 int *fnamelen;
23111{
23112 int sepcount, len, tflen;
23113 char_u *p;
23114 char_u *pbuf, *tfname;
23115 int hasTilde;
23116
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023117 /* Count up the path separators from the RHS.. so we know which part
23118 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023120 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 if (vim_ispathsep(*p))
23122 ++sepcount;
23123
23124 /* Need full path first (use expand_env() to remove a "~/") */
23125 hasTilde = (**fnamep == '~');
23126 if (hasTilde)
23127 pbuf = tfname = expand_env_save(*fnamep);
23128 else
23129 pbuf = tfname = FullName_save(*fnamep, FALSE);
23130
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023131 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023133 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23134 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135
23136 if (len == 0)
23137 {
23138 /* Don't have a valid filename, so shorten the rest of the
23139 * path if we can. This CAN give us invalid 8.3 filenames, but
23140 * there's not a lot of point in guessing what it might be.
23141 */
23142 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023143 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23144 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 }
23146
23147 /* Count the paths backward to find the beginning of the desired string. */
23148 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023149 {
23150#ifdef FEAT_MBYTE
23151 if (has_mbyte)
23152 p -= mb_head_off(tfname, p);
23153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 if (vim_ispathsep(*p))
23155 {
23156 if (sepcount == 0 || (hasTilde && sepcount == 1))
23157 break;
23158 else
23159 sepcount --;
23160 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 if (hasTilde)
23163 {
23164 --p;
23165 if (p >= tfname)
23166 *p = '~';
23167 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023168 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 }
23170 else
23171 ++p;
23172
23173 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23174 vim_free(*bufp);
23175 *fnamelen = (int)STRLEN(p);
23176 *bufp = pbuf;
23177 *fnamep = p;
23178
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023179 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180}
23181#endif /* WIN3264 */
23182
23183/*
23184 * Adjust a filename, according to a string of modifiers.
23185 * *fnamep must be NUL terminated when called. When returning, the length is
23186 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023187 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 * When there is an error, *fnamep is set to NULL.
23189 */
23190 int
23191modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23192 char_u *src; /* string with modifiers */
23193 int *usedlen; /* characters after src that are used */
23194 char_u **fnamep; /* file name so far */
23195 char_u **bufp; /* buffer for allocated file name or NULL */
23196 int *fnamelen; /* length of fnamep */
23197{
23198 int valid = 0;
23199 char_u *tail;
23200 char_u *s, *p, *pbuf;
23201 char_u dirname[MAXPATHL];
23202 int c;
23203 int has_fullname = 0;
23204#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023205 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 int has_shortname = 0;
23207#endif
23208
23209repeat:
23210 /* ":p" - full path/file_name */
23211 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23212 {
23213 has_fullname = 1;
23214
23215 valid |= VALID_PATH;
23216 *usedlen += 2;
23217
23218 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23219 if ((*fnamep)[0] == '~'
23220#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23221 && ((*fnamep)[1] == '/'
23222# ifdef BACKSLASH_IN_FILENAME
23223 || (*fnamep)[1] == '\\'
23224# endif
23225 || (*fnamep)[1] == NUL)
23226
23227#endif
23228 )
23229 {
23230 *fnamep = expand_env_save(*fnamep);
23231 vim_free(*bufp); /* free any allocated file name */
23232 *bufp = *fnamep;
23233 if (*fnamep == NULL)
23234 return -1;
23235 }
23236
23237 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023238 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239 {
23240 if (vim_ispathsep(*p)
23241 && p[1] == '.'
23242 && (p[2] == NUL
23243 || vim_ispathsep(p[2])
23244 || (p[2] == '.'
23245 && (p[3] == NUL || vim_ispathsep(p[3])))))
23246 break;
23247 }
23248
23249 /* FullName_save() is slow, don't use it when not needed. */
23250 if (*p != NUL || !vim_isAbsName(*fnamep))
23251 {
23252 *fnamep = FullName_save(*fnamep, *p != NUL);
23253 vim_free(*bufp); /* free any allocated file name */
23254 *bufp = *fnamep;
23255 if (*fnamep == NULL)
23256 return -1;
23257 }
23258
23259 /* Append a path separator to a directory. */
23260 if (mch_isdir(*fnamep))
23261 {
23262 /* Make room for one or two extra characters. */
23263 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23264 vim_free(*bufp); /* free any allocated file name */
23265 *bufp = *fnamep;
23266 if (*fnamep == NULL)
23267 return -1;
23268 add_pathsep(*fnamep);
23269 }
23270 }
23271
23272 /* ":." - path relative to the current directory */
23273 /* ":~" - path relative to the home directory */
23274 /* ":8" - shortname path - postponed till after */
23275 while (src[*usedlen] == ':'
23276 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23277 {
23278 *usedlen += 2;
23279 if (c == '8')
23280 {
23281#ifdef WIN3264
23282 has_shortname = 1; /* Postpone this. */
23283#endif
23284 continue;
23285 }
23286 pbuf = NULL;
23287 /* Need full path first (use expand_env() to remove a "~/") */
23288 if (!has_fullname)
23289 {
23290 if (c == '.' && **fnamep == '~')
23291 p = pbuf = expand_env_save(*fnamep);
23292 else
23293 p = pbuf = FullName_save(*fnamep, FALSE);
23294 }
23295 else
23296 p = *fnamep;
23297
23298 has_fullname = 0;
23299
23300 if (p != NULL)
23301 {
23302 if (c == '.')
23303 {
23304 mch_dirname(dirname, MAXPATHL);
23305 s = shorten_fname(p, dirname);
23306 if (s != NULL)
23307 {
23308 *fnamep = s;
23309 if (pbuf != NULL)
23310 {
23311 vim_free(*bufp); /* free any allocated file name */
23312 *bufp = pbuf;
23313 pbuf = NULL;
23314 }
23315 }
23316 }
23317 else
23318 {
23319 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23320 /* Only replace it when it starts with '~' */
23321 if (*dirname == '~')
23322 {
23323 s = vim_strsave(dirname);
23324 if (s != NULL)
23325 {
23326 *fnamep = s;
23327 vim_free(*bufp);
23328 *bufp = s;
23329 }
23330 }
23331 }
23332 vim_free(pbuf);
23333 }
23334 }
23335
23336 tail = gettail(*fnamep);
23337 *fnamelen = (int)STRLEN(*fnamep);
23338
23339 /* ":h" - head, remove "/file_name", can be repeated */
23340 /* Don't remove the first "/" or "c:\" */
23341 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23342 {
23343 valid |= VALID_HEAD;
23344 *usedlen += 2;
23345 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023346 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023347 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 *fnamelen = (int)(tail - *fnamep);
23349#ifdef VMS
23350 if (*fnamelen > 0)
23351 *fnamelen += 1; /* the path separator is part of the path */
23352#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023353 if (*fnamelen == 0)
23354 {
23355 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23356 p = vim_strsave((char_u *)".");
23357 if (p == NULL)
23358 return -1;
23359 vim_free(*bufp);
23360 *bufp = *fnamep = tail = p;
23361 *fnamelen = 1;
23362 }
23363 else
23364 {
23365 while (tail > s && !after_pathsep(s, tail))
23366 mb_ptr_back(*fnamep, tail);
23367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 }
23369
23370 /* ":8" - shortname */
23371 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23372 {
23373 *usedlen += 2;
23374#ifdef WIN3264
23375 has_shortname = 1;
23376#endif
23377 }
23378
23379#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023380 /*
23381 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 */
23383 if (has_shortname)
23384 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023385 /* Copy the string if it is shortened by :h and when it wasn't copied
23386 * yet, because we are going to change it in place. Avoids changing
23387 * the buffer name for "%:8". */
23388 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389 {
23390 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023391 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 return -1;
23393 vim_free(*bufp);
23394 *bufp = *fnamep = p;
23395 }
23396
23397 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023398 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 if (!has_fullname && !vim_isAbsName(*fnamep))
23400 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023401 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 return -1;
23403 }
23404 else
23405 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023406 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407
Bram Moolenaardc935552011-08-17 15:23:23 +020023408 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023410 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 return -1;
23412
23413 if (l == 0)
23414 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023415 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023417 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 return -1;
23419 }
23420 *fnamelen = l;
23421 }
23422 }
23423#endif /* WIN3264 */
23424
23425 /* ":t" - tail, just the basename */
23426 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23427 {
23428 *usedlen += 2;
23429 *fnamelen -= (int)(tail - *fnamep);
23430 *fnamep = tail;
23431 }
23432
23433 /* ":e" - extension, can be repeated */
23434 /* ":r" - root, without extension, can be repeated */
23435 while (src[*usedlen] == ':'
23436 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23437 {
23438 /* find a '.' in the tail:
23439 * - for second :e: before the current fname
23440 * - otherwise: The last '.'
23441 */
23442 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23443 s = *fnamep - 2;
23444 else
23445 s = *fnamep + *fnamelen - 1;
23446 for ( ; s > tail; --s)
23447 if (s[0] == '.')
23448 break;
23449 if (src[*usedlen + 1] == 'e') /* :e */
23450 {
23451 if (s > tail)
23452 {
23453 *fnamelen += (int)(*fnamep - (s + 1));
23454 *fnamep = s + 1;
23455#ifdef VMS
23456 /* cut version from the extension */
23457 s = *fnamep + *fnamelen - 1;
23458 for ( ; s > *fnamep; --s)
23459 if (s[0] == ';')
23460 break;
23461 if (s > *fnamep)
23462 *fnamelen = s - *fnamep;
23463#endif
23464 }
23465 else if (*fnamep <= tail)
23466 *fnamelen = 0;
23467 }
23468 else /* :r */
23469 {
23470 if (s > tail) /* remove one extension */
23471 *fnamelen = (int)(s - *fnamep);
23472 }
23473 *usedlen += 2;
23474 }
23475
23476 /* ":s?pat?foo?" - substitute */
23477 /* ":gs?pat?foo?" - global substitute */
23478 if (src[*usedlen] == ':'
23479 && (src[*usedlen + 1] == 's'
23480 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23481 {
23482 char_u *str;
23483 char_u *pat;
23484 char_u *sub;
23485 int sep;
23486 char_u *flags;
23487 int didit = FALSE;
23488
23489 flags = (char_u *)"";
23490 s = src + *usedlen + 2;
23491 if (src[*usedlen + 1] == 'g')
23492 {
23493 flags = (char_u *)"g";
23494 ++s;
23495 }
23496
23497 sep = *s++;
23498 if (sep)
23499 {
23500 /* find end of pattern */
23501 p = vim_strchr(s, sep);
23502 if (p != NULL)
23503 {
23504 pat = vim_strnsave(s, (int)(p - s));
23505 if (pat != NULL)
23506 {
23507 s = p + 1;
23508 /* find end of substitution */
23509 p = vim_strchr(s, sep);
23510 if (p != NULL)
23511 {
23512 sub = vim_strnsave(s, (int)(p - s));
23513 str = vim_strnsave(*fnamep, *fnamelen);
23514 if (sub != NULL && str != NULL)
23515 {
23516 *usedlen = (int)(p + 1 - src);
23517 s = do_string_sub(str, pat, sub, flags);
23518 if (s != NULL)
23519 {
23520 *fnamep = s;
23521 *fnamelen = (int)STRLEN(s);
23522 vim_free(*bufp);
23523 *bufp = s;
23524 didit = TRUE;
23525 }
23526 }
23527 vim_free(sub);
23528 vim_free(str);
23529 }
23530 vim_free(pat);
23531 }
23532 }
23533 /* after using ":s", repeat all the modifiers */
23534 if (didit)
23535 goto repeat;
23536 }
23537 }
23538
23539 return valid;
23540}
23541
23542/*
23543 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23544 * "flags" can be "g" to do a global substitute.
23545 * Returns an allocated string, NULL for error.
23546 */
23547 char_u *
23548do_string_sub(str, pat, sub, flags)
23549 char_u *str;
23550 char_u *pat;
23551 char_u *sub;
23552 char_u *flags;
23553{
23554 int sublen;
23555 regmatch_T regmatch;
23556 int i;
23557 int do_all;
23558 char_u *tail;
23559 garray_T ga;
23560 char_u *ret;
23561 char_u *save_cpo;
23562
23563 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23564 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023565 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566
23567 ga_init2(&ga, 1, 200);
23568
23569 do_all = (flags[0] == 'g');
23570
23571 regmatch.rm_ic = p_ic;
23572 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23573 if (regmatch.regprog != NULL)
23574 {
23575 tail = str;
23576 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23577 {
23578 /*
23579 * Get some space for a temporary buffer to do the substitution
23580 * into. It will contain:
23581 * - The text up to where the match is.
23582 * - The substituted text.
23583 * - The text after the match.
23584 */
23585 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23586 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23587 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23588 {
23589 ga_clear(&ga);
23590 break;
23591 }
23592
23593 /* copy the text up to where the match is */
23594 i = (int)(regmatch.startp[0] - tail);
23595 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23596 /* add the substituted text */
23597 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23598 + ga.ga_len + i, TRUE, TRUE, FALSE);
23599 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 /* avoid getting stuck on a match with an empty string */
23601 if (tail == regmatch.endp[0])
23602 {
23603 if (*tail == NUL)
23604 break;
23605 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23606 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607 }
23608 else
23609 {
23610 tail = regmatch.endp[0];
23611 if (*tail == NUL)
23612 break;
23613 }
23614 if (!do_all)
23615 break;
23616 }
23617
23618 if (ga.ga_data != NULL)
23619 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23620
23621 vim_free(regmatch.regprog);
23622 }
23623
23624 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23625 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023626 if (p_cpo == empty_option)
23627 p_cpo = save_cpo;
23628 else
23629 /* Darn, evaluating {sub} expression changed the value. */
23630 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631
23632 return ret;
23633}
23634
23635#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */