blob: e9ab0359a8832c417cd634a27e14404a510cf443 [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));
383#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
384static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
385#endif
386static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
387static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
388static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
390static void list_glob_vars __ARGS((int *first));
391static void list_buf_vars __ARGS((int *first));
392static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_vim_vars __ARGS((int *first));
397static void list_script_vars __ARGS((int *first));
398static void list_func_vars __ARGS((int *first));
399static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
401static int check_changedtick __ARGS((char_u *arg));
402static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
403static void clear_lval __ARGS((lval_T *lp));
404static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
405static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
406static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
407static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static listitem_T *listitem_alloc __ARGS((void));
431static void listitem_free __ARGS((listitem_T *item));
432static void listitem_remove __ARGS((list_T *l, listitem_T *item));
433static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100434static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
435static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
436static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000438static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000441static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
443static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
444static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000445static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *list2string __ARGS((typval_T *tv, int copyID));
448static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000449static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000450static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
451static void set_ref_in_list __ARGS((list_T *l, int copyID));
452static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200453static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000455static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
457static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000458static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000462static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
463static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static int string2float __ARGS((char_u *text, float_T *value));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
469static int find_internal_func __ARGS((char_u *name));
470static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
471static 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 +0200472static 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 +0000473static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000474static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200488static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#ifdef FEAT_FLOAT
502static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
503#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000504static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000507static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000510static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
513#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000516#ifdef FEAT_FLOAT
517static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200518static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
523static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000567static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000575static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000576static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000590static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000604static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000610static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200623static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000631static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000638#ifdef vim_mkdir
639static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100642#ifdef FEAT_MZSCHEME
643static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000647static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000648#ifdef FEAT_FLOAT
649static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000652static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000653static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000689static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000759static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000760static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static int get_env_len __ARGS((char_u **arg));
762static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
765#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
766#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
767 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000768static 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 +0000769static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000770static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
772static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static typval_T *alloc_tv __ARGS((void));
774static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void init_tv __ARGS((typval_T *varp));
776static long get_tv_number __ARGS((typval_T *varp));
777static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000778static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static char_u *get_tv_string __ARGS((typval_T *varp));
780static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000781static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000783static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
785static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
786static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000787static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
788static 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 +0000789static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
790static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000791static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200792static int var_check_func_name __ARGS((char_u *name, int new_var));
793static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000794static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000795static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
797static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
798static int eval_fname_script __ARGS((char_u *p));
799static int eval_fname_sid __ARGS((char_u *p));
800static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static ufunc_T *find_func __ARGS((char_u *name));
802static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000803static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
805static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000806static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
807static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_total_cmp __ARGS((const void *s1, const void *s2));
813static int
814# ifdef __BORLANDC__
815 _RTLENTRYF
816# endif
817 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000819static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000821static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void func_free __ARGS((ufunc_T *fp));
823static void func_unref __ARGS((char_u *name));
824static void func_ref __ARGS((char_u *name));
825static 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 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
854 init_var_dict(&globvardict, &globvars_var);
855 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200856 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
882 * Sort the function table, to enable binary sort.
883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200914 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915
916 /* global variables */
917 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000919 /* autoloaded script names */
920 ga_clear_strings(&ga_loaded);
921
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200922 /* script-local variables */
923 for (i = 1; i <= ga_scripts.ga_len; ++i)
924 {
925 vars_clear(&SCRIPT_VARS(i));
926 vim_free(SCRIPT_SV(i));
927 }
928 ga_clear(&ga_scripts);
929
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000930 /* unreferenced lists and dicts */
931 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932
933 /* functions */
934 free_all_functions();
935 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936}
937#endif
938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Return the name of the executed function.
941 */
942 char_u *
943func_name(cookie)
944 void *cookie;
945{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000946 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Return the address holding the next breakpoint line for a funccall cookie.
951 */
952 linenr_T *
953func_breakpoint(cookie)
954 void *cookie;
955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the address holding the debug tick for a funccall cookie.
961 */
962 int *
963func_dbg_tick(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Return the nesting level for a funccall cookie.
971 */
972 int
973func_level(cookie)
974 void *cookie;
975{
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000980funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000982/* pointer to list of previously used funccal, still around because some
983 * item in it is still being used. */
984funccall_T *previous_funccal = NULL;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986/*
987 * Return TRUE when a function was ended by a ":return" command.
988 */
989 int
990current_func_returned()
991{
992 return current_funccal->returned;
993}
994
995
996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Set an internal variable to a string value. Creates the variable if it does
998 * not already exist.
999 */
1000 void
1001set_internal_string_var(name, value)
1002 char_u *name;
1003 char_u *value;
1004{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 val = vim_strsave(value);
1009 if (val != NULL)
1010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001015 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018}
1019
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001021static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022static char_u *redir_endp = NULL;
1023static char_u *redir_varname = NULL;
1024
1025/*
1026 * Start recording command output to a variable
1027 * Returns OK if successfully completed the setup. FAIL otherwise.
1028 */
1029 int
1030var_redir_start(name, append)
1031 char_u *name;
1032 int append; /* append to an existing variable */
1033{
1034 int save_emsg;
1035 int err;
1036 typval_T tv;
1037
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001038 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001039 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 {
1041 EMSG(_(e_invarg));
1042 return FAIL;
1043 }
1044
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001045 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 redir_varname = vim_strsave(name);
1047 if (redir_varname == NULL)
1048 return FAIL;
1049
1050 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1051 if (redir_lval == NULL)
1052 {
1053 var_redir_stop();
1054 return FAIL;
1055 }
1056
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 /* The output is stored in growarray "redir_ga" until redirection ends. */
1058 ga_init2(&redir_ga, (int)sizeof(char), 500);
1059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001061 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1062 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1064 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001065 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp != NULL && *redir_endp != NUL)
1067 /* Trailing characters are present after the variable name */
1068 EMSG(_(e_trailing));
1069 else
1070 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001071 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 var_redir_stop();
1073 return FAIL;
1074 }
1075
1076 /* check if we can write to the variable: set it to or append an empty
1077 * string */
1078 save_emsg = did_emsg;
1079 did_emsg = FALSE;
1080 tv.v_type = VAR_STRING;
1081 tv.vval.v_string = (char_u *)"";
1082 if (append)
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1084 else
1085 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001086 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001088 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 if (err)
1090 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001091 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 var_redir_stop();
1093 return FAIL;
1094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 * Append "value[value_len]" to the variable set by var_redir_start().
1101 * The actual appending is postponed until redirection ends, because the value
1102 * appended may in fact be the string we write to, changing it may cause freed
1103 * memory to be used:
1104 * :redir => foo
1105 * :let foo
1106 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 */
1108 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114
1115 if (redir_lval == NULL)
1116 return;
1117
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 {
1125 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 }
1128 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130}
1131
1132/*
1133 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 */
1136 void
1137var_redir_stop()
1138{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 typval_T tv;
1140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_lval != NULL)
1142 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 /* If there was no error: assign the text to the variable. */
1144 if (redir_endp != NULL)
1145 {
1146 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001149 /* Call get_lval() again, if it's inside a Dict or List it may
1150 * have changed. */
1151 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1152 FALSE, FALSE, FALSE, FNE_CHECK_START);
1153 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1154 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1155 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* free the collected output */
1159 vim_free(redir_ga.ga_data);
1160 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 vim_free(redir_lval);
1163 redir_lval = NULL;
1164 }
1165 vim_free(redir_varname);
1166 redir_varname = NULL;
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169# if defined(FEAT_MBYTE) || defined(PROTO)
1170 int
1171eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1172 char_u *enc_from;
1173 char_u *enc_to;
1174 char_u *fname_from;
1175 char_u *fname_to;
1176{
1177 int err = FALSE;
1178
1179 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1180 set_vim_var_string(VV_CC_TO, enc_to, -1);
1181 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1182 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1183 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1184 err = TRUE;
1185 set_vim_var_string(VV_CC_FROM, NULL, -1);
1186 set_vim_var_string(VV_CC_TO, NULL, -1);
1187 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1188 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1189
1190 if (err)
1191 return FAIL;
1192 return OK;
1193}
1194# endif
1195
1196# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1197 int
1198eval_printexpr(fname, args)
1199 char_u *fname;
1200 char_u *args;
1201{
1202 int err = FALSE;
1203
1204 set_vim_var_string(VV_FNAME_IN, fname, -1);
1205 set_vim_var_string(VV_CMDARG, args, -1);
1206 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1207 err = TRUE;
1208 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1209 set_vim_var_string(VV_CMDARG, NULL, -1);
1210
1211 if (err)
1212 {
1213 mch_remove(fname);
1214 return FAIL;
1215 }
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_DIFF) || defined(PROTO)
1221 void
1222eval_diff(origfile, newfile, outfile)
1223 char_u *origfile;
1224 char_u *newfile;
1225 char_u *outfile;
1226{
1227 int err = FALSE;
1228
1229 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1230 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1231 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1232 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1233 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1234 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1235 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1236}
1237
1238 void
1239eval_patch(origfile, difffile, outfile)
1240 char_u *origfile;
1241 char_u *difffile;
1242 char_u *outfile;
1243{
1244 int err;
1245
1246 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1248 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1249 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1250 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1251 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1252 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1253}
1254# endif
1255
1256/*
1257 * Top level evaluation function, returning a boolean.
1258 * Sets "error" to TRUE if there was an error.
1259 * Return TRUE or FALSE.
1260 */
1261 int
1262eval_to_bool(arg, error, nextcmd, skip)
1263 char_u *arg;
1264 int *error;
1265 char_u **nextcmd;
1266 int skip; /* only parse, don't execute */
1267{
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 int retval = FALSE;
1270
1271 if (skip)
1272 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 else
1276 {
1277 *error = FALSE;
1278 if (!skip)
1279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001280 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 }
1284 if (skip)
1285 --emsg_skip;
1286
1287 return retval;
1288}
1289
1290/*
1291 * Top level evaluation function, returning a string. If "skip" is TRUE,
1292 * only parsing to "nextcmd" is done, without reporting errors. Return
1293 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1294 */
1295 char_u *
1296eval_to_string_skip(arg, nextcmd, skip)
1297 char_u *arg;
1298 char_u **nextcmd;
1299 int skip; /* only parse, don't execute */
1300{
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 char_u *retval;
1303
1304 if (skip)
1305 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 retval = NULL;
1308 else
1309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 retval = vim_strsave(get_tv_string(&tv));
1311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 if (skip)
1314 --emsg_skip;
1315
1316 return retval;
1317}
1318
1319/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320 * Skip over an expression at "*pp".
1321 * Return FAIL for an error, OK otherwise.
1322 */
1323 int
1324skip_expr(pp)
1325 char_u **pp;
1326{
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328
1329 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331}
1332
1333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335 * When "convert" is TRUE convert a List into a sequence of lines and convert
1336 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Return pointer to allocated memory, or NULL for failure.
1338 */
1339 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 char_u *arg;
1342 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001347 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 retval = NULL;
1354 else
1355 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 {
1358 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001359 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001360 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001361 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001362 if (tv.vval.v_list->lv_len > 0)
1363 ga_append(&ga, NL);
1364 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001365 ga_append(&ga, NUL);
1366 retval = (char_u *)ga.ga_data;
1367 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368#ifdef FEAT_FLOAT
1369 else if (convert && tv.v_type == VAR_FLOAT)
1370 {
1371 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1372 retval = vim_strsave(numbuf);
1373 }
1374#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001375 else
1376 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379
1380 return retval;
1381}
1382
1383/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384 * Call eval_to_string() without using current local variables and using
1385 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 */
1387 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 char_u *arg;
1390 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 char_u *retval;
1394 void *save_funccalp;
1395
1396 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 ++sandbox;
1399 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001400 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001401 if (use_sandbox)
1402 --sandbox;
1403 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 restore_funccal(save_funccalp);
1405 return retval;
1406}
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408/*
1409 * Top level evaluation function, returning a number.
1410 * Evaluates "expr" silently.
1411 * Returns -1 for an error.
1412 */
1413 int
1414eval_to_number(expr)
1415 char_u *expr;
1416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001419 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 ++emsg_off;
1422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 retval = -1;
1425 else
1426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001427 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
1430 --emsg_off;
1431
1432 return retval;
1433}
1434
Bram Moolenaara40058a2005-07-11 22:42:07 +00001435/*
1436 * Prepare v: variable "idx" to be used.
1437 * Save the current typeval in "save_tv".
1438 * When not used yet add the variable to the v: hashtable.
1439 */
1440 static void
1441prepare_vimvar(idx, save_tv)
1442 int idx;
1443 typval_T *save_tv;
1444{
1445 *save_tv = vimvars[idx].vv_tv;
1446 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1447 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1448}
1449
1450/*
1451 * Restore v: variable "idx" to typeval "save_tv".
1452 * When no longer defined, remove the variable from the v: hashtable.
1453 */
1454 static void
1455restore_vimvar(idx, save_tv)
1456 int idx;
1457 typval_T *save_tv;
1458{
1459 hashitem_T *hi;
1460
Bram Moolenaara40058a2005-07-11 22:42:07 +00001461 vimvars[idx].vv_tv = *save_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 {
1464 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1465 if (HASHITEM_EMPTY(hi))
1466 EMSG2(_(e_intern2), "restore_vimvar()");
1467 else
1468 hash_remove(&vimvarht, hi);
1469 }
1470}
1471
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001472#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473/*
1474 * Evaluate an expression to a list with suggestions.
1475 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001476 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001477 */
1478 list_T *
1479eval_spell_expr(badword, expr)
1480 char_u *badword;
1481 char_u *expr;
1482{
1483 typval_T save_val;
1484 typval_T rettv;
1485 list_T *list = NULL;
1486 char_u *p = skipwhite(expr);
1487
1488 /* Set "v:val" to the bad word. */
1489 prepare_vimvar(VV_VAL, &save_val);
1490 vimvars[VV_VAL].vv_type = VAR_STRING;
1491 vimvars[VV_VAL].vv_str = badword;
1492 if (p_verbose == 0)
1493 ++emsg_off;
1494
1495 if (eval1(&p, &rettv, TRUE) == OK)
1496 {
1497 if (rettv.v_type != VAR_LIST)
1498 clear_tv(&rettv);
1499 else
1500 list = rettv.vval.v_list;
1501 }
1502
1503 if (p_verbose == 0)
1504 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505 restore_vimvar(VV_VAL, &save_val);
1506
1507 return list;
1508}
1509
1510/*
1511 * "list" is supposed to contain two items: a word and a number. Return the
1512 * word in "pp" and the number as the return value.
1513 * Return -1 if anything isn't right.
1514 * Used to get the good word and score from the eval_spell_expr() result.
1515 */
1516 int
1517get_spellword(list, pp)
1518 list_T *list;
1519 char_u **pp;
1520{
1521 listitem_T *li;
1522
1523 li = list->lv_first;
1524 if (li == NULL)
1525 return -1;
1526 *pp = get_tv_string(&li->li_tv);
1527
1528 li = li->li_next;
1529 if (li == NULL)
1530 return -1;
1531 return get_tv_number(&li->li_tv);
1532}
1533#endif
1534
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001536 * Top level evaluation function.
1537 * Returns an allocated typval_T with the result.
1538 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539 */
1540 typval_T *
1541eval_expr(arg, nextcmd)
1542 char_u *arg;
1543 char_u **nextcmd;
1544{
1545 typval_T *tv;
1546
1547 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001548 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 {
1550 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 }
1553
1554 return tv;
1555}
1556
1557
Bram Moolenaar4f688582007-07-24 12:34:30 +00001558#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1559 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562 * Uses argv[argc] for the function arguments. Only Number and String
1563 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001566 static int
1567call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 char_u *func;
1569 int argc;
1570 char_u **argv;
1571 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
Bram Moolenaar33570922005-01-25 22:26:29 +00001574 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 long n;
1576 int len;
1577 int i;
1578 int doesrange;
1579 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001582 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 for (i = 0; i < argc; i++)
1587 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 /* Pass a NULL or empty argument as an empty string */
1589 if (argv[i] == NULL || *argv[i] == NUL)
1590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001591 argvars[i].v_type = VAR_STRING;
1592 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 continue;
1594 }
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /* Recognize a number argument, the others must be strings. */
1597 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1598 if (len != 0 && len == (int)STRLEN(argv[i]))
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_NUMBER;
1601 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 else
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_STRING;
1606 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 }
1609
1610 if (safe)
1611 {
1612 save_funccalp = save_funccal();
1613 ++sandbox;
1614 }
1615
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1617 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (safe)
1621 {
1622 --sandbox;
1623 restore_funccal(save_funccalp);
1624 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 vim_free(argvars);
1626
1627 if (ret == FAIL)
1628 clear_tv(rettv);
1629
1630 return ret;
1631}
1632
Bram Moolenaar4f688582007-07-24 12:34:30 +00001633# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001635 * Call vimL function "func" and return the result as a string.
1636 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 * Uses argv[argc] for the function arguments.
1638 */
1639 void *
1640call_func_retstr(func, argc, argv, safe)
1641 char_u *func;
1642 int argc;
1643 char_u **argv;
1644 int safe; /* use the sandbox */
1645{
1646 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001647 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648
1649 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1650 return NULL;
1651
1652 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 return retval;
1655}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001656# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001660 * Call vimL function "func" and return the result as a number.
1661 * Returns -1 when calling the function fails.
1662 * Uses argv[argc] for the function arguments.
1663 */
1664 long
1665call_func_retnr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
1672 long retval;
1673
1674 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1675 return -1;
1676
1677 retval = get_tv_number_chk(&rettv, NULL);
1678 clear_tv(&rettv);
1679 return retval;
1680}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001681# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682
1683/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001684 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001686 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 */
1688 void *
1689call_func_retlist(func, argc, argv, safe)
1690 char_u *func;
1691 int argc;
1692 char_u **argv;
1693 int safe; /* use the sandbox */
1694{
1695 typval_T rettv;
1696
1697 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1698 return NULL;
1699
1700 if (rettv.v_type != VAR_LIST)
1701 {
1702 clear_tv(&rettv);
1703 return NULL;
1704 }
1705
1706 return rettv.vval.v_list;
1707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711/*
1712 * Save the current function call pointer, and set it to NULL.
1713 * Used when executing autocommands and for ":source".
1714 */
1715 void *
1716save_funccal()
1717{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 current_funccal = NULL;
1721 return (void *)fc;
1722}
1723
1724 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725restore_funccal(vfc)
1726 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728 funccall_T *fc = (funccall_T *)vfc;
1729
1730 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731}
1732
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733#if defined(FEAT_PROFILE) || defined(PROTO)
1734/*
1735 * Prepare profiling for entering a child or something else that is not
1736 * counted for the script/function itself.
1737 * Should always be called in pair with prof_child_exit().
1738 */
1739 void
1740prof_child_enter(tm)
1741 proftime_T *tm; /* place to store waittime */
1742{
1743 funccall_T *fc = current_funccal;
1744
1745 if (fc != NULL && fc->func->uf_profiling)
1746 profile_start(&fc->prof_child);
1747 script_prof_save(tm);
1748}
1749
1750/*
1751 * Take care of time spent in a child.
1752 * Should always be called after prof_child_enter().
1753 */
1754 void
1755prof_child_exit(tm)
1756 proftime_T *tm; /* where waittime was stored */
1757{
1758 funccall_T *fc = current_funccal;
1759
1760 if (fc != NULL && fc->func->uf_profiling)
1761 {
1762 profile_end(&fc->prof_child);
1763 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1764 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1765 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1766 }
1767 script_prof_restore(tm);
1768}
1769#endif
1770
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_FOLDING
1773/*
1774 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1775 * it in "*cp". Doesn't give error messages.
1776 */
1777 int
1778eval_foldexpr(arg, cp)
1779 char_u *arg;
1780 int *cp;
1781{
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 int retval;
1784 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001785 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1786 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001789 if (use_sandbox)
1790 ++sandbox;
1791 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 retval = 0;
1795 else
1796 {
1797 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 if (tv.v_type == VAR_NUMBER)
1799 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001800 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 retval = 0;
1802 else
1803 {
1804 /* If the result is a string, check if there is a non-digit before
1805 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (!VIM_ISDIGIT(*s) && *s != '-')
1808 *cp = *s++;
1809 retval = atol((char *)s);
1810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001814 if (use_sandbox)
1815 --sandbox;
1816 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 return retval;
1819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 * ":let" list all variable values
1824 * ":let var1 var2" list variable values
1825 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 * ":let var += expr" assignment command.
1827 * ":let var -= expr" assignment command.
1828 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831 void
1832ex_let(eap)
1833 exarg_T *eap;
1834{
1835 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 int var_count = 0;
1840 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001843 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 argend = skip_var_list(arg, &var_count, &semicolon);
1846 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001848 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1849 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001853 /*
1854 * ":let" without "=": list variables
1855 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 if (*arg == '[')
1857 EMSG(_(e_invarg));
1858 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_glob_vars(&first);
1865 list_buf_vars(&first);
1866 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001867#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001869#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_script_vars(&first);
1871 list_func_vars(&first);
1872 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 eap->nextcmd = check_nextcmd(arg);
1875 }
1876 else
1877 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 op[0] = '=';
1879 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 {
1882 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1883 op[0] = expr[-1]; /* +=, -= or .= */
1884 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (eap->skip)
1888 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (eap->skip)
1891 {
1892 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 --emsg_skip;
1895 }
1896 else if (i != FAIL)
1897 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 }
1903}
1904
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905/*
1906 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1907 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 * When "nextchars" is not NULL it points to a string with characters that
1909 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1910 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 * Returns OK or FAIL;
1912 */
1913 static int
1914ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1915 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001916 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 int copy; /* copy values from "tv", don't move */
1918 int semicolon; /* from skip_var_list() */
1919 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921{
1922 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 listitem_T *item;
1926 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927
1928 if (*arg != '[')
1929 {
1930 /*
1931 * ":let var = expr" or ":for var in list"
1932 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 return FAIL;
1935 return OK;
1936 }
1937
1938 /*
1939 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1940 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001941 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 {
1943 EMSG(_(e_listreq));
1944 return FAIL;
1945 }
1946
1947 i = list_len(l);
1948 if (semicolon == 0 && var_count < i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953 if (var_count - semicolon > i)
1954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001955 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 return FAIL;
1957 }
1958
1959 item = l->lv_first;
1960 while (*arg != ']')
1961 {
1962 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 item = item->li_next;
1965 if (arg == NULL)
1966 return FAIL;
1967
1968 arg = skipwhite(arg);
1969 if (*arg == ';')
1970 {
1971 /* Put the rest of the list (may be empty) in the var after ';'.
1972 * Create a new list for this. */
1973 l = list_alloc();
1974 if (l == NULL)
1975 return FAIL;
1976 while (item != NULL)
1977 {
1978 list_append_tv(l, &item->li_tv);
1979 item = item->li_next;
1980 }
1981
1982 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001983 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 ltv.vval.v_list = l;
1985 l->lv_refcount = 1;
1986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1988 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 clear_tv(&ltv);
1990 if (arg == NULL)
1991 return FAIL;
1992 break;
1993 }
1994 else if (*arg != ',' && *arg != ']')
1995 {
1996 EMSG2(_(e_intern2), "ex_let_vars()");
1997 return FAIL;
1998 }
1999 }
2000
2001 return OK;
2002}
2003
2004/*
2005 * Skip over assignable variable "var" or list of variables "[var, var]".
2006 * Used for ":let varvar = expr" and ":for varvar in expr".
2007 * For "[var, var]" increment "*var_count" for each variable.
2008 * for "[var, var; var]" set "semicolon".
2009 * Return NULL for an error.
2010 */
2011 static char_u *
2012skip_var_list(arg, var_count, semicolon)
2013 char_u *arg;
2014 int *var_count;
2015 int *semicolon;
2016{
2017 char_u *p, *s;
2018
2019 if (*arg == '[')
2020 {
2021 /* "[var, var]": find the matching ']'. */
2022 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002023 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 {
2025 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2026 s = skip_var_one(p);
2027 if (s == p)
2028 {
2029 EMSG2(_(e_invarg2), p);
2030 return NULL;
2031 }
2032 ++*var_count;
2033
2034 p = skipwhite(s);
2035 if (*p == ']')
2036 break;
2037 else if (*p == ';')
2038 {
2039 if (*semicolon == 1)
2040 {
2041 EMSG(_("Double ; in list of variables"));
2042 return NULL;
2043 }
2044 *semicolon = 1;
2045 }
2046 else if (*p != ',')
2047 {
2048 EMSG2(_(e_invarg2), p);
2049 return NULL;
2050 }
2051 }
2052 return p + 1;
2053 }
2054 else
2055 return skip_var_one(arg);
2056}
2057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002059 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002060 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 static char_u *
2063skip_var_one(arg)
2064 char_u *arg;
2065{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002066 if (*arg == '@' && arg[1] != NUL)
2067 return arg + 2;
2068 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2069 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070}
2071
Bram Moolenaara7043832005-01-21 11:56:39 +00002072/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 * List variables for hashtab "ht" with prefix "prefix".
2074 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082{
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 hashitem_T *hi;
2084 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 int todo;
2086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002087 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2089 {
2090 if (!HASHITEM_EMPTY(hi))
2091 {
2092 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 di = HI2DI(hi);
2094 if (empty || di->di_tv.v_type != VAR_STRING
2095 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
2098 }
2099}
2100
2101/*
2102 * List global variables.
2103 */
2104 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105list_glob_vars(first)
2106 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002107{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002109}
2110
2111/*
2112 * List buffer variables.
2113 */
2114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_buf_vars(first)
2116 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 char_u numbuf[NUMBUFLEN];
2119
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2121 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122
2123 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2125 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List window variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_win_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2136 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137}
2138
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002139#ifdef FEAT_WINDOWS
2140/*
2141 * List tab page variables.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_tab_vars(first)
2145 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002146{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2148 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002149}
2150#endif
2151
Bram Moolenaara7043832005-01-21 11:56:39 +00002152/*
2153 * List Vim variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_vim_vars(first)
2157 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160}
2161
2162/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163 * List script-local variables, if there is a script.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_script_vars(first)
2167 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168{
2169 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2171 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172}
2173
2174/*
2175 * List function variables, if there is a function.
2176 */
2177 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178list_func_vars(first)
2179 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180{
2181 if (current_funccal != NULL)
2182 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002184}
2185
2186/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 * List variables in "arg".
2188 */
2189 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 exarg_T *eap;
2192 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194{
2195 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 char_u *name_start;
2199 char_u *arg_subsc;
2200 char_u *tofree;
2201 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202
2203 while (!ends_excmd(*arg) && !got_int)
2204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002207 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002208 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2209 {
2210 emsg_severe = TRUE;
2211 EMSG(_(e_trailing));
2212 break;
2213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* get_name_len() takes care of expanding curly braces */
2218 name_start = name = arg;
2219 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2220 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 /* This is mainly to keep test 49 working: when expanding
2223 * curly braces fails overrule the exception error message. */
2224 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 emsg_severe = TRUE;
2227 EMSG2(_(e_invarg2), arg);
2228 break;
2229 }
2230 error = TRUE;
2231 }
2232 else
2233 {
2234 if (tofree != NULL)
2235 name = tofree;
2236 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 else
2239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 /* handle d.key, l[idx], f(expr) */
2241 arg_subsc = arg;
2242 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 'g': list_glob_vars(first); break;
2251 case 'b': list_buf_vars(first); break;
2252 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002253#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 'v': list_vim_vars(first); break;
2257 case 's': list_script_vars(first); break;
2258 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 default:
2260 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
2263 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 {
2265 char_u numbuf[NUMBUFLEN];
2266 char_u *tf;
2267 int c;
2268 char_u *s;
2269
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002270 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 c = *arg;
2272 *arg = NUL;
2273 list_one_var_a((char_u *)"",
2274 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002275 tv.v_type,
2276 s == NULL ? (char_u *)"" : s,
2277 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 *arg = c;
2279 vim_free(tf);
2280 }
2281 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285
2286 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288
2289 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 }
2291
2292 return arg;
2293}
2294
2295/*
2296 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2297 * Returns a pointer to the char just after the var name.
2298 * Returns NULL if there is an error.
2299 */
2300 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002303 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002305 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307{
2308 int c1;
2309 char_u *name;
2310 char_u *p;
2311 char_u *arg_end = NULL;
2312 int len;
2313 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315
2316 /*
2317 * ":let $VAR = expr": Set environment variable.
2318 */
2319 if (*arg == '$')
2320 {
2321 /* Find the end of the name. */
2322 ++arg;
2323 name = arg;
2324 len = get_env_len(&arg);
2325 if (len == 0)
2326 EMSG2(_(e_invarg2), name - 1);
2327 else
2328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 if (op != NULL && (*op == '+' || *op == '-'))
2330 EMSG2(_(e_letwrong), op);
2331 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002332 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002334 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 {
2336 c1 = name[len];
2337 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 p = get_tv_string_chk(tv);
2339 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 {
2341 int mustfree = FALSE;
2342 char_u *s = vim_getenv(name, &mustfree);
2343
2344 if (s != NULL)
2345 {
2346 p = tofree = concat_str(s, p);
2347 if (mustfree)
2348 vim_free(s);
2349 }
2350 }
2351 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 if (STRICMP(name, "HOME") == 0)
2355 init_homedir();
2356 else if (didset_vim && STRICMP(name, "VIM") == 0)
2357 didset_vim = FALSE;
2358 else if (didset_vimruntime
2359 && STRICMP(name, "VIMRUNTIME") == 0)
2360 didset_vimruntime = FALSE;
2361 arg_end = arg;
2362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
2367 }
2368
2369 /*
2370 * ":let &option = expr": Set option value.
2371 * ":let &l:option = expr": Set local option value.
2372 * ":let &g:option = expr": Set global option value.
2373 */
2374 else if (*arg == '&')
2375 {
2376 /* Find the end of the name. */
2377 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 if (p == NULL || (endchars != NULL
2379 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 EMSG(_(e_letunexp));
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 long n;
2384 int opt_type;
2385 long numval;
2386 char_u *stringval = NULL;
2387 char_u *s;
2388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 c1 = *p;
2390 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391
2392 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 s = get_tv_string_chk(tv); /* != NULL if number or string */
2394 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 {
2396 opt_type = get_option_value(arg, &numval,
2397 &stringval, opt_flags);
2398 if ((opt_type == 1 && *op == '.')
2399 || (opt_type == 0 && *op != '.'))
2400 EMSG2(_(e_letwrong), op);
2401 else
2402 {
2403 if (opt_type == 1) /* number */
2404 {
2405 if (*op == '+')
2406 n = numval + n;
2407 else
2408 n = numval - n;
2409 }
2410 else if (opt_type == 0 && stringval != NULL) /* string */
2411 {
2412 s = concat_str(stringval, s);
2413 vim_free(stringval);
2414 stringval = s;
2415 }
2416 }
2417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 if (s != NULL)
2419 {
2420 set_option_value(arg, n, s, opt_flags);
2421 arg_end = p;
2422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 }
2426 }
2427
2428 /*
2429 * ":let @r = expr": Set register contents.
2430 */
2431 else if (*arg == '@')
2432 {
2433 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 if (op != NULL && (*op == '+' || *op == '-'))
2435 EMSG2(_(e_letwrong), op);
2436 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 EMSG(_(e_letunexp));
2439 else
2440 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002441 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 char_u *s;
2443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 p = get_tv_string_chk(tv);
2445 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002447 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (s != NULL)
2449 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002450 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(s);
2452 }
2453 }
2454 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 arg_end = arg + 1;
2458 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002459 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 }
2461 }
2462
2463 /*
2464 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002469 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002471 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 arg_end = p;
2480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484
2485 else
2486 EMSG2(_(e_invarg2), arg);
2487
2488 return arg_end;
2489}
2490
2491/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2493 */
2494 static int
2495check_changedtick(arg)
2496 char_u *arg;
2497{
2498 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2499 {
2500 EMSG2(_(e_readonlyvar), arg);
2501 return TRUE;
2502 }
2503 return FALSE;
2504}
2505
2506/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 * Get an lval: variable, Dict item or List item that can be assigned a value
2508 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2509 * "name.key", "name.key[expr]" etc.
2510 * Indexing only works if "name" is an existing List or Dictionary.
2511 * "name" points to the start of the name.
2512 * If "rettv" is not NULL it points to the value to be assigned.
2513 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2514 * wrong; must end in space or cmd separator.
2515 *
2516 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002517 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 */
2520 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002521get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 typval_T *rettv;
2524 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 int unlet;
2526 int skip;
2527 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002528 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 char_u *expr_start, *expr_end;
2532 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 dictitem_T *v;
2534 typval_T var1;
2535 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544
2545 if (skip)
2546 {
2547 /* When skipping just find the end of the name. */
2548 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 }
2551
2552 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (expr_start != NULL)
2555 {
2556 /* Don't expand the name when we already know there is an error. */
2557 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2558 && *p != '[' && *p != '.')
2559 {
2560 EMSG(_(e_trailing));
2561 return NULL;
2562 }
2563
2564 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2565 if (lp->ll_exp_name == NULL)
2566 {
2567 /* Report an invalid expression in braces, unless the
2568 * expression evaluation has been cancelled due to an
2569 * aborting error, an interrupt, or an exception. */
2570 if (!aborting() && !quiet)
2571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002572 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 EMSG2(_(e_invarg2), name);
2574 return NULL;
2575 }
2576 }
2577 lp->ll_name = lp->ll_exp_name;
2578 }
2579 else
2580 lp->ll_name = name;
2581
2582 /* Without [idx] or .key we are done. */
2583 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2584 return p;
2585
2586 cc = *p;
2587 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002588 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (v == NULL && !quiet)
2590 EMSG2(_(e_undefvar), lp->ll_name);
2591 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 if (v == NULL)
2593 return NULL;
2594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /*
2596 * Loop until no more [idx] or .key is following.
2597 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2602 && !(lp->ll_tv->v_type == VAR_DICT
2603 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_("E689: Can only index a List or Dictionary"));
2607 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!quiet)
2612 EMSG(_("E708: [:] must come last"));
2613 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 len = -1;
2617 if (*p == '.')
2618 {
2619 key = p + 1;
2620 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2621 ;
2622 if (len == 0)
2623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_(e_emptykey));
2626 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 }
2628 p = key + len;
2629 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 else
2631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 if (*p == ':')
2635 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 else
2637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 empty1 = FALSE;
2639 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002641 if (get_tv_string_chk(&var1) == NULL)
2642 {
2643 /* not a number or string */
2644 clear_tv(&var1);
2645 return NULL;
2646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 }
2648
2649 /* Optionally get the second index [ :expr]. */
2650 if (*p == ':')
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656 if (!empty1)
2657 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (rettv != NULL && (rettv->v_type != VAR_LIST
2661 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (!empty1)
2666 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = skipwhite(p + 1);
2670 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 else
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (!empty1)
2678 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002681 if (get_tv_string_chk(&var2) == NULL)
2682 {
2683 /* not a number or string */
2684 if (!empty1)
2685 clear_tv(&var1);
2686 clear_tv(&var2);
2687 return NULL;
2688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (!quiet)
2698 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705
2706 /* Skip to past ']'. */
2707 ++p;
2708 }
2709
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
2712 if (len == -1)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002715 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (*key == NUL)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 lp->ll_list = NULL;
2725 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002726 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002727
2728 /* When assigning to g: check that a function and variable name is
2729 * valid. */
2730 if (rettv != NULL && lp->ll_dict == &globvardict)
2731 {
2732 if (rettv->v_type == VAR_FUNC
2733 && var_check_func_name(key, lp->ll_di == NULL))
2734 return NULL;
2735 if (!valid_varname(key))
2736 return NULL;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741 /* Can't add "v:" variable. */
2742 if (lp->ll_dict == &vimvardict)
2743 {
2744 EMSG2(_(e_illvar), name);
2745 return NULL;
2746 }
2747
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (len == -1)
2754 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 }
2757 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (len == -1)
2762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 p = NULL;
2765 break;
2766 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 /* existing variable, need to check if it can be changed */
2768 else if (var_check_ro(lp->ll_di->di_flags, name))
2769 return NULL;
2770
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (len == -1)
2772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775 else
2776 {
2777 /*
2778 * Get the number and item for the only or first index of the List.
2779 */
2780 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 else
2783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 clear_tv(&var1);
2786 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_list = lp->ll_tv->vval.v_list;
2789 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2790 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002792 if (lp->ll_n1 < 0)
2793 {
2794 lp->ll_n1 = 0;
2795 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2796 }
2797 }
2798 if (lp->ll_li == NULL)
2799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002802 if (!quiet)
2803 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 }
2806
2807 /*
2808 * May need to find the item or absolute index for the second
2809 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 * When no index given: "lp->ll_empty2" is TRUE.
2811 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002815 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002821 {
2822 if (!quiet)
2823 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002825 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 }
2828
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2830 if (lp->ll_n1 < 0)
2831 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2832 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 {
2834 if (!quiet)
2835 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002838 }
2839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 return p;
2845}
2846
2847/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002848 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 */
2850 static void
2851clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002852 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853{
2854 vim_free(lp->ll_exp_name);
2855 vim_free(lp->ll_newkey);
2856}
2857
2858/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002859 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002861 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 */
2863 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002865 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870{
2871 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 listitem_T *ri;
2873 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874
2875 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 cc = *endp;
2880 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 if (op != NULL && *op != '=')
2882 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002885 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002886 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002887 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 {
2889 if (tv_op(&tv, rettv, op) == OK)
2890 set_var(lp->ll_name, &tv, FALSE);
2891 clear_tv(&tv);
2892 }
2893 }
2894 else
2895 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002899 else if (tv_check_lock(lp->ll_newkey == NULL
2900 ? lp->ll_tv->v_lock
2901 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2902 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 else if (lp->ll_range)
2904 {
2905 /*
2906 * Assign the List values to the list items.
2907 */
2908 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002909 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 if (op != NULL && *op != '=')
2911 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2912 else
2913 {
2914 clear_tv(&lp->ll_li->li_tv);
2915 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 ri = ri->li_next;
2918 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2919 break;
2920 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002923 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 ri = NULL;
2926 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002927 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 lp->ll_li = lp->ll_li->li_next;
2930 ++lp->ll_n1;
2931 }
2932 if (ri != NULL)
2933 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002934 else if (lp->ll_empty2
2935 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 : lp->ll_n1 != lp->ll_n2)
2937 EMSG(_("E711: List value has not enough items"));
2938 }
2939 else
2940 {
2941 /*
2942 * Assign to a List or Dictionary item.
2943 */
2944 if (lp->ll_newkey != NULL)
2945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 {
2948 EMSG2(_(e_letwrong), op);
2949 return;
2950 }
2951
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002953 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 if (di == NULL)
2955 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002956 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2957 {
2958 vim_free(di);
2959 return;
2960 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 else if (op != NULL && *op != '=')
2964 {
2965 tv_op(lp->ll_tv, rettv, op);
2966 return;
2967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002970
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 /*
2972 * Assign the value to the variable or list item.
2973 */
2974 if (copy)
2975 copy_tv(rettv, lp->ll_tv);
2976 else
2977 {
2978 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002979 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 }
2982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983}
2984
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002985/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2987 * Returns OK or FAIL.
2988 */
2989 static int
2990tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002991 typval_T *tv1;
2992 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 char_u *op;
2994{
2995 long n;
2996 char_u numbuf[NUMBUFLEN];
2997 char_u *s;
2998
2999 /* Can't do anything with a Funcref or a Dict on the right. */
3000 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3001 {
3002 switch (tv1->v_type)
3003 {
3004 case VAR_DICT:
3005 case VAR_FUNC:
3006 break;
3007
3008 case VAR_LIST:
3009 if (*op != '+' || tv2->v_type != VAR_LIST)
3010 break;
3011 /* List += List */
3012 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3013 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3014 return OK;
3015
3016 case VAR_NUMBER:
3017 case VAR_STRING:
3018 if (tv2->v_type == VAR_LIST)
3019 break;
3020 if (*op == '+' || *op == '-')
3021 {
3022 /* nr += nr or nr -= nr*/
3023 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024#ifdef FEAT_FLOAT
3025 if (tv2->v_type == VAR_FLOAT)
3026 {
3027 float_T f = n;
3028
3029 if (*op == '+')
3030 f += tv2->vval.v_float;
3031 else
3032 f -= tv2->vval.v_float;
3033 clear_tv(tv1);
3034 tv1->v_type = VAR_FLOAT;
3035 tv1->vval.v_float = f;
3036 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038#endif
3039 {
3040 if (*op == '+')
3041 n += get_tv_number(tv2);
3042 else
3043 n -= get_tv_number(tv2);
3044 clear_tv(tv1);
3045 tv1->v_type = VAR_NUMBER;
3046 tv1->vval.v_number = n;
3047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 }
3049 else
3050 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051 if (tv2->v_type == VAR_FLOAT)
3052 break;
3053
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 /* str .= str */
3055 s = get_tv_string(tv1);
3056 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3057 clear_tv(tv1);
3058 tv1->v_type = VAR_STRING;
3059 tv1->vval.v_string = s;
3060 }
3061 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003062
3063#ifdef FEAT_FLOAT
3064 case VAR_FLOAT:
3065 {
3066 float_T f;
3067
3068 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3069 && tv2->v_type != VAR_NUMBER
3070 && tv2->v_type != VAR_STRING))
3071 break;
3072 if (tv2->v_type == VAR_FLOAT)
3073 f = tv2->vval.v_float;
3074 else
3075 f = get_tv_number(tv2);
3076 if (*op == '+')
3077 tv1->vval.v_float += f;
3078 else
3079 tv1->vval.v_float -= f;
3080 }
3081 return OK;
3082#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 }
3084 }
3085
3086 EMSG2(_(e_letwrong), op);
3087 return FAIL;
3088}
3089
3090/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091 * Add a watcher to a list.
3092 */
3093 static void
3094list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003095 list_T *l;
3096 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097{
3098 lw->lw_next = l->lv_watch;
3099 l->lv_watch = lw;
3100}
3101
3102/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003103 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104 * No warning when it isn't found...
3105 */
3106 static void
3107list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 list_T *l;
3109 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110{
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112
3113 lwp = &l->lv_watch;
3114 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3115 {
3116 if (lw == lwrem)
3117 {
3118 *lwp = lw->lw_next;
3119 break;
3120 }
3121 lwp = &lw->lw_next;
3122 }
3123}
3124
3125/*
3126 * Just before removing an item from a list: advance watchers to the next
3127 * item.
3128 */
3129 static void
3130list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 list_T *l;
3132 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133{
Bram Moolenaar33570922005-01-25 22:26:29 +00003134 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135
3136 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3137 if (lw->lw_item == item)
3138 lw->lw_item = item->li_next;
3139}
3140
3141/*
3142 * Evaluate the expression used in a ":for var in expr" command.
3143 * "arg" points to "var".
3144 * Set "*errp" to TRUE for an error, FALSE otherwise;
3145 * Return a pointer that holds the info. Null when there is an error.
3146 */
3147 void *
3148eval_for_line(arg, errp, nextcmdp, skip)
3149 char_u *arg;
3150 int *errp;
3151 char_u **nextcmdp;
3152 int skip;
3153{
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 typval_T tv;
3157 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158
3159 *errp = TRUE; /* default: there is an error */
3160
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 if (fi == NULL)
3163 return NULL;
3164
3165 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3166 if (expr == NULL)
3167 return fi;
3168
3169 expr = skipwhite(expr);
3170 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3171 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003172 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 return fi;
3174 }
3175
3176 if (skip)
3177 ++emsg_skip;
3178 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3179 {
3180 *errp = FALSE;
3181 if (!skip)
3182 {
3183 l = tv.vval.v_list;
3184 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003185 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003187 clear_tv(&tv);
3188 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 else
3190 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003191 /* No need to increment the refcount, it's already set for the
3192 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 fi->fi_list = l;
3194 list_add_watch(l, &fi->fi_lw);
3195 fi->fi_lw.lw_item = l->lv_first;
3196 }
3197 }
3198 }
3199 if (skip)
3200 --emsg_skip;
3201
3202 return fi;
3203}
3204
3205/*
3206 * Use the first item in a ":for" list. Advance to the next.
3207 * Assign the values to the variable (list). "arg" points to the first one.
3208 * Return TRUE when a valid item was found, FALSE when at end of list or
3209 * something wrong.
3210 */
3211 int
3212next_for_item(fi_void, arg)
3213 void *fi_void;
3214 char_u *arg;
3215{
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219
3220 item = fi->fi_lw.lw_item;
3221 if (item == NULL)
3222 result = FALSE;
3223 else
3224 {
3225 fi->fi_lw.lw_item = item->li_next;
3226 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3227 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3228 }
3229 return result;
3230}
3231
3232/*
3233 * Free the structure used to store info used by ":for".
3234 */
3235 void
3236free_for_info(fi_void)
3237 void *fi_void;
3238{
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003241 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003242 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 list_unref(fi->fi_list);
3245 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 vim_free(fi);
3247}
3248
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3250
3251 void
3252set_context_for_expression(xp, arg, cmdidx)
3253 expand_T *xp;
3254 char_u *arg;
3255 cmdidx_T cmdidx;
3256{
3257 int got_eq = FALSE;
3258 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 if (cmdidx == CMD_let)
3262 {
3263 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003264 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 {
3266 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003267 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 {
3269 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 if (vim_iswhite(*p))
3272 break;
3273 }
3274 return;
3275 }
3276 }
3277 else
3278 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3279 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 while ((xp->xp_pattern = vim_strpbrk(arg,
3281 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3282 {
3283 c = *xp->xp_pattern;
3284 if (c == '&')
3285 {
3286 c = xp->xp_pattern[1];
3287 if (c == '&')
3288 {
3289 ++xp->xp_pattern;
3290 xp->xp_context = cmdidx != CMD_let || got_eq
3291 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3292 }
3293 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003296 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3297 xp->xp_pattern += 2;
3298
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 else if (c == '$')
3302 {
3303 /* environment variable */
3304 xp->xp_context = EXPAND_ENV_VARS;
3305 }
3306 else if (c == '=')
3307 {
3308 got_eq = TRUE;
3309 xp->xp_context = EXPAND_EXPRESSION;
3310 }
3311 else if (c == '<'
3312 && xp->xp_context == EXPAND_FUNCTIONS
3313 && vim_strchr(xp->xp_pattern, '(') == NULL)
3314 {
3315 /* Function name can start with "<SNR>" */
3316 break;
3317 }
3318 else if (cmdidx != CMD_let || got_eq)
3319 {
3320 if (c == '"') /* string */
3321 {
3322 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3323 if (c == '\\' && xp->xp_pattern[1] != NUL)
3324 ++xp->xp_pattern;
3325 xp->xp_context = EXPAND_NOTHING;
3326 }
3327 else if (c == '\'') /* literal string */
3328 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003329 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3331 /* skip */ ;
3332 xp->xp_context = EXPAND_NOTHING;
3333 }
3334 else if (c == '|')
3335 {
3336 if (xp->xp_pattern[1] == '|')
3337 {
3338 ++xp->xp_pattern;
3339 xp->xp_context = EXPAND_EXPRESSION;
3340 }
3341 else
3342 xp->xp_context = EXPAND_COMMANDS;
3343 }
3344 else
3345 xp->xp_context = EXPAND_EXPRESSION;
3346 }
3347 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 /* Doesn't look like something valid, expand as an expression
3349 * anyway. */
3350 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 arg = xp->xp_pattern;
3352 if (*arg != NUL)
3353 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3354 /* skip */ ;
3355 }
3356 xp->xp_pattern = arg;
3357}
3358
3359#endif /* FEAT_CMDL_COMPL */
3360
3361/*
3362 * ":1,25call func(arg1, arg2)" function call.
3363 */
3364 void
3365ex_call(eap)
3366 exarg_T *eap;
3367{
3368 char_u *arg = eap->arg;
3369 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003371 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 linenr_T lnum;
3375 int doesrange;
3376 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003377 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003379 if (eap->skip)
3380 {
3381 /* trans_function_name() doesn't work well when skipping, use eval0()
3382 * instead to skip to any following command, e.g. for:
3383 * :if 0 | call dict.foo().bar() | endif */
3384 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3385 return;
3386 }
3387
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003388 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003389 if (fudi.fd_newkey != NULL)
3390 {
3391 /* Still need to give an error message for missing key. */
3392 EMSG2(_(e_dictkey), fudi.fd_newkey);
3393 vim_free(fudi.fd_newkey);
3394 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003395 if (tofree == NULL)
3396 return;
3397
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003398 /* Increase refcount on dictionary, it could get deleted when evaluating
3399 * the arguments. */
3400 if (fudi.fd_dict != NULL)
3401 ++fudi.fd_dict->dv_refcount;
3402
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003403 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003404 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003405 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaar532c7802005-01-27 14:44:31 +00003407 /* Skip white space to allow ":call func ()". Not good, but required for
3408 * backward compatibility. */
3409 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003410 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 if (*startarg != '(')
3413 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003414 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 goto end;
3416 }
3417
3418 /*
3419 * When skipping, evaluate the function once, to find the end of the
3420 * arguments.
3421 * When the function takes a range, this is discovered after the first
3422 * call, and the loop is broken.
3423 */
3424 if (eap->skip)
3425 {
3426 ++emsg_skip;
3427 lnum = eap->line2; /* do it once, also with an invalid range */
3428 }
3429 else
3430 lnum = eap->line1;
3431 for ( ; lnum <= eap->line2; ++lnum)
3432 {
3433 if (!eap->skip && eap->addr_count > 0)
3434 {
3435 curwin->w_cursor.lnum = lnum;
3436 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003437#ifdef FEAT_VIRTUALEDIT
3438 curwin->w_cursor.coladd = 0;
3439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003442 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003443 eap->line1, eap->line2, &doesrange,
3444 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 failed = TRUE;
3447 break;
3448 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003449
3450 /* Handle a function returning a Funcref, Dictionary or List. */
3451 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3452 {
3453 failed = TRUE;
3454 break;
3455 }
3456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003457 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (doesrange || eap->skip)
3459 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003460
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003462 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003463 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003464 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (aborting())
3466 break;
3467 }
3468 if (eap->skip)
3469 --emsg_skip;
3470
3471 if (!failed)
3472 {
3473 /* Check for trailing illegal characters and a following command. */
3474 if (!ends_excmd(*arg))
3475 {
3476 emsg_severe = TRUE;
3477 EMSG(_(e_trailing));
3478 }
3479 else
3480 eap->nextcmd = check_nextcmd(arg);
3481 }
3482
3483end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003484 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486}
3487
3488/*
3489 * ":unlet[!] var1 ... " command.
3490 */
3491 void
3492ex_unlet(eap)
3493 exarg_T *eap;
3494{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003495 ex_unletlock(eap, eap->arg, 0);
3496}
3497
3498/*
3499 * ":lockvar" and ":unlockvar" commands
3500 */
3501 void
3502ex_lockvar(eap)
3503 exarg_T *eap;
3504{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003506 int deep = 2;
3507
3508 if (eap->forceit)
3509 deep = -1;
3510 else if (vim_isdigit(*arg))
3511 {
3512 deep = getdigits(&arg);
3513 arg = skipwhite(arg);
3514 }
3515
3516 ex_unletlock(eap, arg, deep);
3517}
3518
3519/*
3520 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3521 */
3522 static void
3523ex_unletlock(eap, argstart, deep)
3524 exarg_T *eap;
3525 char_u *argstart;
3526 int deep;
3527{
3528 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003531 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
3533 do
3534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003535 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003536 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3537 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 if (lv.ll_name == NULL)
3539 error = TRUE; /* error but continue parsing */
3540 if (name_end == NULL || (!vim_iswhite(*name_end)
3541 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003543 if (name_end != NULL)
3544 {
3545 emsg_severe = TRUE;
3546 EMSG(_(e_trailing));
3547 }
3548 if (!(eap->skip || error))
3549 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 break;
3551 }
3552
3553 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 {
3555 if (eap->cmdidx == CMD_unlet)
3556 {
3557 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3558 error = TRUE;
3559 }
3560 else
3561 {
3562 if (do_lock_var(&lv, name_end, deep,
3563 eap->cmdidx == CMD_lockvar) == FAIL)
3564 error = TRUE;
3565 }
3566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 if (!eap->skip)
3569 clear_lval(&lv);
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 arg = skipwhite(name_end);
3572 } while (!ends_excmd(*arg));
3573
3574 eap->nextcmd = check_nextcmd(arg);
3575}
3576
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003578do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003579 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003580 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 int forceit;
3582{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003583 int ret = OK;
3584 int cc;
3585
3586 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 cc = *name_end;
3589 *name_end = NUL;
3590
3591 /* Normal name or expanded name. */
3592 if (check_changedtick(lp->ll_name))
3593 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3599 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 else if (lp->ll_range)
3601 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003602 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603
3604 /* Delete a range of List items. */
3605 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3606 {
3607 li = lp->ll_li->li_next;
3608 listitem_remove(lp->ll_list, lp->ll_li);
3609 lp->ll_li = li;
3610 ++lp->ll_n1;
3611 }
3612 }
3613 else
3614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 /* unlet a List item. */
3617 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003620 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 }
3622
3623 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624}
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626/*
3627 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 */
3630 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003631do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003633 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
Bram Moolenaar33570922005-01-25 22:26:29 +00003635 hashtab_T *ht;
3636 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003637 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003638 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
Bram Moolenaar33570922005-01-25 22:26:29 +00003640 ht = find_var_ht(name, &varname);
3641 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003643 hi = hash_find(ht, varname);
3644 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003645 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003646 di = HI2DI(hi);
3647 if (var_check_fixed(di->di_flags, name)
3648 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649 return FAIL;
3650 delete_var(ht, hi);
3651 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003654 if (forceit)
3655 return OK;
3656 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 return FAIL;
3658}
3659
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660/*
3661 * Lock or unlock variable indicated by "lp".
3662 * "deep" is the levels to go (-1 for unlimited);
3663 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3664 */
3665 static int
3666do_lock_var(lp, name_end, deep, lock)
3667 lval_T *lp;
3668 char_u *name_end;
3669 int deep;
3670 int lock;
3671{
3672 int ret = OK;
3673 int cc;
3674 dictitem_T *di;
3675
3676 if (deep == 0) /* nothing to do */
3677 return OK;
3678
3679 if (lp->ll_tv == NULL)
3680 {
3681 cc = *name_end;
3682 *name_end = NUL;
3683
3684 /* Normal name or expanded name. */
3685 if (check_changedtick(lp->ll_name))
3686 ret = FAIL;
3687 else
3688 {
3689 di = find_var(lp->ll_name, NULL);
3690 if (di == NULL)
3691 ret = FAIL;
3692 else
3693 {
3694 if (lock)
3695 di->di_flags |= DI_FLAGS_LOCK;
3696 else
3697 di->di_flags &= ~DI_FLAGS_LOCK;
3698 item_lock(&di->di_tv, deep, lock);
3699 }
3700 }
3701 *name_end = cc;
3702 }
3703 else if (lp->ll_range)
3704 {
3705 listitem_T *li = lp->ll_li;
3706
3707 /* (un)lock a range of List items. */
3708 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3709 {
3710 item_lock(&li->li_tv, deep, lock);
3711 li = li->li_next;
3712 ++lp->ll_n1;
3713 }
3714 }
3715 else if (lp->ll_list != NULL)
3716 /* (un)lock a List item. */
3717 item_lock(&lp->ll_li->li_tv, deep, lock);
3718 else
3719 /* un(lock) a Dictionary item. */
3720 item_lock(&lp->ll_di->di_tv, deep, lock);
3721
3722 return ret;
3723}
3724
3725/*
3726 * Lock or unlock an item. "deep" is nr of levels to go.
3727 */
3728 static void
3729item_lock(tv, deep, lock)
3730 typval_T *tv;
3731 int deep;
3732 int lock;
3733{
3734 static int recurse = 0;
3735 list_T *l;
3736 listitem_T *li;
3737 dict_T *d;
3738 hashitem_T *hi;
3739 int todo;
3740
3741 if (recurse >= DICT_MAXNEST)
3742 {
3743 EMSG(_("E743: variable nested too deep for (un)lock"));
3744 return;
3745 }
3746 if (deep == 0)
3747 return;
3748 ++recurse;
3749
3750 /* lock/unlock the item itself */
3751 if (lock)
3752 tv->v_lock |= VAR_LOCKED;
3753 else
3754 tv->v_lock &= ~VAR_LOCKED;
3755
3756 switch (tv->v_type)
3757 {
3758 case VAR_LIST:
3759 if ((l = tv->vval.v_list) != NULL)
3760 {
3761 if (lock)
3762 l->lv_lock |= VAR_LOCKED;
3763 else
3764 l->lv_lock &= ~VAR_LOCKED;
3765 if (deep < 0 || deep > 1)
3766 /* recursive: lock/unlock the items the List contains */
3767 for (li = l->lv_first; li != NULL; li = li->li_next)
3768 item_lock(&li->li_tv, deep - 1, lock);
3769 }
3770 break;
3771 case VAR_DICT:
3772 if ((d = tv->vval.v_dict) != NULL)
3773 {
3774 if (lock)
3775 d->dv_lock |= VAR_LOCKED;
3776 else
3777 d->dv_lock &= ~VAR_LOCKED;
3778 if (deep < 0 || deep > 1)
3779 {
3780 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003781 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003782 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3783 {
3784 if (!HASHITEM_EMPTY(hi))
3785 {
3786 --todo;
3787 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3788 }
3789 }
3790 }
3791 }
3792 }
3793 --recurse;
3794}
3795
Bram Moolenaara40058a2005-07-11 22:42:07 +00003796/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003797 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3798 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003799 */
3800 static int
3801tv_islocked(tv)
3802 typval_T *tv;
3803{
3804 return (tv->v_lock & VAR_LOCKED)
3805 || (tv->v_type == VAR_LIST
3806 && tv->vval.v_list != NULL
3807 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3808 || (tv->v_type == VAR_DICT
3809 && tv->vval.v_dict != NULL
3810 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3811}
3812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3814/*
3815 * Delete all "menutrans_" variables.
3816 */
3817 void
3818del_menutrans_vars()
3819{
Bram Moolenaar33570922005-01-25 22:26:29 +00003820 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003821 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Bram Moolenaar33570922005-01-25 22:26:29 +00003823 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003824 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003825 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003826 {
3827 if (!HASHITEM_EMPTY(hi))
3828 {
3829 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3831 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003832 }
3833 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835}
3836#endif
3837
3838#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3839
3840/*
3841 * Local string buffer for the next two functions to store a variable name
3842 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3843 * get_user_var_name().
3844 */
3845
3846static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3847
3848static char_u *varnamebuf = NULL;
3849static int varnamebuflen = 0;
3850
3851/*
3852 * Function to concatenate a prefix and a variable name.
3853 */
3854 static char_u *
3855cat_prefix_varname(prefix, name)
3856 int prefix;
3857 char_u *name;
3858{
3859 int len;
3860
3861 len = (int)STRLEN(name) + 3;
3862 if (len > varnamebuflen)
3863 {
3864 vim_free(varnamebuf);
3865 len += 10; /* some additional space */
3866 varnamebuf = alloc(len);
3867 if (varnamebuf == NULL)
3868 {
3869 varnamebuflen = 0;
3870 return NULL;
3871 }
3872 varnamebuflen = len;
3873 }
3874 *varnamebuf = prefix;
3875 varnamebuf[1] = ':';
3876 STRCPY(varnamebuf + 2, name);
3877 return varnamebuf;
3878}
3879
3880/*
3881 * Function given to ExpandGeneric() to obtain the list of user defined
3882 * (global/buffer/window/built-in) variable names.
3883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 char_u *
3885get_user_var_name(xp, idx)
3886 expand_T *xp;
3887 int idx;
3888{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003889 static long_u gdone;
3890 static long_u bdone;
3891 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003892#ifdef FEAT_WINDOWS
3893 static long_u tdone;
3894#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003895 static int vidx;
3896 static hashitem_T *hi;
3897 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
3899 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003900 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003901 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003902#ifdef FEAT_WINDOWS
3903 tdone = 0;
3904#endif
3905 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003906
3907 /* Global variables */
3908 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003910 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003912 else
3913 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003914 while (HASHITEM_EMPTY(hi))
3915 ++hi;
3916 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3917 return cat_prefix_varname('g', hi->hi_key);
3918 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003920
3921 /* b: variables */
3922 ht = &curbuf->b_vars.dv_hashtab;
3923 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003925 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003927 else
3928 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 while (HASHITEM_EMPTY(hi))
3930 ++hi;
3931 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003933 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 return (char_u *)"b:changedtick";
3937 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003938
3939 /* w: variables */
3940 ht = &curwin->w_vars.dv_hashtab;
3941 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003943 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003945 else
3946 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 while (HASHITEM_EMPTY(hi))
3948 ++hi;
3949 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003952#ifdef FEAT_WINDOWS
3953 /* t: variables */
3954 ht = &curtab->tp_vars.dv_hashtab;
3955 if (tdone < ht->ht_used)
3956 {
3957 if (tdone++ == 0)
3958 hi = ht->ht_array;
3959 else
3960 ++hi;
3961 while (HASHITEM_EMPTY(hi))
3962 ++hi;
3963 return cat_prefix_varname('t', hi->hi_key);
3964 }
3965#endif
3966
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 /* v: variables */
3968 if (vidx < VV_LEN)
3969 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
3971 vim_free(varnamebuf);
3972 varnamebuf = NULL;
3973 varnamebuflen = 0;
3974 return NULL;
3975}
3976
3977#endif /* FEAT_CMDL_COMPL */
3978
3979/*
3980 * types for expressions.
3981 */
3982typedef enum
3983{
3984 TYPE_UNKNOWN = 0
3985 , TYPE_EQUAL /* == */
3986 , TYPE_NEQUAL /* != */
3987 , TYPE_GREATER /* > */
3988 , TYPE_GEQUAL /* >= */
3989 , TYPE_SMALLER /* < */
3990 , TYPE_SEQUAL /* <= */
3991 , TYPE_MATCH /* =~ */
3992 , TYPE_NOMATCH /* !~ */
3993} exptype_T;
3994
3995/*
3996 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003997 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3999 */
4000
4001/*
4002 * Handle zero level expression.
4003 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004005 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 * Return OK or FAIL.
4007 */
4008 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004009eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 char_u **nextcmd;
4013 int evaluate;
4014{
4015 int ret;
4016 char_u *p;
4017
4018 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (ret == FAIL || !ends_excmd(*p))
4021 {
4022 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 /*
4025 * Report the invalid expression unless the expression evaluation has
4026 * been cancelled due to an aborting error, an interrupt, or an
4027 * exception.
4028 */
4029 if (!aborting())
4030 EMSG2(_(e_invexpr2), arg);
4031 ret = FAIL;
4032 }
4033 if (nextcmd != NULL)
4034 *nextcmd = check_nextcmd(p);
4035
4036 return ret;
4037}
4038
4039/*
4040 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004041 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 *
4043 * "arg" must point to the first non-white of the expression.
4044 * "arg" is advanced to the next non-white after the recognized expression.
4045 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004046 * Note: "rettv.v_lock" is not set.
4047 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 * Return OK or FAIL.
4049 */
4050 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 int evaluate;
4055{
4056 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 /*
4060 * Get the first variable.
4061 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return FAIL;
4064
4065 if ((*arg)[0] == '?')
4066 {
4067 result = FALSE;
4068 if (evaluate)
4069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004070 int error = FALSE;
4071
4072 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004075 if (error)
4076 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078
4079 /*
4080 * Get the second variable.
4081 */
4082 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 return FAIL;
4085
4086 /*
4087 * Check for the ":".
4088 */
4089 if ((*arg)[0] != ':')
4090 {
4091 EMSG(_("E109: Missing ':' after '?'"));
4092 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return FAIL;
4095 }
4096
4097 /*
4098 * Get the third variable.
4099 */
4100 *arg = skipwhite(*arg + 1);
4101 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4102 {
4103 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 return FAIL;
4106 }
4107 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110
4111 return OK;
4112}
4113
4114/*
4115 * Handle first level expression:
4116 * expr2 || expr2 || expr2 logical OR
4117 *
4118 * "arg" must point to the first non-white of the expression.
4119 * "arg" is advanced to the next non-white after the recognized expression.
4120 *
4121 * Return OK or FAIL.
4122 */
4123 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 int evaluate;
4128{
Bram Moolenaar33570922005-01-25 22:26:29 +00004129 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 long result;
4131 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004132 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134 /*
4135 * Get the first variable.
4136 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 return FAIL;
4139
4140 /*
4141 * Repeat until there is no following "||".
4142 */
4143 first = TRUE;
4144 result = FALSE;
4145 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4146 {
4147 if (evaluate && first)
4148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 if (error)
4153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 first = FALSE;
4155 }
4156
4157 /*
4158 * Get the second variable.
4159 */
4160 *arg = skipwhite(*arg + 2);
4161 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4162 return FAIL;
4163
4164 /*
4165 * Compute the result.
4166 */
4167 if (evaluate && !result)
4168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 if (error)
4173 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175 if (evaluate)
4176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 rettv->v_type = VAR_NUMBER;
4178 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180 }
4181
4182 return OK;
4183}
4184
4185/*
4186 * Handle second level expression:
4187 * expr3 && expr3 && expr3 logical AND
4188 *
4189 * "arg" must point to the first non-white of the expression.
4190 * "arg" is advanced to the next non-white after the recognized expression.
4191 *
4192 * Return OK or FAIL.
4193 */
4194 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 int evaluate;
4199{
Bram Moolenaar33570922005-01-25 22:26:29 +00004200 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 long result;
4202 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 /*
4206 * Get the first variable.
4207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210
4211 /*
4212 * Repeat until there is no following "&&".
4213 */
4214 first = TRUE;
4215 result = TRUE;
4216 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4217 {
4218 if (evaluate && first)
4219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004220 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (error)
4224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 first = FALSE;
4226 }
4227
4228 /*
4229 * Get the second variable.
4230 */
4231 *arg = skipwhite(*arg + 2);
4232 if (eval4(arg, &var2, evaluate && result) == FAIL)
4233 return FAIL;
4234
4235 /*
4236 * Compute the result.
4237 */
4238 if (evaluate && result)
4239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (error)
4244 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 if (evaluate)
4247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252
4253 return OK;
4254}
4255
4256/*
4257 * Handle third level expression:
4258 * var1 == var2
4259 * var1 =~ var2
4260 * var1 != var2
4261 * var1 !~ var2
4262 * var1 > var2
4263 * var1 >= var2
4264 * var1 < var2
4265 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004266 * var1 is var2
4267 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 *
4269 * "arg" must point to the first non-white of the expression.
4270 * "arg" is advanced to the next non-white after the recognized expression.
4271 *
4272 * Return OK or FAIL.
4273 */
4274 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int evaluate;
4279{
Bram Moolenaar33570922005-01-25 22:26:29 +00004280 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 char_u *p;
4282 int i;
4283 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 int len = 2;
4286 long n1, n2;
4287 char_u *s1, *s2;
4288 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4289 regmatch_T regmatch;
4290 int ic;
4291 char_u *save_cpo;
4292
4293 /*
4294 * Get the first variable.
4295 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 return FAIL;
4298
4299 p = *arg;
4300 switch (p[0])
4301 {
4302 case '=': if (p[1] == '=')
4303 type = TYPE_EQUAL;
4304 else if (p[1] == '~')
4305 type = TYPE_MATCH;
4306 break;
4307 case '!': if (p[1] == '=')
4308 type = TYPE_NEQUAL;
4309 else if (p[1] == '~')
4310 type = TYPE_NOMATCH;
4311 break;
4312 case '>': if (p[1] != '=')
4313 {
4314 type = TYPE_GREATER;
4315 len = 1;
4316 }
4317 else
4318 type = TYPE_GEQUAL;
4319 break;
4320 case '<': if (p[1] != '=')
4321 {
4322 type = TYPE_SMALLER;
4323 len = 1;
4324 }
4325 else
4326 type = TYPE_SEQUAL;
4327 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004328 case 'i': if (p[1] == 's')
4329 {
4330 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4331 len = 5;
4332 if (!vim_isIDc(p[len]))
4333 {
4334 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4335 type_is = TRUE;
4336 }
4337 }
4338 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340
4341 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004342 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 */
4344 if (type != TYPE_UNKNOWN)
4345 {
4346 /* extra question mark appended: ignore case */
4347 if (p[len] == '?')
4348 {
4349 ic = TRUE;
4350 ++len;
4351 }
4352 /* extra '#' appended: match case */
4353 else if (p[len] == '#')
4354 {
4355 ic = FALSE;
4356 ++len;
4357 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004358 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 else
4360 ic = p_ic;
4361
4362 /*
4363 * Get the second variable.
4364 */
4365 *arg = skipwhite(p + len);
4366 if (eval5(arg, &var2, evaluate) == FAIL)
4367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 return FAIL;
4370 }
4371
4372 if (evaluate)
4373 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 if (type_is && rettv->v_type != var2.v_type)
4375 {
4376 /* For "is" a different type always means FALSE, for "notis"
4377 * it means TRUE. */
4378 n1 = (type == TYPE_NEQUAL);
4379 }
4380 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4381 {
4382 if (type_is)
4383 {
4384 n1 = (rettv->v_type == var2.v_type
4385 && rettv->vval.v_list == var2.vval.v_list);
4386 if (type == TYPE_NEQUAL)
4387 n1 = !n1;
4388 }
4389 else if (rettv->v_type != var2.v_type
4390 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4391 {
4392 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004393 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004394 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004395 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 clear_tv(rettv);
4397 clear_tv(&var2);
4398 return FAIL;
4399 }
4400 else
4401 {
4402 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004403 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4404 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004405 if (type == TYPE_NEQUAL)
4406 n1 = !n1;
4407 }
4408 }
4409
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004410 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4411 {
4412 if (type_is)
4413 {
4414 n1 = (rettv->v_type == var2.v_type
4415 && rettv->vval.v_dict == var2.vval.v_dict);
4416 if (type == TYPE_NEQUAL)
4417 n1 = !n1;
4418 }
4419 else if (rettv->v_type != var2.v_type
4420 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4421 {
4422 if (rettv->v_type != var2.v_type)
4423 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4424 else
4425 EMSG(_("E736: Invalid operation for Dictionary"));
4426 clear_tv(rettv);
4427 clear_tv(&var2);
4428 return FAIL;
4429 }
4430 else
4431 {
4432 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004433 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4434 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 }
4439
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4441 {
4442 if (rettv->v_type != var2.v_type
4443 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4444 {
4445 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004446 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004448 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 clear_tv(rettv);
4450 clear_tv(&var2);
4451 return FAIL;
4452 }
4453 else
4454 {
4455 /* Compare two Funcrefs for being equal or unequal. */
4456 if (rettv->vval.v_string == NULL
4457 || var2.vval.v_string == NULL)
4458 n1 = FALSE;
4459 else
4460 n1 = STRCMP(rettv->vval.v_string,
4461 var2.vval.v_string) == 0;
4462 if (type == TYPE_NEQUAL)
4463 n1 = !n1;
4464 }
4465 }
4466
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004467#ifdef FEAT_FLOAT
4468 /*
4469 * If one of the two variables is a float, compare as a float.
4470 * When using "=~" or "!~", always compare as string.
4471 */
4472 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4473 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4474 {
4475 float_T f1, f2;
4476
4477 if (rettv->v_type == VAR_FLOAT)
4478 f1 = rettv->vval.v_float;
4479 else
4480 f1 = get_tv_number(rettv);
4481 if (var2.v_type == VAR_FLOAT)
4482 f2 = var2.vval.v_float;
4483 else
4484 f2 = get_tv_number(&var2);
4485 n1 = FALSE;
4486 switch (type)
4487 {
4488 case TYPE_EQUAL: n1 = (f1 == f2); break;
4489 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4490 case TYPE_GREATER: n1 = (f1 > f2); break;
4491 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4492 case TYPE_SMALLER: n1 = (f1 < f2); break;
4493 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4494 case TYPE_UNKNOWN:
4495 case TYPE_MATCH:
4496 case TYPE_NOMATCH: break; /* avoid gcc warning */
4497 }
4498 }
4499#endif
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 /*
4502 * If one of the two variables is a number, compare as a number.
4503 * When using "=~" or "!~", always compare as string.
4504 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004505 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004508 n1 = get_tv_number(rettv);
4509 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 switch (type)
4511 {
4512 case TYPE_EQUAL: n1 = (n1 == n2); break;
4513 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4514 case TYPE_GREATER: n1 = (n1 > n2); break;
4515 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4516 case TYPE_SMALLER: n1 = (n1 < n2); break;
4517 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4518 case TYPE_UNKNOWN:
4519 case TYPE_MATCH:
4520 case TYPE_NOMATCH: break; /* avoid gcc warning */
4521 }
4522 }
4523 else
4524 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004525 s1 = get_tv_string_buf(rettv, buf1);
4526 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4528 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4529 else
4530 i = 0;
4531 n1 = FALSE;
4532 switch (type)
4533 {
4534 case TYPE_EQUAL: n1 = (i == 0); break;
4535 case TYPE_NEQUAL: n1 = (i != 0); break;
4536 case TYPE_GREATER: n1 = (i > 0); break;
4537 case TYPE_GEQUAL: n1 = (i >= 0); break;
4538 case TYPE_SMALLER: n1 = (i < 0); break;
4539 case TYPE_SEQUAL: n1 = (i <= 0); break;
4540
4541 case TYPE_MATCH:
4542 case TYPE_NOMATCH:
4543 /* avoid 'l' flag in 'cpoptions' */
4544 save_cpo = p_cpo;
4545 p_cpo = (char_u *)"";
4546 regmatch.regprog = vim_regcomp(s2,
4547 RE_MAGIC + RE_STRING);
4548 regmatch.rm_ic = ic;
4549 if (regmatch.regprog != NULL)
4550 {
4551 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4552 vim_free(regmatch.regprog);
4553 if (type == TYPE_NOMATCH)
4554 n1 = !n1;
4555 }
4556 p_cpo = save_cpo;
4557 break;
4558
4559 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4560 }
4561 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 clear_tv(rettv);
4563 clear_tv(&var2);
4564 rettv->v_type = VAR_NUMBER;
4565 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 }
4568
4569 return OK;
4570}
4571
4572/*
4573 * Handle fourth level expression:
4574 * + number addition
4575 * - number subtraction
4576 * . string concatenation
4577 *
4578 * "arg" must point to the first non-white of the expression.
4579 * "arg" is advanced to the next non-white after the recognized expression.
4580 *
4581 * Return OK or FAIL.
4582 */
4583 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004584eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 int evaluate;
4588{
Bram Moolenaar33570922005-01-25 22:26:29 +00004589 typval_T var2;
4590 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 int op;
4592 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004593#ifdef FEAT_FLOAT
4594 float_T f1 = 0, f2 = 0;
4595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 char_u *s1, *s2;
4597 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4598 char_u *p;
4599
4600 /*
4601 * Get the first variable.
4602 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004603 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 return FAIL;
4605
4606 /*
4607 * Repeat computing, until no '+', '-' or '.' is following.
4608 */
4609 for (;;)
4610 {
4611 op = **arg;
4612 if (op != '+' && op != '-' && op != '.')
4613 break;
4614
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004615 if ((op != '+' || rettv->v_type != VAR_LIST)
4616#ifdef FEAT_FLOAT
4617 && (op == '.' || rettv->v_type != VAR_FLOAT)
4618#endif
4619 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004620 {
4621 /* For "list + ...", an illegal use of the first operand as
4622 * a number cannot be determined before evaluating the 2nd
4623 * operand: if this is also a list, all is ok.
4624 * For "something . ...", "something - ..." or "non-list + ...",
4625 * we know that the first operand needs to be a string or number
4626 * without evaluating the 2nd operand. So check before to avoid
4627 * side effects after an error. */
4628 if (evaluate && get_tv_string_chk(rettv) == NULL)
4629 {
4630 clear_tv(rettv);
4631 return FAIL;
4632 }
4633 }
4634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 /*
4636 * Get the second variable.
4637 */
4638 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004639 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 return FAIL;
4643 }
4644
4645 if (evaluate)
4646 {
4647 /*
4648 * Compute the result.
4649 */
4650 if (op == '.')
4651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4653 s2 = get_tv_string_buf_chk(&var2, buf2);
4654 if (s2 == NULL) /* type error ? */
4655 {
4656 clear_tv(rettv);
4657 clear_tv(&var2);
4658 return FAIL;
4659 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004660 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 clear_tv(rettv);
4662 rettv->v_type = VAR_STRING;
4663 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004665 else if (op == '+' && rettv->v_type == VAR_LIST
4666 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004667 {
4668 /* concatenate Lists */
4669 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4670 &var3) == FAIL)
4671 {
4672 clear_tv(rettv);
4673 clear_tv(&var2);
4674 return FAIL;
4675 }
4676 clear_tv(rettv);
4677 *rettv = var3;
4678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 else
4680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 int error = FALSE;
4682
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004683#ifdef FEAT_FLOAT
4684 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004686 f1 = rettv->vval.v_float;
4687 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689 else
4690#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004692 n1 = get_tv_number_chk(rettv, &error);
4693 if (error)
4694 {
4695 /* This can only happen for "list + non-list". For
4696 * "non-list + ..." or "something - ...", we returned
4697 * before evaluating the 2nd operand. */
4698 clear_tv(rettv);
4699 return FAIL;
4700 }
4701#ifdef FEAT_FLOAT
4702 if (var2.v_type == VAR_FLOAT)
4703 f1 = n1;
4704#endif
4705 }
4706#ifdef FEAT_FLOAT
4707 if (var2.v_type == VAR_FLOAT)
4708 {
4709 f2 = var2.vval.v_float;
4710 n2 = 0;
4711 }
4712 else
4713#endif
4714 {
4715 n2 = get_tv_number_chk(&var2, &error);
4716 if (error)
4717 {
4718 clear_tv(rettv);
4719 clear_tv(&var2);
4720 return FAIL;
4721 }
4722#ifdef FEAT_FLOAT
4723 if (rettv->v_type == VAR_FLOAT)
4724 f2 = n2;
4725#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004727 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728
4729#ifdef FEAT_FLOAT
4730 /* If there is a float on either side the result is a float. */
4731 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4732 {
4733 if (op == '+')
4734 f1 = f1 + f2;
4735 else
4736 f1 = f1 - f2;
4737 rettv->v_type = VAR_FLOAT;
4738 rettv->vval.v_float = f1;
4739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004741#endif
4742 {
4743 if (op == '+')
4744 n1 = n1 + n2;
4745 else
4746 n1 = n1 - n2;
4747 rettv->v_type = VAR_NUMBER;
4748 rettv->vval.v_number = n1;
4749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004751 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753 }
4754 return OK;
4755}
4756
4757/*
4758 * Handle fifth level expression:
4759 * * number multiplication
4760 * / number division
4761 * % number modulo
4762 *
4763 * "arg" must point to the first non-white of the expression.
4764 * "arg" is advanced to the next non-white after the recognized expression.
4765 *
4766 * Return OK or FAIL.
4767 */
4768 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004769eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004773 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
Bram Moolenaar33570922005-01-25 22:26:29 +00004775 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 int op;
4777 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004778#ifdef FEAT_FLOAT
4779 int use_float = FALSE;
4780 float_T f1 = 0, f2;
4781#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 /*
4785 * Get the first variable.
4786 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004787 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 return FAIL;
4789
4790 /*
4791 * Repeat computing, until no '*', '/' or '%' is following.
4792 */
4793 for (;;)
4794 {
4795 op = **arg;
4796 if (op != '*' && op != '/' && op != '%')
4797 break;
4798
4799 if (evaluate)
4800 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801#ifdef FEAT_FLOAT
4802 if (rettv->v_type == VAR_FLOAT)
4803 {
4804 f1 = rettv->vval.v_float;
4805 use_float = TRUE;
4806 n1 = 0;
4807 }
4808 else
4809#endif
4810 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 if (error)
4813 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 else
4816 n1 = 0;
4817
4818 /*
4819 * Get the second variable.
4820 */
4821 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004822 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return FAIL;
4824
4825 if (evaluate)
4826 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827#ifdef FEAT_FLOAT
4828 if (var2.v_type == VAR_FLOAT)
4829 {
4830 if (!use_float)
4831 {
4832 f1 = n1;
4833 use_float = TRUE;
4834 }
4835 f2 = var2.vval.v_float;
4836 n2 = 0;
4837 }
4838 else
4839#endif
4840 {
4841 n2 = get_tv_number_chk(&var2, &error);
4842 clear_tv(&var2);
4843 if (error)
4844 return FAIL;
4845#ifdef FEAT_FLOAT
4846 if (use_float)
4847 f2 = n2;
4848#endif
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 /*
4852 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004853 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004855#ifdef FEAT_FLOAT
4856 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004858 if (op == '*')
4859 f1 = f1 * f2;
4860 else if (op == '/')
4861 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004862# ifdef VMS
4863 /* VMS crashes on divide by zero, work around it */
4864 if (f2 == 0.0)
4865 {
4866 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004867 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004868 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004869 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004870 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004871 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004872 }
4873 else
4874 f1 = f1 / f2;
4875# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 /* We rely on the floating point library to handle divide
4877 * by zero to result in "inf" and not a crash. */
4878 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004879# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004883 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884 return FAIL;
4885 }
4886 rettv->v_type = VAR_FLOAT;
4887 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892 if (op == '*')
4893 n1 = n1 * n2;
4894 else if (op == '/')
4895 {
4896 if (n2 == 0) /* give an error message? */
4897 {
4898 if (n1 == 0)
4899 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4900 else if (n1 < 0)
4901 n1 = -0x7fffffffL;
4902 else
4903 n1 = 0x7fffffffL;
4904 }
4905 else
4906 n1 = n1 / n2;
4907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 {
4910 if (n2 == 0) /* give an error message? */
4911 n1 = 0;
4912 else
4913 n1 = n1 % n2;
4914 }
4915 rettv->v_type = VAR_NUMBER;
4916 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 }
4920
4921 return OK;
4922}
4923
4924/*
4925 * Handle sixth level expression:
4926 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004927 * "string" string constant
4928 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 * &option-name option value
4930 * @r register contents
4931 * identifier variable value
4932 * function() function call
4933 * $VAR environment variable
4934 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004935 * [expr, expr] List
4936 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 *
4938 * Also handle:
4939 * ! in front logical NOT
4940 * - in front unary minus
4941 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942 * trailing [] subscript in String or List
4943 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 *
4945 * "arg" must point to the first non-white of the expression.
4946 * "arg" is advanced to the next non-white after the recognized expression.
4947 *
4948 * Return OK or FAIL.
4949 */
4950 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004951eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004955 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 long n;
4958 int len;
4959 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 char_u *start_leader, *end_leader;
4961 int ret = OK;
4962 char_u *alias;
4963
4964 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004968 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /*
4971 * Skip '!' and '-' characters. They are handled later.
4972 */
4973 start_leader = *arg;
4974 while (**arg == '!' || **arg == '-' || **arg == '+')
4975 *arg = skipwhite(*arg + 1);
4976 end_leader = *arg;
4977
4978 switch (**arg)
4979 {
4980 /*
4981 * Number constant.
4982 */
4983 case '0':
4984 case '1':
4985 case '2':
4986 case '3':
4987 case '4':
4988 case '5':
4989 case '6':
4990 case '7':
4991 case '8':
4992 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 {
4994#ifdef FEAT_FLOAT
4995 char_u *p = skipdigits(*arg + 1);
4996 int get_float = FALSE;
4997
4998 /* We accept a float when the format matches
4999 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005000 * strict to avoid backwards compatibility problems.
5001 * Don't look for a float after the "." operator, so that
5002 * ":let vers = 1.2.3" doesn't fail. */
5003 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 get_float = TRUE;
5006 p = skipdigits(p + 2);
5007 if (*p == 'e' || *p == 'E')
5008 {
5009 ++p;
5010 if (*p == '-' || *p == '+')
5011 ++p;
5012 if (!vim_isdigit(*p))
5013 get_float = FALSE;
5014 else
5015 p = skipdigits(p + 1);
5016 }
5017 if (ASCII_ISALPHA(*p) || *p == '.')
5018 get_float = FALSE;
5019 }
5020 if (get_float)
5021 {
5022 float_T f;
5023
5024 *arg += string2float(*arg, &f);
5025 if (evaluate)
5026 {
5027 rettv->v_type = VAR_FLOAT;
5028 rettv->vval.v_float = f;
5029 }
5030 }
5031 else
5032#endif
5033 {
5034 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5035 *arg += len;
5036 if (evaluate)
5037 {
5038 rettv->v_type = VAR_NUMBER;
5039 rettv->vval.v_number = n;
5040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045 /*
5046 * String constant: "string".
5047 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005048 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 break;
5050
5051 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 break;
5056
5057 /*
5058 * List: [expr, expr]
5059 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 break;
5062
5063 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 * Dictionary: {key: val, key: val}
5065 */
5066 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5067 break;
5068
5069 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005070 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005072 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 break;
5074
5075 /*
5076 * Environment variable: $VAR.
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
5082 * Register contents: @r.
5083 */
5084 case '@': ++*arg;
5085 if (evaluate)
5086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005088 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090 if (**arg != NUL)
5091 ++*arg;
5092 break;
5093
5094 /*
5095 * nested expression: (expression).
5096 */
5097 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (**arg == ')')
5100 ++*arg;
5101 else if (ret == OK)
5102 {
5103 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 ret = FAIL;
5106 }
5107 break;
5108
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 break;
5111 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005112
5113 if (ret == NOTDONE)
5114 {
5115 /*
5116 * Must be a variable or function name.
5117 * Can also be a curly-braces kind of name: {expr}.
5118 */
5119 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005120 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 if (alias != NULL)
5122 s = alias;
5123
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005124 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125 ret = FAIL;
5126 else
5127 {
5128 if (**arg == '(') /* recursive! */
5129 {
5130 /* If "s" is the name of a variable of type VAR_FUNC
5131 * use its contents. */
5132 s = deref_func_name(s, &len);
5133
5134 /* Invoke the function. */
5135 ret = get_func_tv(s, len, rettv, arg,
5136 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005137 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 /* Stop the expression evaluation when immediately
5139 * aborting on error, or when an interrupt occurred or
5140 * an exception was thrown but not caught. */
5141 if (aborting())
5142 {
5143 if (ret == OK)
5144 clear_tv(rettv);
5145 ret = FAIL;
5146 }
5147 }
5148 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005150 else
5151 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005153 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 }
5155
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 *arg = skipwhite(*arg);
5157
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005158 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5159 * expr(expr). */
5160 if (ret == OK)
5161 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162
5163 /*
5164 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5165 */
5166 if (ret == OK && evaluate && end_leader > start_leader)
5167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005168 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005169 int val = 0;
5170#ifdef FEAT_FLOAT
5171 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005172
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005173 if (rettv->v_type == VAR_FLOAT)
5174 f = rettv->vval.v_float;
5175 else
5176#endif
5177 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005178 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005180 clear_tv(rettv);
5181 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005183 else
5184 {
5185 while (end_leader > start_leader)
5186 {
5187 --end_leader;
5188 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005189 {
5190#ifdef FEAT_FLOAT
5191 if (rettv->v_type == VAR_FLOAT)
5192 f = !f;
5193 else
5194#endif
5195 val = !val;
5196 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 {
5199#ifdef FEAT_FLOAT
5200 if (rettv->v_type == VAR_FLOAT)
5201 f = -f;
5202 else
5203#endif
5204 val = -val;
5205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005207#ifdef FEAT_FLOAT
5208 if (rettv->v_type == VAR_FLOAT)
5209 {
5210 clear_tv(rettv);
5211 rettv->vval.v_float = f;
5212 }
5213 else
5214#endif
5215 {
5216 clear_tv(rettv);
5217 rettv->v_type = VAR_NUMBER;
5218 rettv->vval.v_number = val;
5219 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
5222
5223 return ret;
5224}
5225
5226/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005227 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5228 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5230 */
5231 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005232eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005234 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237{
5238 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 long len = -1;
5242 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005245
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246 if (rettv->v_type == VAR_FUNC
5247#ifdef FEAT_FLOAT
5248 || rettv->v_type == VAR_FLOAT
5249#endif
5250 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005252 if (verbose)
5253 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 return FAIL;
5255 }
5256
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 /*
5260 * dict.name
5261 */
5262 key = *arg + 1;
5263 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5264 ;
5265 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 }
5269 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 /*
5272 * something[idx]
5273 *
5274 * Get the (first) variable from inside the [].
5275 */
5276 *arg = skipwhite(*arg + 1);
5277 if (**arg == ':')
5278 empty1 = TRUE;
5279 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5280 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5282 {
5283 /* not a number or string */
5284 clear_tv(&var1);
5285 return FAIL;
5286 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287
5288 /*
5289 * Get the second variable from inside the [:].
5290 */
5291 if (**arg == ':')
5292 {
5293 range = TRUE;
5294 *arg = skipwhite(*arg + 1);
5295 if (**arg == ']')
5296 empty2 = TRUE;
5297 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 if (!empty1)
5300 clear_tv(&var1);
5301 return FAIL;
5302 }
5303 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5304 {
5305 /* not a number or string */
5306 if (!empty1)
5307 clear_tv(&var1);
5308 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 return FAIL;
5310 }
5311 }
5312
5313 /* Check for the ']'. */
5314 if (**arg != ']')
5315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005316 if (verbose)
5317 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 clear_tv(&var1);
5319 if (range)
5320 clear_tv(&var2);
5321 return FAIL;
5322 }
5323 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 }
5325
5326 if (evaluate)
5327 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328 n1 = 0;
5329 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005331 n1 = get_tv_number(&var1);
5332 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 }
5334 if (range)
5335 {
5336 if (empty2)
5337 n2 = -1;
5338 else
5339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005340 n2 = get_tv_number(&var2);
5341 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 }
5343 }
5344
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005345 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 {
5347 case VAR_NUMBER:
5348 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 len = (long)STRLEN(s);
5351 if (range)
5352 {
5353 /* The resulting variable is a substring. If the indexes
5354 * are out of range the result is empty. */
5355 if (n1 < 0)
5356 {
5357 n1 = len + n1;
5358 if (n1 < 0)
5359 n1 = 0;
5360 }
5361 if (n2 < 0)
5362 n2 = len + n2;
5363 else if (n2 >= len)
5364 n2 = len;
5365 if (n1 >= len || n2 < 0 || n1 > n2)
5366 s = NULL;
5367 else
5368 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5369 }
5370 else
5371 {
5372 /* The resulting variable is a string of a single
5373 * character. If the index is too big or negative the
5374 * result is empty. */
5375 if (n1 >= len || n1 < 0)
5376 s = NULL;
5377 else
5378 s = vim_strnsave(s + n1, 1);
5379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005380 clear_tv(rettv);
5381 rettv->v_type = VAR_STRING;
5382 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 break;
5384
5385 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005386 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 if (n1 < 0)
5388 n1 = len + n1;
5389 if (!empty1 && (n1 < 0 || n1 >= len))
5390 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005391 /* For a range we allow invalid values and return an empty
5392 * list. A list index out of range is an error. */
5393 if (!range)
5394 {
5395 if (verbose)
5396 EMSGN(_(e_listidx), n1);
5397 return FAIL;
5398 }
5399 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 }
5401 if (range)
5402 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005403 list_T *l;
5404 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405
5406 if (n2 < 0)
5407 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005408 else if (n2 >= len)
5409 n2 = len - 1;
5410 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005411 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 l = list_alloc();
5413 if (l == NULL)
5414 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 n1 <= n2; ++n1)
5417 {
5418 if (list_append_tv(l, &item->li_tv) == FAIL)
5419 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005420 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 return FAIL;
5422 }
5423 item = item->li_next;
5424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 clear_tv(rettv);
5426 rettv->v_type = VAR_LIST;
5427 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005428 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 }
5430 else
5431 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005432 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 clear_tv(rettv);
5434 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 }
5436 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437
5438 case VAR_DICT:
5439 if (range)
5440 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005441 if (verbose)
5442 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 if (len == -1)
5444 clear_tv(&var1);
5445 return FAIL;
5446 }
5447 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005448 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005449
5450 if (len == -1)
5451 {
5452 key = get_tv_string(&var1);
5453 if (*key == NUL)
5454 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005455 if (verbose)
5456 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 clear_tv(&var1);
5458 return FAIL;
5459 }
5460 }
5461
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005462 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005464 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005465 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 if (len == -1)
5467 clear_tv(&var1);
5468 if (item == NULL)
5469 return FAIL;
5470
5471 copy_tv(&item->di_tv, &var1);
5472 clear_tv(rettv);
5473 *rettv = var1;
5474 }
5475 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 }
5477 }
5478
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 return OK;
5480}
5481
5482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 * Get an option value.
5484 * "arg" points to the '&' or '+' before the option name.
5485 * "arg" is advanced to character after the option name.
5486 * Return OK or FAIL.
5487 */
5488 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 int evaluate;
5493{
5494 char_u *option_end;
5495 long numval;
5496 char_u *stringval;
5497 int opt_type;
5498 int c;
5499 int working = (**arg == '+'); /* has("+option") */
5500 int ret = OK;
5501 int opt_flags;
5502
5503 /*
5504 * Isolate the option name and find its value.
5505 */
5506 option_end = find_option_end(arg, &opt_flags);
5507 if (option_end == NULL)
5508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005509 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 EMSG2(_("E112: Option name missing: %s"), *arg);
5511 return FAIL;
5512 }
5513
5514 if (!evaluate)
5515 {
5516 *arg = option_end;
5517 return OK;
5518 }
5519
5520 c = *option_end;
5521 *option_end = NUL;
5522 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 if (opt_type == -3) /* invalid name */
5526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 EMSG2(_("E113: Unknown option: %s"), *arg);
5529 ret = FAIL;
5530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 {
5533 if (opt_type == -2) /* hidden string option */
5534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 rettv->v_type = VAR_STRING;
5536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538 else if (opt_type == -1) /* hidden number option */
5539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 rettv->v_type = VAR_NUMBER;
5541 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543 else if (opt_type == 1) /* number option */
5544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 rettv->v_type = VAR_NUMBER;
5546 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 }
5548 else /* string option */
5549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 rettv->v_type = VAR_STRING;
5551 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553 }
5554 else if (working && (opt_type == -2 || opt_type == -1))
5555 ret = FAIL;
5556
5557 *option_end = c; /* put back for error messages */
5558 *arg = option_end;
5559
5560 return ret;
5561}
5562
5563/*
5564 * Allocate a variable for a string constant.
5565 * Return OK or FAIL.
5566 */
5567 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 int evaluate;
5572{
5573 char_u *p;
5574 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 int extra = 0;
5576
5577 /*
5578 * Find the end of the string, skipping backslashed characters.
5579 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005580 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 if (*p == '\\' && p[1] != NUL)
5583 {
5584 ++p;
5585 /* A "\<x>" form occupies at least 4 characters, and produces up
5586 * to 6 characters: reserve space for 2 extra */
5587 if (*p == '<')
5588 extra += 2;
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 }
5591
5592 if (*p != '"')
5593 {
5594 EMSG2(_("E114: Missing quote: %s"), *arg);
5595 return FAIL;
5596 }
5597
5598 /* If only parsing, set *arg and return here */
5599 if (!evaluate)
5600 {
5601 *arg = p + 1;
5602 return OK;
5603 }
5604
5605 /*
5606 * Copy the string into allocated memory, handling backslashed
5607 * characters.
5608 */
5609 name = alloc((unsigned)(p - *arg + extra));
5610 if (name == NULL)
5611 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 rettv->v_type = VAR_STRING;
5613 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 if (*p == '\\')
5618 {
5619 switch (*++p)
5620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621 case 'b': *name++ = BS; ++p; break;
5622 case 'e': *name++ = ESC; ++p; break;
5623 case 'f': *name++ = FF; ++p; break;
5624 case 'n': *name++ = NL; ++p; break;
5625 case 'r': *name++ = CAR; ++p; break;
5626 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
5628 case 'X': /* hex: "\x1", "\x12" */
5629 case 'x':
5630 case 'u': /* Unicode: "\u0023" */
5631 case 'U':
5632 if (vim_isxdigit(p[1]))
5633 {
5634 int n, nr;
5635 int c = toupper(*p);
5636
5637 if (c == 'X')
5638 n = 2;
5639 else
5640 n = 4;
5641 nr = 0;
5642 while (--n >= 0 && vim_isxdigit(p[1]))
5643 {
5644 ++p;
5645 nr = (nr << 4) + hex2nr(*p);
5646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#ifdef FEAT_MBYTE
5649 /* For "\u" store the number according to
5650 * 'encoding'. */
5651 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
5654#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 break;
5658
5659 /* octal: "\1", "\12", "\123" */
5660 case '0':
5661 case '1':
5662 case '2':
5663 case '3':
5664 case '4':
5665 case '5':
5666 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 case '7': *name = *p++ - '0';
5668 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 *name = (*name << 3) + *p++ - '0';
5671 if (*p >= '0' && *p <= '7')
5672 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 break;
5676
5677 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (extra != 0)
5680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683 }
5684 /* FALLTHROUGH */
5685
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 break;
5688 }
5689 }
5690 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 *arg = p + 1;
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 return OK;
5698}
5699
5700/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 * Return OK or FAIL.
5703 */
5704 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 int evaluate;
5709{
5710 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 int reduce = 0;
5713
5714 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 */
5717 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005720 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005722 break;
5723 ++reduce;
5724 ++p;
5725 }
5726 }
5727
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005731 return FAIL;
5732 }
5733
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 if (!evaluate)
5736 {
5737 *arg = p + 1;
5738 return OK;
5739 }
5740
5741 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 */
5744 str = alloc((unsigned)((p - *arg) - reduce));
5745 if (str == NULL)
5746 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 rettv->v_type = VAR_STRING;
5748 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 break;
5756 ++p;
5757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 *arg = p + 1;
5762
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 return OK;
5764}
5765
5766/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767 * Allocate a variable for a List and fill it from "*arg".
5768 * Return OK or FAIL.
5769 */
5770 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005771get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005773 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 int evaluate;
5775{
Bram Moolenaar33570922005-01-25 22:26:29 +00005776 list_T *l = NULL;
5777 typval_T tv;
5778 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779
5780 if (evaluate)
5781 {
5782 l = list_alloc();
5783 if (l == NULL)
5784 return FAIL;
5785 }
5786
5787 *arg = skipwhite(*arg + 1);
5788 while (**arg != ']' && **arg != NUL)
5789 {
5790 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5791 goto failret;
5792 if (evaluate)
5793 {
5794 item = listitem_alloc();
5795 if (item != NULL)
5796 {
5797 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005798 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 list_append(l, item);
5800 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005801 else
5802 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 }
5804
5805 if (**arg == ']')
5806 break;
5807 if (**arg != ',')
5808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810 goto failret;
5811 }
5812 *arg = skipwhite(*arg + 1);
5813 }
5814
5815 if (**arg != ']')
5816 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818failret:
5819 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005820 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 return FAIL;
5822 }
5823
5824 *arg = skipwhite(*arg + 1);
5825 if (evaluate)
5826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005827 rettv->v_type = VAR_LIST;
5828 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 ++l->lv_refcount;
5830 }
5831
5832 return OK;
5833}
5834
5835/*
5836 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005837 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005839 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840list_alloc()
5841{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005842 list_T *l;
5843
5844 l = (list_T *)alloc_clear(sizeof(list_T));
5845 if (l != NULL)
5846 {
5847 /* Prepend the list to the list of lists for garbage collection. */
5848 if (first_list != NULL)
5849 first_list->lv_used_prev = l;
5850 l->lv_used_prev = NULL;
5851 l->lv_used_next = first_list;
5852 first_list = l;
5853 }
5854 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855}
5856
5857/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005858 * Allocate an empty list for a return value.
5859 * Returns OK or FAIL.
5860 */
5861 static int
5862rettv_list_alloc(rettv)
5863 typval_T *rettv;
5864{
5865 list_T *l = list_alloc();
5866
5867 if (l == NULL)
5868 return FAIL;
5869
5870 rettv->vval.v_list = l;
5871 rettv->v_type = VAR_LIST;
5872 ++l->lv_refcount;
5873 return OK;
5874}
5875
5876/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877 * Unreference a list: decrement the reference count and free it when it
5878 * becomes zero.
5879 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005880 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005882 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005884 if (l != NULL && --l->lv_refcount <= 0)
5885 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886}
5887
5888/*
5889 * Free a list, including all items it points to.
5890 * Ignores the reference count.
5891 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005892 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005893list_free(l, recurse)
5894 list_T *l;
5895 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896{
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005899 /* Remove the list from the list of lists for garbage collection. */
5900 if (l->lv_used_prev == NULL)
5901 first_list = l->lv_used_next;
5902 else
5903 l->lv_used_prev->lv_used_next = l->lv_used_next;
5904 if (l->lv_used_next != NULL)
5905 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5906
Bram Moolenaard9fba312005-06-26 22:34:35 +00005907 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005909 /* Remove the item before deleting it. */
5910 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005911 if (recurse || (item->li_tv.v_type != VAR_LIST
5912 && item->li_tv.v_type != VAR_DICT))
5913 clear_tv(&item->li_tv);
5914 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 }
5916 vim_free(l);
5917}
5918
5919/*
5920 * Allocate a list item.
5921 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923listitem_alloc()
5924{
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926}
5927
5928/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005929 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 */
5931 static void
5932listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005935 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 vim_free(item);
5937}
5938
5939/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005940 * Remove a list item from a List and free it. Also clears the value.
5941 */
5942 static void
5943listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 list_T *l;
5945 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005946{
5947 list_remove(l, item, item);
5948 listitem_free(item);
5949}
5950
5951/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 * Get the number of items in a list.
5953 */
5954 static long
5955list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 if (l == NULL)
5959 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005960 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961}
5962
5963/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964 * Return TRUE when two lists have exactly the same values.
5965 */
5966 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005967list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005968 list_T *l1;
5969 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005970 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005971 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005972{
Bram Moolenaar33570922005-01-25 22:26:29 +00005973 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005975 if (l1 == NULL || l2 == NULL)
5976 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005977 if (l1 == l2)
5978 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005979 if (list_len(l1) != list_len(l2))
5980 return FALSE;
5981
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982 for (item1 = l1->lv_first, item2 = l2->lv_first;
5983 item1 != NULL && item2 != NULL;
5984 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005985 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005986 return FALSE;
5987 return item1 == NULL && item2 == NULL;
5988}
5989
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005990#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5991 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005992/*
5993 * Return the dictitem that an entry in a hashtable points to.
5994 */
5995 dictitem_T *
5996dict_lookup(hi)
5997 hashitem_T *hi;
5998{
5999 return HI2DI(hi);
6000}
6001#endif
6002
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006004 * Return TRUE when two dictionaries have exactly the same key/values.
6005 */
6006 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006007dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 dict_T *d1;
6009 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006010 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006011 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006012{
Bram Moolenaar33570922005-01-25 22:26:29 +00006013 hashitem_T *hi;
6014 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006015 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006016
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006017 if (d1 == NULL || d2 == NULL)
6018 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006019 if (d1 == d2)
6020 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006021 if (dict_len(d1) != dict_len(d2))
6022 return FALSE;
6023
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006024 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006027 if (!HASHITEM_EMPTY(hi))
6028 {
6029 item2 = dict_find(d2, hi->hi_key, -1);
6030 if (item2 == NULL)
6031 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006033 return FALSE;
6034 --todo;
6035 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006036 }
6037 return TRUE;
6038}
6039
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040static int tv_equal_recurse_limit;
6041
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006042/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006043 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006044 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006045 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006046 */
6047 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006048tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 typval_T *tv1;
6050 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006051 int ic; /* ignore case */
6052 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006053{
6054 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006055 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006056 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006057 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006058
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006059 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006060 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006062 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006063 * recursiveness to a limit. We guess they are equal then.
6064 * A fixed limit has the problem of still taking an awful long time.
6065 * Reduce the limit every time running into it. That should work fine for
6066 * deeply linked structures that are not recursively linked and catch
6067 * recursiveness quickly. */
6068 if (!recursive)
6069 tv_equal_recurse_limit = 1000;
6070 if (recursive_cnt >= tv_equal_recurse_limit)
6071 {
6072 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006073 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006074 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006075
6076 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006077 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006078 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079 ++recursive_cnt;
6080 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6081 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006082 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006083
6084 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006085 ++recursive_cnt;
6086 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6087 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006088 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006089
6090 case VAR_FUNC:
6091 return (tv1->vval.v_string != NULL
6092 && tv2->vval.v_string != NULL
6093 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6094
6095 case VAR_NUMBER:
6096 return tv1->vval.v_number == tv2->vval.v_number;
6097
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006098#ifdef FEAT_FLOAT
6099 case VAR_FLOAT:
6100 return tv1->vval.v_float == tv2->vval.v_float;
6101#endif
6102
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006103 case VAR_STRING:
6104 s1 = get_tv_string_buf(tv1, buf1);
6105 s2 = get_tv_string_buf(tv2, buf2);
6106 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006108
6109 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 return TRUE;
6111}
6112
6113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114 * Locate item with index "n" in list "l" and return it.
6115 * A negative index is counted from the end; -1 is the last item.
6116 * Returns NULL when "n" is out of range.
6117 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006119list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006121 long n;
6122{
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124 long idx;
6125
6126 if (l == NULL)
6127 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006128
6129 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006131 n = l->lv_len + n;
6132
6133 /* Check for index out of range. */
6134 if (n < 0 || n >= l->lv_len)
6135 return NULL;
6136
6137 /* When there is a cached index may start search from there. */
6138 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006140 if (n < l->lv_idx / 2)
6141 {
6142 /* closest to the start of the list */
6143 item = l->lv_first;
6144 idx = 0;
6145 }
6146 else if (n > (l->lv_idx + l->lv_len) / 2)
6147 {
6148 /* closest to the end of the list */
6149 item = l->lv_last;
6150 idx = l->lv_len - 1;
6151 }
6152 else
6153 {
6154 /* closest to the cached index */
6155 item = l->lv_idx_item;
6156 idx = l->lv_idx;
6157 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 }
6159 else
6160 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006161 if (n < l->lv_len / 2)
6162 {
6163 /* closest to the start of the list */
6164 item = l->lv_first;
6165 idx = 0;
6166 }
6167 else
6168 {
6169 /* closest to the end of the list */
6170 item = l->lv_last;
6171 idx = l->lv_len - 1;
6172 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006174
6175 while (n > idx)
6176 {
6177 /* search forward */
6178 item = item->li_next;
6179 ++idx;
6180 }
6181 while (n < idx)
6182 {
6183 /* search backward */
6184 item = item->li_prev;
6185 --idx;
6186 }
6187
6188 /* cache the used index */
6189 l->lv_idx = idx;
6190 l->lv_idx_item = item;
6191
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 return item;
6193}
6194
6195/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006196 * Get list item "l[idx]" as a number.
6197 */
6198 static long
6199list_find_nr(l, idx, errorp)
6200 list_T *l;
6201 long idx;
6202 int *errorp; /* set to TRUE when something wrong */
6203{
6204 listitem_T *li;
6205
6206 li = list_find(l, idx);
6207 if (li == NULL)
6208 {
6209 if (errorp != NULL)
6210 *errorp = TRUE;
6211 return -1L;
6212 }
6213 return get_tv_number_chk(&li->li_tv, errorp);
6214}
6215
6216/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006217 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6218 */
6219 char_u *
6220list_find_str(l, idx)
6221 list_T *l;
6222 long idx;
6223{
6224 listitem_T *li;
6225
6226 li = list_find(l, idx - 1);
6227 if (li == NULL)
6228 {
6229 EMSGN(_(e_listidx), idx);
6230 return NULL;
6231 }
6232 return get_tv_string(&li->li_tv);
6233}
6234
6235/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006236 * Locate "item" list "l" and return its index.
6237 * Returns -1 when "item" is not in the list.
6238 */
6239 static long
6240list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 list_T *l;
6242 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006243{
6244 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006245 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006246
6247 if (l == NULL)
6248 return -1;
6249 idx = 0;
6250 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6251 ++idx;
6252 if (li == NULL)
6253 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006254 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006255}
6256
6257/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258 * Append item "item" to the end of list "l".
6259 */
6260 static void
6261list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 list_T *l;
6263 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264{
6265 if (l->lv_last == NULL)
6266 {
6267 /* empty list */
6268 l->lv_first = item;
6269 l->lv_last = item;
6270 item->li_prev = NULL;
6271 }
6272 else
6273 {
6274 l->lv_last->li_next = item;
6275 item->li_prev = l->lv_last;
6276 l->lv_last = item;
6277 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006278 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 item->li_next = NULL;
6280}
6281
6282/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006284 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006286 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 list_T *l;
6289 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006291 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292
Bram Moolenaar05159a02005-02-26 23:04:13 +00006293 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006295 copy_tv(tv, &li->li_tv);
6296 list_append(l, li);
6297 return OK;
6298}
6299
6300/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006301 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006302 * Return FAIL when out of memory.
6303 */
6304 int
6305list_append_dict(list, dict)
6306 list_T *list;
6307 dict_T *dict;
6308{
6309 listitem_T *li = listitem_alloc();
6310
6311 if (li == NULL)
6312 return FAIL;
6313 li->li_tv.v_type = VAR_DICT;
6314 li->li_tv.v_lock = 0;
6315 li->li_tv.vval.v_dict = dict;
6316 list_append(list, li);
6317 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 return OK;
6319}
6320
6321/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006322 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006323 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006324 * Returns FAIL when out of memory.
6325 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006326 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006327list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006328 list_T *l;
6329 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006330 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006331{
6332 listitem_T *li = listitem_alloc();
6333
6334 if (li == NULL)
6335 return FAIL;
6336 list_append(l, li);
6337 li->li_tv.v_type = VAR_STRING;
6338 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006339 if (str == NULL)
6340 li->li_tv.vval.v_string = NULL;
6341 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006342 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006343 return FAIL;
6344 return OK;
6345}
6346
6347/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006348 * Append "n" to list "l".
6349 * Returns FAIL when out of memory.
6350 */
6351 static int
6352list_append_number(l, n)
6353 list_T *l;
6354 varnumber_T n;
6355{
6356 listitem_T *li;
6357
6358 li = listitem_alloc();
6359 if (li == NULL)
6360 return FAIL;
6361 li->li_tv.v_type = VAR_NUMBER;
6362 li->li_tv.v_lock = 0;
6363 li->li_tv.vval.v_number = n;
6364 list_append(l, li);
6365 return OK;
6366}
6367
6368/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006370 * If "item" is NULL append at the end.
6371 * Return FAIL when out of memory.
6372 */
6373 static int
6374list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 list_T *l;
6376 typval_T *tv;
6377 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006378{
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006380
6381 if (ni == NULL)
6382 return FAIL;
6383 copy_tv(tv, &ni->li_tv);
6384 if (item == NULL)
6385 /* Append new item at end of list. */
6386 list_append(l, ni);
6387 else
6388 {
6389 /* Insert new item before existing item. */
6390 ni->li_prev = item->li_prev;
6391 ni->li_next = item;
6392 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006393 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006394 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006395 ++l->lv_idx;
6396 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006398 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006400 l->lv_idx_item = NULL;
6401 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006403 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006404 }
6405 return OK;
6406}
6407
6408/*
6409 * Extend "l1" with "l2".
6410 * If "bef" is NULL append at the end, otherwise insert before this item.
6411 * Returns FAIL when out of memory.
6412 */
6413 static int
6414list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 list_T *l1;
6416 list_T *l2;
6417 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418{
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006420 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006422 /* We also quit the loop when we have inserted the original item count of
6423 * the list, avoid a hang when we extend a list with itself. */
6424 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6426 return FAIL;
6427 return OK;
6428}
6429
6430/*
6431 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6432 * Return FAIL when out of memory.
6433 */
6434 static int
6435list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 list_T *l1;
6437 list_T *l2;
6438 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439{
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006442 if (l1 == NULL || l2 == NULL)
6443 return FAIL;
6444
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006446 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 if (l == NULL)
6448 return FAIL;
6449 tv->v_type = VAR_LIST;
6450 tv->vval.v_list = l;
6451
6452 /* append all items from the second list */
6453 return list_extend(l, l2, NULL);
6454}
6455
6456/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006459 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460 * Returns NULL when out of memory.
6461 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006463list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006466 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467{
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *copy;
6469 listitem_T *item;
6470 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471
6472 if (orig == NULL)
6473 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474
6475 copy = list_alloc();
6476 if (copy != NULL)
6477 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006478 if (copyID != 0)
6479 {
6480 /* Do this before adding the items, because one of the items may
6481 * refer back to this list. */
6482 orig->lv_copyID = copyID;
6483 orig->lv_copylist = copy;
6484 }
6485 for (item = orig->lv_first; item != NULL && !got_int;
6486 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487 {
6488 ni = listitem_alloc();
6489 if (ni == NULL)
6490 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006491 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006492 {
6493 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6494 {
6495 vim_free(ni);
6496 break;
6497 }
6498 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006500 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501 list_append(copy, ni);
6502 }
6503 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 if (item != NULL)
6505 {
6506 list_unref(copy);
6507 copy = NULL;
6508 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 }
6510
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 return copy;
6512}
6513
6514/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006516 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006519list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006520 list_T *l;
6521 listitem_T *item;
6522 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523{
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 /* notify watchers */
6527 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 list_fix_watch(l, ip);
6531 if (ip == item2)
6532 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534
6535 if (item2->li_next == NULL)
6536 l->lv_last = item->li_prev;
6537 else
6538 item2->li_next->li_prev = item->li_prev;
6539 if (item->li_prev == NULL)
6540 l->lv_first = item2->li_next;
6541 else
6542 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006543 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544}
6545
6546/*
6547 * Return an allocated string with the string representation of a list.
6548 * May return NULL.
6549 */
6550 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006551list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006553 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554{
6555 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556
6557 if (tv->vval.v_list == NULL)
6558 return NULL;
6559 ga_init2(&ga, (int)sizeof(char), 80);
6560 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006561 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 {
6563 vim_free(ga.ga_data);
6564 return NULL;
6565 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566 ga_append(&ga, ']');
6567 ga_append(&ga, NUL);
6568 return (char_u *)ga.ga_data;
6569}
6570
6571/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006572 * Join list "l" into a string in "*gap", using separator "sep".
6573 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006574 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006575 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006576 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006577list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006578 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006580 char_u *sep;
6581 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006582 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006583{
6584 int first = TRUE;
6585 char_u *tofree;
6586 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006588 char_u *s;
6589
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006591 {
6592 if (first)
6593 first = FALSE;
6594 else
6595 ga_concat(gap, sep);
6596
6597 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006598 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006599 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006600 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006601 if (s != NULL)
6602 ga_concat(gap, s);
6603 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 if (s == NULL)
6605 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006606 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006607 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006609}
6610
6611/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006612 * Garbage collection for lists and dictionaries.
6613 *
6614 * We use reference counts to be able to free most items right away when they
6615 * are no longer used. But for composite items it's possible that it becomes
6616 * unused while the reference count is > 0: When there is a recursive
6617 * reference. Example:
6618 * :let l = [1, 2, 3]
6619 * :let d = {9: l}
6620 * :let l[1] = d
6621 *
6622 * Since this is quite unusual we handle this with garbage collection: every
6623 * once in a while find out which lists and dicts are not referenced from any
6624 * variable.
6625 *
6626 * Here is a good reference text about garbage collection (refers to Python
6627 * but it applies to all reference-counting mechanisms):
6628 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006629 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006630
6631/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006632 * Do garbage collection for lists and dicts.
6633 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006634 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006635 int
6636garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006637{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006638 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006639 buf_T *buf;
6640 win_T *wp;
6641 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006642 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006643 int did_free;
6644 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006645#ifdef FEAT_WINDOWS
6646 tabpage_T *tp;
6647#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006648
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006649 /* Only do this once. */
6650 want_garbage_collect = FALSE;
6651 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006652 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006653
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006654 /* We advance by two because we add one for items referenced through
6655 * previous_funccal. */
6656 current_copyID += COPYID_INC;
6657 copyID = current_copyID;
6658
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006659 /*
6660 * 1. Go through all accessible variables and mark all lists and dicts
6661 * with copyID.
6662 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006663
6664 /* Don't free variables in the previous_funccal list unless they are only
6665 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006666 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006667 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6668 {
6669 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6670 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6671 }
6672
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006673 /* script-local variables */
6674 for (i = 1; i <= ga_scripts.ga_len; ++i)
6675 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6676
6677 /* buffer-local variables */
6678 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6679 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6680
6681 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006682 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006683 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6684
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006685#ifdef FEAT_WINDOWS
6686 /* tabpage-local variables */
6687 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6688 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6689#endif
6690
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006691 /* global variables */
6692 set_ref_in_ht(&globvarht, copyID);
6693
6694 /* function-local variables */
6695 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6696 {
6697 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6698 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6699 }
6700
Bram Moolenaard812df62008-11-09 12:46:09 +00006701 /* v: vars */
6702 set_ref_in_ht(&vimvarht, copyID);
6703
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006704 /*
6705 * 2. Free lists and dictionaries that are not referenced.
6706 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006707 did_free = free_unref_items(copyID);
6708
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006709 /*
6710 * 3. Check if any funccal can be freed now.
6711 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006712 for (pfc = &previous_funccal; *pfc != NULL; )
6713 {
6714 if (can_free_funccal(*pfc, copyID))
6715 {
6716 fc = *pfc;
6717 *pfc = fc->caller;
6718 free_funccal(fc, TRUE);
6719 did_free = TRUE;
6720 did_free_funccal = TRUE;
6721 }
6722 else
6723 pfc = &(*pfc)->caller;
6724 }
6725 if (did_free_funccal)
6726 /* When a funccal was freed some more items might be garbage
6727 * collected, so run again. */
6728 (void)garbage_collect();
6729
6730 return did_free;
6731}
6732
6733/*
6734 * Free lists and dictionaries that are no longer referenced.
6735 */
6736 static int
6737free_unref_items(copyID)
6738 int copyID;
6739{
6740 dict_T *dd;
6741 list_T *ll;
6742 int did_free = FALSE;
6743
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006745 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 */
6747 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006748 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006750 /* Free the Dictionary and ordinary items it contains, but don't
6751 * recurse into Lists and Dictionaries, they will be in the list
6752 * of dicts or list of lists. */
6753 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006754 did_free = TRUE;
6755
6756 /* restart, next dict may also have been freed */
6757 dd = first_dict;
6758 }
6759 else
6760 dd = dd->dv_used_next;
6761
6762 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006763 * Go through the list of lists and free items without the copyID.
6764 * But don't free a list that has a watcher (used in a for loop), these
6765 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 */
6767 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6769 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006771 /* Free the List and ordinary items it contains, but don't recurse
6772 * into Lists and Dictionaries, they will be in the list of dicts
6773 * or list of lists. */
6774 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775 did_free = TRUE;
6776
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006777 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 ll = first_list;
6779 }
6780 else
6781 ll = ll->lv_used_next;
6782
6783 return did_free;
6784}
6785
6786/*
6787 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6788 */
6789 static void
6790set_ref_in_ht(ht, copyID)
6791 hashtab_T *ht;
6792 int copyID;
6793{
6794 int todo;
6795 hashitem_T *hi;
6796
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006797 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006798 for (hi = ht->ht_array; todo > 0; ++hi)
6799 if (!HASHITEM_EMPTY(hi))
6800 {
6801 --todo;
6802 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6803 }
6804}
6805
6806/*
6807 * Mark all lists and dicts referenced through list "l" with "copyID".
6808 */
6809 static void
6810set_ref_in_list(l, copyID)
6811 list_T *l;
6812 int copyID;
6813{
6814 listitem_T *li;
6815
6816 for (li = l->lv_first; li != NULL; li = li->li_next)
6817 set_ref_in_item(&li->li_tv, copyID);
6818}
6819
6820/*
6821 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6822 */
6823 static void
6824set_ref_in_item(tv, copyID)
6825 typval_T *tv;
6826 int copyID;
6827{
6828 dict_T *dd;
6829 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006830
6831 switch (tv->v_type)
6832 {
6833 case VAR_DICT:
6834 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006835 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 /* Didn't see this dict yet. */
6838 dd->dv_copyID = copyID;
6839 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842
6843 case VAR_LIST:
6844 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006845 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 /* Didn't see this list yet. */
6848 ll->lv_copyID = copyID;
6849 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006852 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006854}
6855
6856/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006857 * Allocate an empty header for a dictionary.
6858 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006859 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006860dict_alloc()
6861{
Bram Moolenaar33570922005-01-25 22:26:29 +00006862 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006863
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006865 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006866 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006867 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868 if (first_dict != NULL)
6869 first_dict->dv_used_prev = d;
6870 d->dv_used_next = first_dict;
6871 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006872 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873
Bram Moolenaar33570922005-01-25 22:26:29 +00006874 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006875 d->dv_lock = 0;
6876 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006877 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006878 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006879 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880}
6881
6882/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006883 * Allocate an empty dict for a return value.
6884 * Returns OK or FAIL.
6885 */
6886 static int
6887rettv_dict_alloc(rettv)
6888 typval_T *rettv;
6889{
6890 dict_T *d = dict_alloc();
6891
6892 if (d == NULL)
6893 return FAIL;
6894
6895 rettv->vval.v_dict = d;
6896 rettv->v_type = VAR_DICT;
6897 ++d->dv_refcount;
6898 return OK;
6899}
6900
6901
6902/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006903 * Unreference a Dictionary: decrement the reference count and free it when it
6904 * becomes zero.
6905 */
6906 static void
6907dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006908 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006909{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006910 if (d != NULL && --d->dv_refcount <= 0)
6911 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006912}
6913
6914/*
6915 * Free a Dictionary, including all items it contains.
6916 * Ignores the reference count.
6917 */
6918 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006919dict_free(d, recurse)
6920 dict_T *d;
6921 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006922{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006923 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006924 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006925 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006926
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 /* Remove the dict from the list of dicts for garbage collection. */
6928 if (d->dv_used_prev == NULL)
6929 first_dict = d->dv_used_next;
6930 else
6931 d->dv_used_prev->dv_used_next = d->dv_used_next;
6932 if (d->dv_used_next != NULL)
6933 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6934
6935 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006936 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006937 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006938 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006939 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006940 if (!HASHITEM_EMPTY(hi))
6941 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006942 /* Remove the item before deleting it, just in case there is
6943 * something recursive causing trouble. */
6944 di = HI2DI(hi);
6945 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006946 if (recurse || (di->di_tv.v_type != VAR_LIST
6947 && di->di_tv.v_type != VAR_DICT))
6948 clear_tv(&di->di_tv);
6949 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006950 --todo;
6951 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006952 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006954 vim_free(d);
6955}
6956
6957/*
6958 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006959 * The "key" is copied to the new item.
6960 * Note that the value of the item "di_tv" still needs to be initialized!
6961 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006962 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006963 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006964dictitem_alloc(key)
6965 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966{
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006968
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006969 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006970 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006971 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006973 di->di_flags = 0;
6974 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976}
6977
6978/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006979 * Make a copy of a Dictionary item.
6980 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006981 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006982dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006983 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006984{
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006987 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6988 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 if (di != NULL)
6990 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006991 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006992 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006993 copy_tv(&org->di_tv, &di->di_tv);
6994 }
6995 return di;
6996}
6997
6998/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006999 * Remove item "item" from Dictionary "dict" and free it.
7000 */
7001 static void
7002dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007003 dict_T *dict;
7004 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007005{
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007007
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007009 if (HASHITEM_EMPTY(hi))
7010 EMSG2(_(e_intern2), "dictitem_remove()");
7011 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007013 dictitem_free(item);
7014}
7015
7016/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017 * Free a dict item. Also clears the value.
7018 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007019 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007021 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007023 clear_tv(&item->di_tv);
7024 vim_free(item);
7025}
7026
7027/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7029 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007030 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031 * Returns NULL when out of memory.
7032 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007033 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007034dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007036 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007037 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007038{
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 dict_T *copy;
7040 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007041 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007042 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007043
7044 if (orig == NULL)
7045 return NULL;
7046
7047 copy = dict_alloc();
7048 if (copy != NULL)
7049 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007050 if (copyID != 0)
7051 {
7052 orig->dv_copyID = copyID;
7053 orig->dv_copydict = copy;
7054 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007055 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007056 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007057 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007058 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007060 --todo;
7061
7062 di = dictitem_alloc(hi->hi_key);
7063 if (di == NULL)
7064 break;
7065 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007066 {
7067 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7068 copyID) == FAIL)
7069 {
7070 vim_free(di);
7071 break;
7072 }
7073 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074 else
7075 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7076 if (dict_add(copy, di) == FAIL)
7077 {
7078 dictitem_free(di);
7079 break;
7080 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007081 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007082 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083
Bram Moolenaare9a41262005-01-15 22:18:47 +00007084 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007085 if (todo > 0)
7086 {
7087 dict_unref(copy);
7088 copy = NULL;
7089 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007090 }
7091
7092 return copy;
7093}
7094
7095/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007097 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007099 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 dict_T *d;
7102 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103{
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007105}
7106
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007108 * Add a number or string entry to dictionary "d".
7109 * When "str" is NULL use number "nr", otherwise use "str".
7110 * Returns FAIL when out of memory and when key already exists.
7111 */
7112 int
7113dict_add_nr_str(d, key, nr, str)
7114 dict_T *d;
7115 char *key;
7116 long nr;
7117 char_u *str;
7118{
7119 dictitem_T *item;
7120
7121 item = dictitem_alloc((char_u *)key);
7122 if (item == NULL)
7123 return FAIL;
7124 item->di_tv.v_lock = 0;
7125 if (str == NULL)
7126 {
7127 item->di_tv.v_type = VAR_NUMBER;
7128 item->di_tv.vval.v_number = nr;
7129 }
7130 else
7131 {
7132 item->di_tv.v_type = VAR_STRING;
7133 item->di_tv.vval.v_string = vim_strsave(str);
7134 }
7135 if (dict_add(d, item) == FAIL)
7136 {
7137 dictitem_free(item);
7138 return FAIL;
7139 }
7140 return OK;
7141}
7142
7143/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007144 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007145 * Returns FAIL when out of memory and when key already exists.
7146 */
7147 int
7148dict_add_list(d, key, list)
7149 dict_T *d;
7150 char *key;
7151 list_T *list;
7152{
7153 dictitem_T *item;
7154
7155 item = dictitem_alloc((char_u *)key);
7156 if (item == NULL)
7157 return FAIL;
7158 item->di_tv.v_lock = 0;
7159 item->di_tv.v_type = VAR_LIST;
7160 item->di_tv.vval.v_list = list;
7161 if (dict_add(d, item) == FAIL)
7162 {
7163 dictitem_free(item);
7164 return FAIL;
7165 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007166 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007167 return OK;
7168}
7169
7170/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 * Get the number of items in a Dictionary.
7172 */
7173 static long
7174dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007177 if (d == NULL)
7178 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007179 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180}
7181
7182/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183 * Find item "key[len]" in Dictionary "d".
7184 * If "len" is negative use strlen(key).
7185 * Returns NULL when not found.
7186 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007187 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190 char_u *key;
7191 int len;
7192{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193#define AKEYLEN 200
7194 char_u buf[AKEYLEN];
7195 char_u *akey;
7196 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 if (len < 0)
7200 akey = key;
7201 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007202 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 tofree = akey = vim_strnsave(key, len);
7204 if (akey == NULL)
7205 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 else
7208 {
7209 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007210 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211 akey = buf;
7212 }
7213
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007215 vim_free(tofree);
7216 if (HASHITEM_EMPTY(hi))
7217 return NULL;
7218 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219}
7220
7221/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007222 * Get a string item from a dictionary.
7223 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007224 * Returns NULL if the entry doesn't exist or out of memory.
7225 */
7226 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007227get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007228 dict_T *d;
7229 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007230 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007231{
7232 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007233 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007234
7235 di = dict_find(d, key, -1);
7236 if (di == NULL)
7237 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007238 s = get_tv_string(&di->di_tv);
7239 if (save && s != NULL)
7240 s = vim_strsave(s);
7241 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007242}
7243
7244/*
7245 * Get a number item from a dictionary.
7246 * Returns 0 if the entry doesn't exist or out of memory.
7247 */
7248 long
7249get_dict_number(d, key)
7250 dict_T *d;
7251 char_u *key;
7252{
7253 dictitem_T *di;
7254
7255 di = dict_find(d, key, -1);
7256 if (di == NULL)
7257 return 0;
7258 return get_tv_number(&di->di_tv);
7259}
7260
7261/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 * Return an allocated string with the string representation of a Dictionary.
7263 * May return NULL.
7264 */
7265 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007266dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007268 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269{
7270 garray_T ga;
7271 int first = TRUE;
7272 char_u *tofree;
7273 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007277 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 return NULL;
7281 ga_init2(&ga, (int)sizeof(char), 80);
7282 ga_append(&ga, '{');
7283
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007284 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007285 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 --todo;
7290
7291 if (first)
7292 first = FALSE;
7293 else
7294 ga_concat(&ga, (char_u *)", ");
7295
7296 tofree = string_quote(hi->hi_key, FALSE);
7297 if (tofree != NULL)
7298 {
7299 ga_concat(&ga, tofree);
7300 vim_free(tofree);
7301 }
7302 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007303 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 if (s != NULL)
7305 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007307 if (s == NULL)
7308 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007311 if (todo > 0)
7312 {
7313 vim_free(ga.ga_data);
7314 return NULL;
7315 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316
7317 ga_append(&ga, '}');
7318 ga_append(&ga, NUL);
7319 return (char_u *)ga.ga_data;
7320}
7321
7322/*
7323 * Allocate a variable for a Dictionary and fill it from "*arg".
7324 * Return OK or FAIL. Returns NOTDONE for {expr}.
7325 */
7326 static int
7327get_dict_tv(arg, rettv, evaluate)
7328 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330 int evaluate;
7331{
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 dict_T *d = NULL;
7333 typval_T tvkey;
7334 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007335 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339
7340 /*
7341 * First check if it's not a curly-braces thing: {expr}.
7342 * Must do this without evaluating, otherwise a function may be called
7343 * twice. Unfortunately this means we need to call eval1() twice for the
7344 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007347 if (*start != '}')
7348 {
7349 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7350 return FAIL;
7351 if (*start == '}')
7352 return NOTDONE;
7353 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354
7355 if (evaluate)
7356 {
7357 d = dict_alloc();
7358 if (d == NULL)
7359 return FAIL;
7360 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 tvkey.v_type = VAR_UNKNOWN;
7362 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363
7364 *arg = skipwhite(*arg + 1);
7365 while (**arg != '}' && **arg != NUL)
7366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368 goto failret;
7369 if (**arg != ':')
7370 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007371 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373 goto failret;
7374 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007375 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007377 key = get_tv_string_buf_chk(&tvkey, buf);
7378 if (key == NULL || *key == NUL)
7379 {
7380 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7381 if (key != NULL)
7382 EMSG(_(e_emptykey));
7383 clear_tv(&tvkey);
7384 goto failret;
7385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387
7388 *arg = skipwhite(*arg + 1);
7389 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7390 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007391 if (evaluate)
7392 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 goto failret;
7394 }
7395 if (evaluate)
7396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 if (item != NULL)
7399 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007400 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 clear_tv(&tv);
7403 goto failret;
7404 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405 item = dictitem_alloc(key);
7406 clear_tv(&tvkey);
7407 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007410 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 if (dict_add(d, item) == FAIL)
7412 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 }
7414 }
7415
7416 if (**arg == '}')
7417 break;
7418 if (**arg != ',')
7419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007420 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 goto failret;
7422 }
7423 *arg = skipwhite(*arg + 1);
7424 }
7425
7426 if (**arg != '}')
7427 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007428 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429failret:
7430 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007431 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 return FAIL;
7433 }
7434
7435 *arg = skipwhite(*arg + 1);
7436 if (evaluate)
7437 {
7438 rettv->v_type = VAR_DICT;
7439 rettv->vval.v_dict = d;
7440 ++d->dv_refcount;
7441 }
7442
7443 return OK;
7444}
7445
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007447 * Return a string with the string representation of a variable.
7448 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007449 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007450 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007452 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007453 */
7454 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007457 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007458 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007459 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007460{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007461 static int recurse = 0;
7462 char_u *r = NULL;
7463
Bram Moolenaar33570922005-01-25 22:26:29 +00007464 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007466 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007467 *tofree = NULL;
7468 return NULL;
7469 }
7470 ++recurse;
7471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007472 switch (tv->v_type)
7473 {
7474 case VAR_FUNC:
7475 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 r = tv->vval.v_string;
7477 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007478
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007479 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007480 if (tv->vval.v_list == NULL)
7481 {
7482 *tofree = NULL;
7483 r = NULL;
7484 }
7485 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7486 {
7487 *tofree = NULL;
7488 r = (char_u *)"[...]";
7489 }
7490 else
7491 {
7492 tv->vval.v_list->lv_copyID = copyID;
7493 *tofree = list2string(tv, copyID);
7494 r = *tofree;
7495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007496 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007497
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007499 if (tv->vval.v_dict == NULL)
7500 {
7501 *tofree = NULL;
7502 r = NULL;
7503 }
7504 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7505 {
7506 *tofree = NULL;
7507 r = (char_u *)"{...}";
7508 }
7509 else
7510 {
7511 tv->vval.v_dict->dv_copyID = copyID;
7512 *tofree = dict2string(tv, copyID);
7513 r = *tofree;
7514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007517 case VAR_STRING:
7518 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007519 *tofree = NULL;
7520 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007521 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007522
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007523#ifdef FEAT_FLOAT
7524 case VAR_FLOAT:
7525 *tofree = NULL;
7526 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7527 r = numbuf;
7528 break;
7529#endif
7530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007531 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007532 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007533 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007535
7536 --recurse;
7537 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538}
7539
7540/*
7541 * Return a string with the string representation of a variable.
7542 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7543 * "numbuf" is used for a number.
7544 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007545 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007546 */
7547 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007548tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007550 char_u **tofree;
7551 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007552 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007553{
7554 switch (tv->v_type)
7555 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007556 case VAR_FUNC:
7557 *tofree = string_quote(tv->vval.v_string, TRUE);
7558 return *tofree;
7559 case VAR_STRING:
7560 *tofree = string_quote(tv->vval.v_string, FALSE);
7561 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007562#ifdef FEAT_FLOAT
7563 case VAR_FLOAT:
7564 *tofree = NULL;
7565 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7566 return numbuf;
7567#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007568 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007569 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007572 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007575 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007576}
7577
7578/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 * Return string "str" in ' quotes, doubling ' characters.
7580 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007582 */
7583 static char_u *
7584string_quote(str, function)
7585 char_u *str;
7586 int function;
7587{
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007589 char_u *p, *r, *s;
7590
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 len = (function ? 13 : 3);
7592 if (str != NULL)
7593 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007594 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 for (p = str; *p != NUL; mb_ptr_adv(p))
7596 if (*p == '\'')
7597 ++len;
7598 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007599 s = r = alloc(len);
7600 if (r != NULL)
7601 {
7602 if (function)
7603 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007605 r += 10;
7606 }
7607 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 if (str != NULL)
7610 for (p = str; *p != NUL; )
7611 {
7612 if (*p == '\'')
7613 *r++ = '\'';
7614 MB_COPY_CHAR(p, r);
7615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007617 if (function)
7618 *r++ = ')';
7619 *r++ = NUL;
7620 }
7621 return s;
7622}
7623
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007624#ifdef FEAT_FLOAT
7625/*
7626 * Convert the string "text" to a floating point number.
7627 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7628 * this always uses a decimal point.
7629 * Returns the length of the text that was consumed.
7630 */
7631 static int
7632string2float(text, value)
7633 char_u *text;
7634 float_T *value; /* result stored here */
7635{
7636 char *s = (char *)text;
7637 float_T f;
7638
7639 f = strtod(s, &s);
7640 *value = f;
7641 return (int)((char_u *)s - text);
7642}
7643#endif
7644
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 * Get the value of an environment variable.
7647 * "arg" is pointing to the '$'. It is advanced to after the name.
7648 * If the environment variable was not set, silently assume it is empty.
7649 * Always return OK.
7650 */
7651 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 int evaluate;
7656{
7657 char_u *string = NULL;
7658 int len;
7659 int cc;
7660 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007661 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
7663 ++*arg;
7664 name = *arg;
7665 len = get_env_len(arg);
7666 if (evaluate)
7667 {
7668 if (len != 0)
7669 {
7670 cc = name[len];
7671 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007672 /* first try vim_getenv(), fast for normal environment vars */
7673 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007675 {
7676 if (!mustfree)
7677 string = vim_strsave(string);
7678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 else
7680 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007681 if (mustfree)
7682 vim_free(string);
7683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 /* next try expanding things like $VIM and ${HOME} */
7685 string = expand_env_save(name - 1);
7686 if (string != NULL && *string == '$')
7687 {
7688 vim_free(string);
7689 string = NULL;
7690 }
7691 }
7692 name[len] = cc;
7693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 rettv->v_type = VAR_STRING;
7695 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 }
7697
7698 return OK;
7699}
7700
7701/*
7702 * Array with names and number of arguments of all internal functions
7703 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7704 */
7705static struct fst
7706{
7707 char *f_name; /* function name */
7708 char f_min_argc; /* minimal number of arguments */
7709 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007710 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007711 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712} functions[] =
7713{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007714#ifdef FEAT_FLOAT
7715 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007716 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007717#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007718 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {"append", 2, 2, f_append},
7720 {"argc", 0, 0, f_argc},
7721 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007722 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007723#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007724 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007725 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007726 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007729 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {"bufexists", 1, 1, f_bufexists},
7731 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7732 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7733 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7734 {"buflisted", 1, 1, f_buflisted},
7735 {"bufloaded", 1, 1, f_bufloaded},
7736 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007737 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {"bufwinnr", 1, 1, f_bufwinnr},
7739 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007740 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007742#ifdef FEAT_FLOAT
7743 {"ceil", 1, 1, f_ceil},
7744#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007745 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {"char2nr", 1, 1, f_char2nr},
7747 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007748 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007750#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007751 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007752 {"complete_add", 1, 1, f_complete_add},
7753 {"complete_check", 0, 0, f_complete_check},
7754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007757#ifdef FEAT_FLOAT
7758 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007759 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007760#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007761 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007763 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007764 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {"delete", 1, 1, f_delete},
7766 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007767 {"diff_filler", 1, 1, f_diff_filler},
7768 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007769 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {"eventhandler", 0, 0, f_eventhandler},
7773 {"executable", 1, 1, f_executable},
7774 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007775#ifdef FEAT_FLOAT
7776 {"exp", 1, 1, f_exp},
7777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007779 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007780 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7782 {"filereadable", 1, 1, f_filereadable},
7783 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007785 {"finddir", 1, 3, f_finddir},
7786 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007787#ifdef FEAT_FLOAT
7788 {"float2nr", 1, 1, f_float2nr},
7789 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007790 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007791#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007792 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {"fnamemodify", 2, 2, f_fnamemodify},
7794 {"foldclosed", 1, 1, f_foldclosed},
7795 {"foldclosedend", 1, 1, f_foldclosedend},
7796 {"foldlevel", 1, 1, f_foldlevel},
7797 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007798 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007801 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007802 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007803 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {"getbufvar", 2, 2, f_getbufvar},
7805 {"getchar", 0, 1, f_getchar},
7806 {"getcharmod", 0, 0, f_getcharmod},
7807 {"getcmdline", 0, 0, f_getcmdline},
7808 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007809 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007811 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007812 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {"getfsize", 1, 1, f_getfsize},
7814 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007815 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007816 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007817 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007818 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007819 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007820 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007821 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007822 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007824 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007825 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {"getwinposx", 0, 0, f_getwinposx},
7827 {"getwinposy", 0, 0, f_getwinposy},
7828 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007829 {"glob", 1, 2, f_glob},
7830 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007833 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007834 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7836 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7837 {"histadd", 2, 2, f_histadd},
7838 {"histdel", 1, 2, f_histdel},
7839 {"histget", 1, 2, f_histget},
7840 {"histnr", 1, 1, f_histnr},
7841 {"hlID", 1, 1, f_hlID},
7842 {"hlexists", 1, 1, f_hlexists},
7843 {"hostname", 0, 0, f_hostname},
7844 {"iconv", 3, 3, f_iconv},
7845 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007846 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007847 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007849 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"inputrestore", 0, 0, f_inputrestore},
7851 {"inputsave", 0, 0, f_inputsave},
7852 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007853 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007855 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007857 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007858 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"libcall", 3, 3, f_libcall},
7862 {"libcallnr", 3, 3, f_libcallnr},
7863 {"line", 1, 1, f_line},
7864 {"line2byte", 1, 1, f_line2byte},
7865 {"lispindent", 1, 1, f_lispindent},
7866 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007868 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869 {"log10", 1, 1, f_log10},
7870#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007871 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007872 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007873 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007874 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007875 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007876 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007877 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007878 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007879 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007880 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007881 {"max", 1, 1, f_max},
7882 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007883#ifdef vim_mkdir
7884 {"mkdir", 1, 3, f_mkdir},
7885#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007887#ifdef FEAT_MZSCHEME
7888 {"mzeval", 1, 1, f_mzeval},
7889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"nextnonblank", 1, 1, f_nextnonblank},
7891 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007892 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007893#ifdef FEAT_FLOAT
7894 {"pow", 2, 2, f_pow},
7895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007897 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007898 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007899 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007900 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007901 {"reltime", 0, 2, f_reltime},
7902 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"remote_expr", 2, 3, f_remote_expr},
7904 {"remote_foreground", 1, 1, f_remote_foreground},
7905 {"remote_peek", 1, 2, f_remote_peek},
7906 {"remote_read", 1, 1, f_remote_read},
7907 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007908 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007910 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007912 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913#ifdef FEAT_FLOAT
7914 {"round", 1, 1, f_round},
7915#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007916 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007917 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007918 {"searchpair", 3, 7, f_searchpair},
7919 {"searchpairpos", 3, 7, f_searchpairpos},
7920 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"server2client", 2, 2, f_server2client},
7922 {"serverlist", 0, 0, f_serverlist},
7923 {"setbufvar", 3, 3, f_setbufvar},
7924 {"setcmdpos", 1, 1, f_setcmdpos},
7925 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007926 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007927 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007928 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007929 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007931 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007932 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007934 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007936#ifdef FEAT_FLOAT
7937 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007938 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007939#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02007940 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007941 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007942 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007943 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007944 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945#ifdef FEAT_FLOAT
7946 {"sqrt", 1, 1, f_sqrt},
7947 {"str2float", 1, 1, f_str2float},
7948#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007949 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007950 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007951 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952#ifdef HAVE_STRFTIME
7953 {"strftime", 1, 2, f_strftime},
7954#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007956 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"strlen", 1, 1, f_strlen},
7958 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007959 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007961 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {"submatch", 1, 1, f_submatch},
7963 {"substitute", 4, 4, f_substitute},
7964 {"synID", 3, 3, f_synID},
7965 {"synIDattr", 2, 3, f_synIDattr},
7966 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007967 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007968 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007969 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007970 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007971 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007972 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007973 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007974 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007975#ifdef FEAT_FLOAT
7976 {"tan", 1, 1, f_tan},
7977 {"tanh", 1, 1, f_tanh},
7978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007980 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"tolower", 1, 1, f_tolower},
7982 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007983 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007984#ifdef FEAT_FLOAT
7985 {"trunc", 1, 1, f_trunc},
7986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007988 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007989 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007990 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {"virtcol", 1, 1, f_virtcol},
7992 {"visualmode", 0, 1, f_visualmode},
7993 {"winbufnr", 1, 1, f_winbufnr},
7994 {"wincol", 0, 0, f_wincol},
7995 {"winheight", 1, 1, f_winheight},
7996 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007997 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007999 {"winrestview", 1, 1, f_winrestview},
8000 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008002 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003};
8004
8005#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8006
8007/*
8008 * Function given to ExpandGeneric() to obtain the list of internal
8009 * or user defined function names.
8010 */
8011 char_u *
8012get_function_name(xp, idx)
8013 expand_T *xp;
8014 int idx;
8015{
8016 static int intidx = -1;
8017 char_u *name;
8018
8019 if (idx == 0)
8020 intidx = -1;
8021 if (intidx < 0)
8022 {
8023 name = get_user_func_name(xp, idx);
8024 if (name != NULL)
8025 return name;
8026 }
8027 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8028 {
8029 STRCPY(IObuff, functions[intidx].f_name);
8030 STRCAT(IObuff, "(");
8031 if (functions[intidx].f_max_argc == 0)
8032 STRCAT(IObuff, ")");
8033 return IObuff;
8034 }
8035
8036 return NULL;
8037}
8038
8039/*
8040 * Function given to ExpandGeneric() to obtain the list of internal or
8041 * user defined variable or function names.
8042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 char_u *
8044get_expr_name(xp, idx)
8045 expand_T *xp;
8046 int idx;
8047{
8048 static int intidx = -1;
8049 char_u *name;
8050
8051 if (idx == 0)
8052 intidx = -1;
8053 if (intidx < 0)
8054 {
8055 name = get_function_name(xp, idx);
8056 if (name != NULL)
8057 return name;
8058 }
8059 return get_user_var_name(xp, ++intidx);
8060}
8061
8062#endif /* FEAT_CMDL_COMPL */
8063
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008064#if defined(EBCDIC) || defined(PROTO)
8065/*
8066 * Compare struct fst by function name.
8067 */
8068 static int
8069compare_func_name(s1, s2)
8070 const void *s1;
8071 const void *s2;
8072{
8073 struct fst *p1 = (struct fst *)s1;
8074 struct fst *p2 = (struct fst *)s2;
8075
8076 return STRCMP(p1->f_name, p2->f_name);
8077}
8078
8079/*
8080 * Sort the function table by function name.
8081 * The sorting of the table above is ASCII dependant.
8082 * On machines using EBCDIC we have to sort it.
8083 */
8084 static void
8085sortFunctions()
8086{
8087 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8088
8089 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8090}
8091#endif
8092
8093
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094/*
8095 * Find internal function in table above.
8096 * Return index, or -1 if not found
8097 */
8098 static int
8099find_internal_func(name)
8100 char_u *name; /* name of the function */
8101{
8102 int first = 0;
8103 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8104 int cmp;
8105 int x;
8106
8107 /*
8108 * Find the function name in the table. Binary search.
8109 */
8110 while (first <= last)
8111 {
8112 x = first + ((unsigned)(last - first) >> 1);
8113 cmp = STRCMP(name, functions[x].f_name);
8114 if (cmp < 0)
8115 last = x - 1;
8116 else if (cmp > 0)
8117 first = x + 1;
8118 else
8119 return x;
8120 }
8121 return -1;
8122}
8123
8124/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8126 * name it contains, otherwise return "name".
8127 */
8128 static char_u *
8129deref_func_name(name, lenp)
8130 char_u *name;
8131 int *lenp;
8132{
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134 int cc;
8135
8136 cc = name[*lenp];
8137 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008138 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008139 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 {
8144 *lenp = 0;
8145 return (char_u *)""; /* just in case */
8146 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008147 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 }
8150
8151 return name;
8152}
8153
8154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 * Allocate a variable for the result of a function.
8156 * Return OK or FAIL.
8157 */
8158 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008159get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8160 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 char_u *name; /* name of the function */
8162 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 char_u **arg; /* argument, pointing to the '(' */
8165 linenr_T firstline; /* first line of range */
8166 linenr_T lastline; /* last line of range */
8167 int *doesrange; /* return: function handled range */
8168 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
8171 char_u *argp;
8172 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008173 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 int argcount = 0; /* number of arguments found */
8175
8176 /*
8177 * Get the arguments.
8178 */
8179 argp = *arg;
8180 while (argcount < MAX_FUNC_ARGS)
8181 {
8182 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8183 if (*argp == ')' || *argp == ',' || *argp == NUL)
8184 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8186 {
8187 ret = FAIL;
8188 break;
8189 }
8190 ++argcount;
8191 if (*argp != ',')
8192 break;
8193 }
8194 if (*argp == ')')
8195 ++argp;
8196 else
8197 ret = FAIL;
8198
8199 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008200 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 {
8204 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008205 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008206 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008207 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
8210 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008211 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212
8213 *arg = skipwhite(argp);
8214 return ret;
8215}
8216
8217
8218/*
8219 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008220 * Return OK when the function can't be called, FAIL otherwise.
8221 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 */
8223 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008224call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008225 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008226 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008230 typval_T *argvars; /* vars for arguments, must have "argcount"
8231 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 linenr_T firstline; /* first line of range */
8233 linenr_T lastline; /* last line of range */
8234 int *doesrange; /* return: function handled range */
8235 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008236 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239#define ERROR_UNKNOWN 0
8240#define ERROR_TOOMANY 1
8241#define ERROR_TOOFEW 2
8242#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008243#define ERROR_DICT 4
8244#define ERROR_NONE 5
8245#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 int error = ERROR_NONE;
8247 int i;
8248 int llen;
8249 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250#define FLEN_FIXED 40
8251 char_u fname_buf[FLEN_FIXED + 1];
8252 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008253 char_u *name;
8254
8255 /* Make a copy of the name, if it comes from a funcref variable it could
8256 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008257 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008258 if (name == NULL)
8259 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260
8261 /*
8262 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8263 * Change <SNR>123_name() to K_SNR 123_name().
8264 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 llen = eval_fname_script(name);
8267 if (llen > 0)
8268 {
8269 fname_buf[0] = K_SPECIAL;
8270 fname_buf[1] = KS_EXTRA;
8271 fname_buf[2] = (int)KE_SNR;
8272 i = 3;
8273 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8274 {
8275 if (current_SID <= 0)
8276 error = ERROR_SCRIPT;
8277 else
8278 {
8279 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8280 i = (int)STRLEN(fname_buf);
8281 }
8282 }
8283 if (i + STRLEN(name + llen) < FLEN_FIXED)
8284 {
8285 STRCPY(fname_buf + i, name + llen);
8286 fname = fname_buf;
8287 }
8288 else
8289 {
8290 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8291 if (fname == NULL)
8292 error = ERROR_OTHER;
8293 else
8294 {
8295 mch_memmove(fname, fname_buf, (size_t)i);
8296 STRCPY(fname + i, name + llen);
8297 }
8298 }
8299 }
8300 else
8301 fname = name;
8302
8303 *doesrange = FALSE;
8304
8305
8306 /* execute the function if no errors detected and executing */
8307 if (evaluate && error == ERROR_NONE)
8308 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008309 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8310 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 error = ERROR_UNKNOWN;
8312
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008313 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {
8315 /*
8316 * User defined function.
8317 */
8318 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008319
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008321 /* Trigger FuncUndefined event, may load the function. */
8322 if (fp == NULL
8323 && apply_autocmds(EVENT_FUNCUNDEFINED,
8324 fname, fname, TRUE, NULL)
8325 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008327 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 fp = find_func(fname);
8329 }
8330#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008331 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008332 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008333 {
8334 /* loaded a package, search for the function again */
8335 fp = find_func(fname);
8336 }
8337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (fp != NULL)
8339 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008340 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008342 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008344 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008346 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008347 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 else
8349 {
8350 /*
8351 * Call the user function.
8352 * Save and restore search patterns, script variables and
8353 * redo buffer.
8354 */
8355 save_search_patterns();
8356 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008357 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008358 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008359 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008360 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8361 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8362 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008363 /* Function was unreferenced while being used, free it
8364 * now. */
8365 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 restoreRedobuff();
8367 restore_search_patterns();
8368 error = ERROR_NONE;
8369 }
8370 }
8371 }
8372 else
8373 {
8374 /*
8375 * Find the function name in the table, call its implementation.
8376 */
8377 i = find_internal_func(fname);
8378 if (i >= 0)
8379 {
8380 if (argcount < functions[i].f_min_argc)
8381 error = ERROR_TOOFEW;
8382 else if (argcount > functions[i].f_max_argc)
8383 error = ERROR_TOOMANY;
8384 else
8385 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008386 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 error = ERROR_NONE;
8389 }
8390 }
8391 }
8392 /*
8393 * The function call (or "FuncUndefined" autocommand sequence) might
8394 * have been aborted by an error, an interrupt, or an explicitly thrown
8395 * exception that has not been caught so far. This situation can be
8396 * tested for by calling aborting(). For an error in an internal
8397 * function or for the "E132" error in call_user_func(), however, the
8398 * throw point at which the "force_abort" flag (temporarily reset by
8399 * emsg()) is normally updated has not been reached yet. We need to
8400 * update that flag first to make aborting() reliable.
8401 */
8402 update_force_abort();
8403 }
8404 if (error == ERROR_NONE)
8405 ret = OK;
8406
8407 /*
8408 * Report an error unless the argument evaluation or function call has been
8409 * cancelled due to an aborting error, an interrupt, or an exception.
8410 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008411 if (!aborting())
8412 {
8413 switch (error)
8414 {
8415 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008416 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008417 break;
8418 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008419 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008420 break;
8421 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008422 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008423 name);
8424 break;
8425 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008426 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008427 name);
8428 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008429 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008430 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008431 name);
8432 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008433 }
8434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 if (fname != name && fname != fname_buf)
8437 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008438 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439
8440 return ret;
8441}
8442
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008443/*
8444 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008445 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008446 */
8447 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008448emsg_funcname(ermsg, name)
8449 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008450 char_u *name;
8451{
8452 char_u *p;
8453
8454 if (*name == K_SPECIAL)
8455 p = concat_str((char_u *)"<SNR>", name + 3);
8456 else
8457 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008458 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008459 if (p != name)
8460 vim_free(p);
8461}
8462
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008463/*
8464 * Return TRUE for a non-zero Number and a non-empty String.
8465 */
8466 static int
8467non_zero_arg(argvars)
8468 typval_T *argvars;
8469{
8470 return ((argvars[0].v_type == VAR_NUMBER
8471 && argvars[0].vval.v_number != 0)
8472 || (argvars[0].v_type == VAR_STRING
8473 && argvars[0].vval.v_string != NULL
8474 && *argvars[0].vval.v_string != NUL));
8475}
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477/*********************************************
8478 * Implementation of the built-in functions
8479 */
8480
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008481#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008482static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8483
8484/*
8485 * Get the float value of "argvars[0]" into "f".
8486 * Returns FAIL when the argument is not a Number or Float.
8487 */
8488 static int
8489get_float_arg(argvars, f)
8490 typval_T *argvars;
8491 float_T *f;
8492{
8493 if (argvars[0].v_type == VAR_FLOAT)
8494 {
8495 *f = argvars[0].vval.v_float;
8496 return OK;
8497 }
8498 if (argvars[0].v_type == VAR_NUMBER)
8499 {
8500 *f = (float_T)argvars[0].vval.v_number;
8501 return OK;
8502 }
8503 EMSG(_("E808: Number or Float required"));
8504 return FAIL;
8505}
8506
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008507/*
8508 * "abs(expr)" function
8509 */
8510 static void
8511f_abs(argvars, rettv)
8512 typval_T *argvars;
8513 typval_T *rettv;
8514{
8515 if (argvars[0].v_type == VAR_FLOAT)
8516 {
8517 rettv->v_type = VAR_FLOAT;
8518 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8519 }
8520 else
8521 {
8522 varnumber_T n;
8523 int error = FALSE;
8524
8525 n = get_tv_number_chk(&argvars[0], &error);
8526 if (error)
8527 rettv->vval.v_number = -1;
8528 else if (n > 0)
8529 rettv->vval.v_number = n;
8530 else
8531 rettv->vval.v_number = -n;
8532 }
8533}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008534
8535/*
8536 * "acos()" function
8537 */
8538 static void
8539f_acos(argvars, rettv)
8540 typval_T *argvars;
8541 typval_T *rettv;
8542{
8543 float_T f;
8544
8545 rettv->v_type = VAR_FLOAT;
8546 if (get_float_arg(argvars, &f) == OK)
8547 rettv->vval.v_float = acos(f);
8548 else
8549 rettv->vval.v_float = 0.0;
8550}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008551#endif
8552
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008554 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 */
8556 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008557f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008558 typval_T *argvars;
8559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008564 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008566 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008567 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008568 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 }
8571 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008572 EMSG(_(e_listreq));
8573}
8574
8575/*
8576 * "append(lnum, string/list)" function
8577 */
8578 static void
8579f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008580 typval_T *argvars;
8581 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008582{
8583 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008584 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008585 list_T *l = NULL;
8586 listitem_T *li = NULL;
8587 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008588 long added = 0;
8589
Bram Moolenaar0d660222005-01-07 21:51:51 +00008590 lnum = get_tv_lnum(argvars);
8591 if (lnum >= 0
8592 && lnum <= curbuf->b_ml.ml_line_count
8593 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008594 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008595 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008596 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008597 l = argvars[1].vval.v_list;
8598 if (l == NULL)
8599 return;
8600 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008601 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008602 for (;;)
8603 {
8604 if (l == NULL)
8605 tv = &argvars[1]; /* append a string */
8606 else if (li == NULL)
8607 break; /* end of list */
8608 else
8609 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008610 line = get_tv_string_chk(tv);
8611 if (line == NULL) /* type error */
8612 {
8613 rettv->vval.v_number = 1; /* Failed */
8614 break;
8615 }
8616 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008617 ++added;
8618 if (l == NULL)
8619 break;
8620 li = li->li_next;
8621 }
8622
8623 appended_lines_mark(lnum, added);
8624 if (curwin->w_cursor.lnum > lnum)
8625 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008627 else
8628 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629}
8630
8631/*
8632 * "argc()" function
8633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008635f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640}
8641
8642/*
8643 * "argidx()" function
8644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008647 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008650 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651}
8652
8653/*
8654 * "argv(nr)" function
8655 */
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 int idx;
8662
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008663 if (argvars[0].v_type != VAR_UNKNOWN)
8664 {
8665 idx = get_tv_number_chk(&argvars[0], NULL);
8666 if (idx >= 0 && idx < ARGCOUNT)
8667 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8668 else
8669 rettv->vval.v_string = NULL;
8670 rettv->v_type = VAR_STRING;
8671 }
8672 else if (rettv_list_alloc(rettv) == OK)
8673 for (idx = 0; idx < ARGCOUNT; ++idx)
8674 list_append_string(rettv->vval.v_list,
8675 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676}
8677
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008678#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008679/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008680 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008681 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008682 static void
8683f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008684 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008685 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008687 float_T f;
8688
8689 rettv->v_type = VAR_FLOAT;
8690 if (get_float_arg(argvars, &f) == OK)
8691 rettv->vval.v_float = asin(f);
8692 else
8693 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008694}
8695
8696/*
8697 * "atan()" function
8698 */
8699 static void
8700f_atan(argvars, rettv)
8701 typval_T *argvars;
8702 typval_T *rettv;
8703{
8704 float_T f;
8705
8706 rettv->v_type = VAR_FLOAT;
8707 if (get_float_arg(argvars, &f) == OK)
8708 rettv->vval.v_float = atan(f);
8709 else
8710 rettv->vval.v_float = 0.0;
8711}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008712
8713/*
8714 * "atan2()" function
8715 */
8716 static void
8717f_atan2(argvars, rettv)
8718 typval_T *argvars;
8719 typval_T *rettv;
8720{
8721 float_T fx, fy;
8722
8723 rettv->v_type = VAR_FLOAT;
8724 if (get_float_arg(argvars, &fx) == OK
8725 && get_float_arg(&argvars[1], &fy) == OK)
8726 rettv->vval.v_float = atan2(fx, fy);
8727 else
8728 rettv->vval.v_float = 0.0;
8729}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008730#endif
8731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732/*
8733 * "browse(save, title, initdir, default)" function
8734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008737 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
8740#ifdef FEAT_BROWSE
8741 int save;
8742 char_u *title;
8743 char_u *initdir;
8744 char_u *defname;
8745 char_u buf[NUMBUFLEN];
8746 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008747 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008749 save = get_tv_number_chk(&argvars[0], &error);
8750 title = get_tv_string_chk(&argvars[1]);
8751 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8752 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008754 if (error || title == NULL || initdir == NULL || defname == NULL)
8755 rettv->vval.v_string = NULL;
8756 else
8757 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008758 do_browse(save ? BROWSE_SAVE : 0,
8759 title, defname, NULL, initdir, NULL, curbuf);
8760#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008762#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008764}
8765
8766/*
8767 * "browsedir(title, initdir)" function
8768 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008771 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008772 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008773{
8774#ifdef FEAT_BROWSE
8775 char_u *title;
8776 char_u *initdir;
8777 char_u buf[NUMBUFLEN];
8778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008779 title = get_tv_string_chk(&argvars[0]);
8780 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008782 if (title == NULL || initdir == NULL)
8783 rettv->vval.v_string = NULL;
8784 else
8785 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008786 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791}
8792
Bram Moolenaar33570922005-01-25 22:26:29 +00008793static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008794
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795/*
8796 * Find a buffer by number or exact name.
8797 */
8798 static buf_T *
8799find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008804 if (avar->v_type == VAR_NUMBER)
8805 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008806 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008808 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008809 if (buf == NULL)
8810 {
8811 /* No full path name match, try a match with a URL or a "nofile"
8812 * buffer, these don't use the full path. */
8813 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8814 if (buf->b_fname != NULL
8815 && (path_with_url(buf->b_fname)
8816#ifdef FEAT_QUICKFIX
8817 || bt_nofile(buf)
8818#endif
8819 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008820 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008821 break;
8822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 }
8824 return buf;
8825}
8826
8827/*
8828 * "bufexists(expr)" function
8829 */
8830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 typval_T *argvars;
8833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836}
8837
8838/*
8839 * "buflisted(expr)" function
8840 */
8841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008843 typval_T *argvars;
8844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
8846 buf_T *buf;
8847
8848 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850}
8851
8852/*
8853 * "bufloaded(expr)" function
8854 */
8855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 typval_T *argvars;
8858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 buf_T *buf;
8861
8862 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864}
8865
Bram Moolenaar33570922005-01-25 22:26:29 +00008866static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008867
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868/*
8869 * Get buffer by number or pattern.
8870 */
8871 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008873 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008875 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 int save_magic;
8877 char_u *save_cpo;
8878 buf_T *buf;
8879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880 if (tv->v_type == VAR_NUMBER)
8881 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008882 if (tv->v_type != VAR_STRING)
8883 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 if (name == NULL || *name == NUL)
8885 return curbuf;
8886 if (name[0] == '$' && name[1] == NUL)
8887 return lastbuf;
8888
8889 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8890 save_magic = p_magic;
8891 p_magic = TRUE;
8892 save_cpo = p_cpo;
8893 p_cpo = (char_u *)"";
8894
8895 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8896 TRUE, FALSE));
8897
8898 p_magic = save_magic;
8899 p_cpo = save_cpo;
8900
8901 /* If not found, try expanding the name, like done for bufexists(). */
8902 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008903 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
8905 return buf;
8906}
8907
8908/*
8909 * "bufname(expr)" function
8910 */
8911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008913 typval_T *argvars;
8914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 buf_T *buf;
8917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008918 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 buf = get_buf_tv(&argvars[0]);
8921 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 --emsg_off;
8927}
8928
8929/*
8930 * "bufnr(expr)" function
8931 */
8932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *argvars;
8935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008938 int error = FALSE;
8939 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008944 --emsg_off;
8945
8946 /* If the buffer isn't found and the second argument is not zero create a
8947 * new buffer. */
8948 if (buf == NULL
8949 && argvars[1].v_type != VAR_UNKNOWN
8950 && get_tv_number_chk(&argvars[1], &error) != 0
8951 && !error
8952 && (name = get_tv_string_chk(&argvars[0])) != NULL
8953 && !error)
8954 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8955
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960}
8961
8962/*
8963 * "bufwinnr(nr)" function
8964 */
8965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008967 typval_T *argvars;
8968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970#ifdef FEAT_WINDOWS
8971 win_T *wp;
8972 int winnr = 0;
8973#endif
8974 buf_T *buf;
8975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979#ifdef FEAT_WINDOWS
8980 for (wp = firstwin; wp; wp = wp->w_next)
8981 {
8982 ++winnr;
8983 if (wp->w_buffer == buf)
8984 break;
8985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989#endif
8990 --emsg_off;
8991}
8992
8993/*
8994 * "byte2line(byte)" function
8995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008998 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003#else
9004 long boff = 0;
9005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 (linenr_T)0, &boff);
9012#endif
9013}
9014
9015/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009016 * "byteidx()" function
9017 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009020 typval_T *argvars;
9021 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009022{
9023#ifdef FEAT_MBYTE
9024 char_u *t;
9025#endif
9026 char_u *str;
9027 long idx;
9028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009029 str = get_tv_string_chk(&argvars[0]);
9030 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009032 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009033 return;
9034
9035#ifdef FEAT_MBYTE
9036 t = str;
9037 for ( ; idx > 0; idx--)
9038 {
9039 if (*t == NUL) /* EOL reached */
9040 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009041 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009042 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009043 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009044#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009045 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009047#endif
9048}
9049
9050/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009051 * "call(func, arglist)" function
9052 */
9053 static void
9054f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009055 typval_T *argvars;
9056 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009057{
9058 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009059 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009060 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009061 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009062 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009064
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009065 if (argvars[1].v_type != VAR_LIST)
9066 {
9067 EMSG(_(e_listreq));
9068 return;
9069 }
9070 if (argvars[1].vval.v_list == NULL)
9071 return;
9072
9073 if (argvars[0].v_type == VAR_FUNC)
9074 func = argvars[0].vval.v_string;
9075 else
9076 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077 if (*func == NUL)
9078 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009079
Bram Moolenaare9a41262005-01-15 22:18:47 +00009080 if (argvars[2].v_type != VAR_UNKNOWN)
9081 {
9082 if (argvars[2].v_type != VAR_DICT)
9083 {
9084 EMSG(_(e_dictreq));
9085 return;
9086 }
9087 selfdict = argvars[2].vval.v_dict;
9088 }
9089
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009090 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9091 item = item->li_next)
9092 {
9093 if (argc == MAX_FUNC_ARGS)
9094 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009095 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009096 break;
9097 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009098 /* Make a copy of each argument. This is needed to be able to set
9099 * v_lock to VAR_FIXED in the copy without changing the original list.
9100 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009101 copy_tv(&item->li_tv, &argv[argc++]);
9102 }
9103
9104 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009105 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009106 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9107 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009108
9109 /* Free the arguments. */
9110 while (argc > 0)
9111 clear_tv(&argv[--argc]);
9112}
9113
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009114#ifdef FEAT_FLOAT
9115/*
9116 * "ceil({float})" function
9117 */
9118 static void
9119f_ceil(argvars, rettv)
9120 typval_T *argvars;
9121 typval_T *rettv;
9122{
9123 float_T f;
9124
9125 rettv->v_type = VAR_FLOAT;
9126 if (get_float_arg(argvars, &f) == OK)
9127 rettv->vval.v_float = ceil(f);
9128 else
9129 rettv->vval.v_float = 0.0;
9130}
9131#endif
9132
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009133/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009134 * "changenr()" function
9135 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009136 static void
9137f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009138 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009139 typval_T *rettv;
9140{
9141 rettv->vval.v_number = curbuf->b_u_seq_cur;
9142}
9143
9144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 * "char2nr(string)" function
9146 */
9147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009149 typval_T *argvars;
9150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151{
9152#ifdef FEAT_MBYTE
9153 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 else
9156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158}
9159
9160/*
9161 * "cindent(lnum)" function
9162 */
9163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 typval_T *argvars;
9166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168#ifdef FEAT_CINDENT
9169 pos_T pos;
9170 linenr_T lnum;
9171
9172 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9175 {
9176 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 curwin->w_cursor = pos;
9179 }
9180 else
9181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183}
9184
9185/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009186 * "clearmatches()" function
9187 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009188 static void
9189f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009190 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009191 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009192{
9193#ifdef FEAT_SEARCH_EXTRA
9194 clear_matches(curwin);
9195#endif
9196}
9197
9198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 * "col(string)" function
9200 */
9201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009203 typval_T *argvars;
9204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
9206 colnr_T col = 0;
9207 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009208 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009210 fp = var2fpos(&argvars[0], FALSE, &fnum);
9211 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 {
9213 if (fp->col == MAXCOL)
9214 {
9215 /* '> can be MAXCOL, get the length of the line then */
9216 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009217 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 else
9219 col = MAXCOL;
9220 }
9221 else
9222 {
9223 col = fp->col + 1;
9224#ifdef FEAT_VIRTUALEDIT
9225 /* col(".") when the cursor is on the NUL at the end of the line
9226 * because of "coladd" can be seen as an extra column. */
9227 if (virtual_active() && fp == &curwin->w_cursor)
9228 {
9229 char_u *p = ml_get_cursor();
9230
9231 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9232 curwin->w_virtcol - curwin->w_cursor.coladd))
9233 {
9234# ifdef FEAT_MBYTE
9235 int l;
9236
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009237 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 col += l;
9239# else
9240 if (*p != NUL && p[1] == NUL)
9241 ++col;
9242# endif
9243 }
9244 }
9245#endif
9246 }
9247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009248 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
Bram Moolenaar572cb562005-08-05 21:35:02 +00009251#if defined(FEAT_INS_EXPAND)
9252/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009253 * "complete()" function
9254 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009255 static void
9256f_complete(argvars, rettv)
9257 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009258 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009259{
9260 int startcol;
9261
9262 if ((State & INSERT) == 0)
9263 {
9264 EMSG(_("E785: complete() can only be used in Insert mode"));
9265 return;
9266 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009267
9268 /* Check for undo allowed here, because if something was already inserted
9269 * the line was already saved for undo and this check isn't done. */
9270 if (!undo_allowed())
9271 return;
9272
Bram Moolenaarade00832006-03-10 21:46:58 +00009273 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9274 {
9275 EMSG(_(e_invarg));
9276 return;
9277 }
9278
9279 startcol = get_tv_number_chk(&argvars[0], NULL);
9280 if (startcol <= 0)
9281 return;
9282
9283 set_completion(startcol - 1, argvars[1].vval.v_list);
9284}
9285
9286/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009287 * "complete_add()" function
9288 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009289 static void
9290f_complete_add(argvars, rettv)
9291 typval_T *argvars;
9292 typval_T *rettv;
9293{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009294 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009295}
9296
9297/*
9298 * "complete_check()" function
9299 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009300 static void
9301f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009302 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009303 typval_T *rettv;
9304{
9305 int saved = RedrawingDisabled;
9306
9307 RedrawingDisabled = 0;
9308 ins_compl_check_keys(0);
9309 rettv->vval.v_number = compl_interrupted;
9310 RedrawingDisabled = saved;
9311}
9312#endif
9313
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314/*
9315 * "confirm(message, buttons[, default [, type]])" function
9316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009319 typval_T *argvars UNUSED;
9320 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9323 char_u *message;
9324 char_u *buttons = NULL;
9325 char_u buf[NUMBUFLEN];
9326 char_u buf2[NUMBUFLEN];
9327 int def = 1;
9328 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 char_u *typestr;
9330 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 message = get_tv_string_chk(&argvars[0]);
9333 if (message == NULL)
9334 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009335 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9338 if (buttons == NULL)
9339 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009340 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009343 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9346 if (typestr == NULL)
9347 error = TRUE;
9348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009350 switch (TOUPPER_ASC(*typestr))
9351 {
9352 case 'E': type = VIM_ERROR; break;
9353 case 'Q': type = VIM_QUESTION; break;
9354 case 'I': type = VIM_INFO; break;
9355 case 'W': type = VIM_WARNING; break;
9356 case 'G': type = VIM_GENERIC; break;
9357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 }
9359 }
9360 }
9361 }
9362
9363 if (buttons == NULL || *buttons == NUL)
9364 buttons = (char_u *)_("&Ok");
9365
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009366 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009368 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369#endif
9370}
9371
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009372/*
9373 * "copy()" function
9374 */
9375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009377 typval_T *argvars;
9378 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009379{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009380 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009383#ifdef FEAT_FLOAT
9384/*
9385 * "cos()" function
9386 */
9387 static void
9388f_cos(argvars, rettv)
9389 typval_T *argvars;
9390 typval_T *rettv;
9391{
9392 float_T f;
9393
9394 rettv->v_type = VAR_FLOAT;
9395 if (get_float_arg(argvars, &f) == OK)
9396 rettv->vval.v_float = cos(f);
9397 else
9398 rettv->vval.v_float = 0.0;
9399}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009400
9401/*
9402 * "cosh()" function
9403 */
9404 static void
9405f_cosh(argvars, rettv)
9406 typval_T *argvars;
9407 typval_T *rettv;
9408{
9409 float_T f;
9410
9411 rettv->v_type = VAR_FLOAT;
9412 if (get_float_arg(argvars, &f) == OK)
9413 rettv->vval.v_float = cosh(f);
9414 else
9415 rettv->vval.v_float = 0.0;
9416}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009417#endif
9418
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009420 * "count()" function
9421 */
9422 static void
9423f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009424 typval_T *argvars;
9425 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009426{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009427 long n = 0;
9428 int ic = FALSE;
9429
Bram Moolenaare9a41262005-01-15 22:18:47 +00009430 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009431 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 listitem_T *li;
9433 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009434 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009435
Bram Moolenaare9a41262005-01-15 22:18:47 +00009436 if ((l = argvars[0].vval.v_list) != NULL)
9437 {
9438 li = l->lv_first;
9439 if (argvars[2].v_type != VAR_UNKNOWN)
9440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441 int error = FALSE;
9442
9443 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009444 if (argvars[3].v_type != VAR_UNKNOWN)
9445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009446 idx = get_tv_number_chk(&argvars[3], &error);
9447 if (!error)
9448 {
9449 li = list_find(l, idx);
9450 if (li == NULL)
9451 EMSGN(_(e_listidx), idx);
9452 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009453 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009454 if (error)
9455 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009456 }
9457
9458 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009459 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009460 ++n;
9461 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009463 else if (argvars[0].v_type == VAR_DICT)
9464 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009465 int todo;
9466 dict_T *d;
9467 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009468
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009469 if ((d = argvars[0].vval.v_dict) != NULL)
9470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471 int error = FALSE;
9472
Bram Moolenaare9a41262005-01-15 22:18:47 +00009473 if (argvars[2].v_type != VAR_UNKNOWN)
9474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009475 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009476 if (argvars[3].v_type != VAR_UNKNOWN)
9477 EMSG(_(e_invarg));
9478 }
9479
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009480 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009481 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009482 {
9483 if (!HASHITEM_EMPTY(hi))
9484 {
9485 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009486 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009487 ++n;
9488 }
9489 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009490 }
9491 }
9492 else
9493 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009494 rettv->vval.v_number = n;
9495}
9496
9497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9499 *
9500 * Checks the existence of a cscope connection.
9501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009504 typval_T *argvars UNUSED;
9505 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506{
9507#ifdef FEAT_CSCOPE
9508 int num = 0;
9509 char_u *dbpath = NULL;
9510 char_u *prepend = NULL;
9511 char_u buf[NUMBUFLEN];
9512
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 if (argvars[0].v_type != VAR_UNKNOWN
9514 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 num = (int)get_tv_number(&argvars[0]);
9517 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 }
9521
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523#endif
9524}
9525
9526/*
9527 * "cursor(lnum, col)" function
9528 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009529 * Moves the cursor to the specified line and column.
9530 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009534 typval_T *argvars;
9535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
9537 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009538#ifdef FEAT_VIRTUALEDIT
9539 long coladd = 0;
9540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009542 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009543 if (argvars[1].v_type == VAR_UNKNOWN)
9544 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009545 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009546
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009547 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009548 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009549 line = pos.lnum;
9550 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009551#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009552 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009553#endif
9554 }
9555 else
9556 {
9557 line = get_tv_lnum(argvars);
9558 col = get_tv_number_chk(&argvars[1], NULL);
9559#ifdef FEAT_VIRTUALEDIT
9560 if (argvars[2].v_type != VAR_UNKNOWN)
9561 coladd = get_tv_number_chk(&argvars[2], NULL);
9562#endif
9563 }
9564 if (line < 0 || col < 0
9565#ifdef FEAT_VIRTUALEDIT
9566 || coladd < 0
9567#endif
9568 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 if (line > 0)
9571 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 if (col > 0)
9573 curwin->w_cursor.col = col - 1;
9574#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009575 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576#endif
9577
9578 /* Make sure the cursor is in a valid position. */
9579 check_cursor();
9580#ifdef FEAT_MBYTE
9581 /* Correct cursor for multi-byte character. */
9582 if (has_mbyte)
9583 mb_adjust_cursor();
9584#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009585
9586 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009587 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588}
9589
9590/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 */
9593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009595 typval_T *argvars;
9596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009598 int noref = 0;
9599
9600 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009602 if (noref < 0 || noref > 1)
9603 EMSG(_(e_invarg));
9604 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009605 {
9606 current_copyID += COPYID_INC;
9607 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609}
9610
9611/*
9612 * "delete()" function
9613 */
9614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009616 typval_T *argvars;
9617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
9619 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623}
9624
9625/*
9626 * "did_filetype()" function
9627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009630 typval_T *argvars UNUSED;
9631 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635#endif
9636}
9637
9638/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009639 * "diff_filler()" function
9640 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009643 typval_T *argvars UNUSED;
9644 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009645{
9646#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009648#endif
9649}
9650
9651/*
9652 * "diff_hlID()" function
9653 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009656 typval_T *argvars UNUSED;
9657 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009658{
9659#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009661 static linenr_T prev_lnum = 0;
9662 static int changedtick = 0;
9663 static int fnum = 0;
9664 static int change_start = 0;
9665 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009666 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009667 int filler_lines;
9668 int col;
9669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009670 if (lnum < 0) /* ignore type error in {lnum} arg */
9671 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009672 if (lnum != prev_lnum
9673 || changedtick != curbuf->b_changedtick
9674 || fnum != curbuf->b_fnum)
9675 {
9676 /* New line, buffer, change: need to get the values. */
9677 filler_lines = diff_check(curwin, lnum);
9678 if (filler_lines < 0)
9679 {
9680 if (filler_lines == -1)
9681 {
9682 change_start = MAXCOL;
9683 change_end = -1;
9684 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9685 hlID = HLF_ADD; /* added line */
9686 else
9687 hlID = HLF_CHD; /* changed line */
9688 }
9689 else
9690 hlID = HLF_ADD; /* added line */
9691 }
9692 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009693 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009694 prev_lnum = lnum;
9695 changedtick = curbuf->b_changedtick;
9696 fnum = curbuf->b_fnum;
9697 }
9698
9699 if (hlID == HLF_CHD || hlID == HLF_TXD)
9700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009701 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009702 if (col >= change_start && col <= change_end)
9703 hlID = HLF_TXD; /* changed text */
9704 else
9705 hlID = HLF_CHD; /* changed line */
9706 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009707 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009708#endif
9709}
9710
9711/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009712 * "empty({expr})" function
9713 */
9714 static void
9715f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009716 typval_T *argvars;
9717 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009718{
9719 int n;
9720
9721 switch (argvars[0].v_type)
9722 {
9723 case VAR_STRING:
9724 case VAR_FUNC:
9725 n = argvars[0].vval.v_string == NULL
9726 || *argvars[0].vval.v_string == NUL;
9727 break;
9728 case VAR_NUMBER:
9729 n = argvars[0].vval.v_number == 0;
9730 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009731#ifdef FEAT_FLOAT
9732 case VAR_FLOAT:
9733 n = argvars[0].vval.v_float == 0.0;
9734 break;
9735#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009736 case VAR_LIST:
9737 n = argvars[0].vval.v_list == NULL
9738 || argvars[0].vval.v_list->lv_first == NULL;
9739 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009740 case VAR_DICT:
9741 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009742 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009743 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009744 default:
9745 EMSG2(_(e_intern2), "f_empty()");
9746 n = 0;
9747 }
9748
9749 rettv->vval.v_number = n;
9750}
9751
9752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 * "escape({string}, {chars})" function
9754 */
9755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009757 typval_T *argvars;
9758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760 char_u buf[NUMBUFLEN];
9761
Bram Moolenaar758711c2005-02-02 23:11:38 +00009762 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9763 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765}
9766
9767/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009768 * "eval()" function
9769 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009770 static void
9771f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009772 typval_T *argvars;
9773 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009774{
9775 char_u *s;
9776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 s = get_tv_string_chk(&argvars[0]);
9778 if (s != NULL)
9779 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9782 {
9783 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009784 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009786 else if (*s != NUL)
9787 EMSG(_(e_trailing));
9788}
9789
9790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 * "eventhandler()" function
9792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009795 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799}
9800
9801/*
9802 * "executable()" function
9803 */
9804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 typval_T *argvars;
9807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810}
9811
9812/*
9813 * "exists()" function
9814 */
9815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009817 typval_T *argvars;
9818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819{
9820 char_u *p;
9821 char_u *name;
9822 int n = FALSE;
9823 int len = 0;
9824
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009825 no_autoload = TRUE;
9826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (*p == '$') /* environment variable */
9829 {
9830 /* first try "normal" environment variables (fast) */
9831 if (mch_getenv(p + 1) != NULL)
9832 n = TRUE;
9833 else
9834 {
9835 /* try expanding things like $VIM and ${HOME} */
9836 p = expand_env_save(p);
9837 if (p != NULL && *p != '$')
9838 n = TRUE;
9839 vim_free(p);
9840 }
9841 }
9842 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009845 if (*skipwhite(p) != NUL)
9846 n = FALSE; /* trailing garbage */
9847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 else if (*p == '*') /* internal or user defined function */
9849 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009850 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 }
9852 else if (*p == ':')
9853 {
9854 n = cmd_exists(p + 1);
9855 }
9856 else if (*p == '#')
9857 {
9858#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009859 if (p[1] == '#')
9860 n = autocmd_supported(p + 2);
9861 else
9862 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863#endif
9864 }
9865 else /* internal variable */
9866 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009867 char_u *tofree;
9868 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009870 /* get_name_len() takes care of expanding curly braces */
9871 name = p;
9872 len = get_name_len(&p, &tofree, TRUE, FALSE);
9873 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009875 if (tofree != NULL)
9876 name = tofree;
9877 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9878 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009880 /* handle d.key, l[idx], f(expr) */
9881 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9882 if (n)
9883 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 }
9885 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009886 if (*p != NUL)
9887 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009889 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 }
9891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009893
9894 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895}
9896
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009897#ifdef FEAT_FLOAT
9898/*
9899 * "exp()" function
9900 */
9901 static void
9902f_exp(argvars, rettv)
9903 typval_T *argvars;
9904 typval_T *rettv;
9905{
9906 float_T f;
9907
9908 rettv->v_type = VAR_FLOAT;
9909 if (get_float_arg(argvars, &f) == OK)
9910 rettv->vval.v_float = exp(f);
9911 else
9912 rettv->vval.v_float = 0.0;
9913}
9914#endif
9915
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916/*
9917 * "expand()" function
9918 */
9919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009920f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009921 typval_T *argvars;
9922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
9924 char_u *s;
9925 int len;
9926 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009927 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 rettv->v_type = VAR_STRING;
9932 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 if (*s == '%' || *s == '#' || *s == '<')
9934 {
9935 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009936 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 --emsg_off;
9938 }
9939 else
9940 {
9941 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009942 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009943 if (argvars[1].v_type != VAR_UNKNOWN
9944 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009945 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 if (!error)
9947 {
9948 ExpandInit(&xpc);
9949 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009950 if (p_wic)
9951 options += WILD_ICASE;
9952 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009953 }
9954 else
9955 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 }
9957}
9958
9959/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009960 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009961 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009962 */
9963 static void
9964f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 typval_T *argvars;
9966 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009967{
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009968 char *arg_errmsg = N_("extend() argument");
9969
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009971 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009972 list_T *l1, *l2;
9973 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009975 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009976
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 l1 = argvars[0].vval.v_list;
9978 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009979 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009980 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009981 {
9982 if (argvars[2].v_type != VAR_UNKNOWN)
9983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009984 before = get_tv_number_chk(&argvars[2], &error);
9985 if (error)
9986 return; /* type error; errmsg already given */
9987
Bram Moolenaar758711c2005-02-02 23:11:38 +00009988 if (before == l1->lv_len)
9989 item = NULL;
9990 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009991 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009992 item = list_find(l1, before);
9993 if (item == NULL)
9994 {
9995 EMSGN(_(e_listidx), before);
9996 return;
9997 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009998 }
9999 }
10000 else
10001 item = NULL;
10002 list_extend(l1, l2, item);
10003
Bram Moolenaare9a41262005-01-15 22:18:47 +000010004 copy_tv(&argvars[0], rettv);
10005 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010006 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010007 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10008 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010009 dict_T *d1, *d2;
10010 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010011 char_u *action;
10012 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010013 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010014 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010015
10016 d1 = argvars[0].vval.v_dict;
10017 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010018 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010019 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010020 {
10021 /* Check the third argument. */
10022 if (argvars[2].v_type != VAR_UNKNOWN)
10023 {
10024 static char *(av[]) = {"keep", "force", "error"};
10025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010026 action = get_tv_string_chk(&argvars[2]);
10027 if (action == NULL)
10028 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010029 for (i = 0; i < 3; ++i)
10030 if (STRCMP(action, av[i]) == 0)
10031 break;
10032 if (i == 3)
10033 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010034 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010035 return;
10036 }
10037 }
10038 else
10039 action = (char_u *)"force";
10040
10041 /* Go over all entries in the second dict and add them to the
10042 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010043 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010044 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010045 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010046 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010048 --todo;
10049 di1 = dict_find(d1, hi2->hi_key, -1);
10050 if (di1 == NULL)
10051 {
10052 di1 = dictitem_copy(HI2DI(hi2));
10053 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10054 dictitem_free(di1);
10055 }
10056 else if (*action == 'e')
10057 {
10058 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10059 break;
10060 }
10061 else if (*action == 'f')
10062 {
10063 clear_tv(&di1->di_tv);
10064 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10065 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010066 }
10067 }
10068
Bram Moolenaare9a41262005-01-15 22:18:47 +000010069 copy_tv(&argvars[0], rettv);
10070 }
10071 }
10072 else
10073 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010074}
10075
10076/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010077 * "feedkeys()" function
10078 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010079 static void
10080f_feedkeys(argvars, rettv)
10081 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010082 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010083{
10084 int remap = TRUE;
10085 char_u *keys, *flags;
10086 char_u nbuf[NUMBUFLEN];
10087 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010088 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010089
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010090 /* This is not allowed in the sandbox. If the commands would still be
10091 * executed in the sandbox it would be OK, but it probably happens later,
10092 * when "sandbox" is no longer set. */
10093 if (check_secure())
10094 return;
10095
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010096 keys = get_tv_string(&argvars[0]);
10097 if (*keys != NUL)
10098 {
10099 if (argvars[1].v_type != VAR_UNKNOWN)
10100 {
10101 flags = get_tv_string_buf(&argvars[1], nbuf);
10102 for ( ; *flags != NUL; ++flags)
10103 {
10104 switch (*flags)
10105 {
10106 case 'n': remap = FALSE; break;
10107 case 'm': remap = TRUE; break;
10108 case 't': typed = TRUE; break;
10109 }
10110 }
10111 }
10112
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010113 /* Need to escape K_SPECIAL and CSI before putting the string in the
10114 * typeahead buffer. */
10115 keys_esc = vim_strsave_escape_csi(keys);
10116 if (keys_esc != NULL)
10117 {
10118 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010119 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010120 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010121 if (vgetc_busy)
10122 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010123 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010124 }
10125}
10126
10127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 * "filereadable()" function
10129 */
10130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010132 typval_T *argvars;
10133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010135 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 char_u *p;
10137 int n;
10138
Bram Moolenaarc236c162008-07-13 17:41:49 +000010139#ifndef O_NONBLOCK
10140# define O_NONBLOCK 0
10141#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010143 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10144 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 {
10146 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010147 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 }
10149 else
10150 n = FALSE;
10151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153}
10154
10155/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010156 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 * rights to write into.
10158 */
10159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010161 typval_T *argvars;
10162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010164 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165}
10166
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010167static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010168
10169 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010170findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010171 typval_T *argvars;
10172 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010173 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010174{
10175#ifdef FEAT_SEARCHPATH
10176 char_u *fname;
10177 char_u *fresult = NULL;
10178 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10179 char_u *p;
10180 char_u pathbuf[NUMBUFLEN];
10181 int count = 1;
10182 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010183 int error = FALSE;
10184#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010185
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010186 rettv->vval.v_string = NULL;
10187 rettv->v_type = VAR_STRING;
10188
10189#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010191
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010192 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10195 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010196 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 else
10198 {
10199 if (*p != NUL)
10200 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010203 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010204 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010205 }
10206
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010207 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10208 error = TRUE;
10209
10210 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 do
10213 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010214 if (rettv->v_type == VAR_STRING)
10215 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 fresult = find_file_in_path_option(first ? fname : NULL,
10217 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010218 0, first, path,
10219 find_what,
10220 curbuf->b_ffname,
10221 find_what == FINDFILE_DIR
10222 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010224
10225 if (fresult != NULL && rettv->v_type == VAR_LIST)
10226 list_append_string(rettv->vval.v_list, fresult, -1);
10227
10228 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010230
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010231 if (rettv->v_type == VAR_STRING)
10232 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010233#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010234}
10235
Bram Moolenaar33570922005-01-25 22:26:29 +000010236static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10237static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010238
10239/*
10240 * Implementation of map() and filter().
10241 */
10242 static void
10243filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010244 typval_T *argvars;
10245 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010246 int map;
10247{
10248 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010249 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010250 listitem_T *li, *nli;
10251 list_T *l = NULL;
10252 dictitem_T *di;
10253 hashtab_T *ht;
10254 hashitem_T *hi;
10255 dict_T *d = NULL;
10256 typval_T save_val;
10257 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010259 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010260 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10261 char *arg_errmsg = (map ? N_("map() argument")
10262 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010263 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010264 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010265
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010267 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010268 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010269 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270 return;
10271 }
10272 else if (argvars[0].v_type == VAR_DICT)
10273 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010274 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010275 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276 return;
10277 }
10278 else
10279 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010280 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 return;
10282 }
10283
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010284 expr = get_tv_string_buf_chk(&argvars[1], buf);
10285 /* On type errors, the preceding call has already displayed an error
10286 * message. Avoid a misleading error message for an empty string that
10287 * was not passed as argument. */
10288 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 prepare_vimvar(VV_VAL, &save_val);
10291 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010292
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010293 /* We reset "did_emsg" to be able to detect whether an error
10294 * occurred during evaluation of the expression. */
10295 save_did_emsg = did_emsg;
10296 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010297
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010298 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010299 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 vimvars[VV_KEY].vv_type = VAR_STRING;
10302
10303 ht = &d->dv_hashtab;
10304 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010305 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 if (!HASHITEM_EMPTY(hi))
10309 {
10310 --todo;
10311 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010312 if (tv_check_lock(di->di_tv.v_lock,
10313 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 break;
10315 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010316 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010317 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 break;
10319 if (!map && rem)
10320 dictitem_remove(d, di);
10321 clear_tv(&vimvars[VV_KEY].vv_tv);
10322 }
10323 }
10324 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 }
10326 else
10327 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010328 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010330 for (li = l->lv_first; li != NULL; li = nli)
10331 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010332 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010333 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010334 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010335 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010336 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010337 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010338 break;
10339 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010340 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010341 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010342 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010343 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010344
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010345 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010346 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010347
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010348 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010349 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010350
10351 copy_tv(&argvars[0], rettv);
10352}
10353
10354 static int
10355filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010356 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010357 char_u *expr;
10358 int map;
10359 int *remp;
10360{
Bram Moolenaar33570922005-01-25 22:26:29 +000010361 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010362 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010363 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010364
Bram Moolenaar33570922005-01-25 22:26:29 +000010365 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010366 s = expr;
10367 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010368 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010369 if (*s != NUL) /* check for trailing chars after expr */
10370 {
10371 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010372 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010373 }
10374 if (map)
10375 {
10376 /* map(): replace the list item value */
10377 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010378 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379 *tv = rettv;
10380 }
10381 else
10382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010383 int error = FALSE;
10384
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010387 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010388 /* On type error, nothing has been removed; return FAIL to stop the
10389 * loop. The error message was given by get_tv_number_chk(). */
10390 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010391 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010392 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010393 retval = OK;
10394theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010395 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010396 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010397}
10398
10399/*
10400 * "filter()" function
10401 */
10402 static void
10403f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010404 typval_T *argvars;
10405 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010406{
10407 filter_map(argvars, rettv, FALSE);
10408}
10409
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010410/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010411 * "finddir({fname}[, {path}[, {count}]])" function
10412 */
10413 static void
10414f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010415 typval_T *argvars;
10416 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010417{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010418 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010419}
10420
10421/*
10422 * "findfile({fname}[, {path}[, {count}]])" function
10423 */
10424 static void
10425f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 typval_T *argvars;
10427 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010428{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010429 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010430}
10431
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010432#ifdef FEAT_FLOAT
10433/*
10434 * "float2nr({float})" function
10435 */
10436 static void
10437f_float2nr(argvars, rettv)
10438 typval_T *argvars;
10439 typval_T *rettv;
10440{
10441 float_T f;
10442
10443 if (get_float_arg(argvars, &f) == OK)
10444 {
10445 if (f < -0x7fffffff)
10446 rettv->vval.v_number = -0x7fffffff;
10447 else if (f > 0x7fffffff)
10448 rettv->vval.v_number = 0x7fffffff;
10449 else
10450 rettv->vval.v_number = (varnumber_T)f;
10451 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010452}
10453
10454/*
10455 * "floor({float})" function
10456 */
10457 static void
10458f_floor(argvars, rettv)
10459 typval_T *argvars;
10460 typval_T *rettv;
10461{
10462 float_T f;
10463
10464 rettv->v_type = VAR_FLOAT;
10465 if (get_float_arg(argvars, &f) == OK)
10466 rettv->vval.v_float = floor(f);
10467 else
10468 rettv->vval.v_float = 0.0;
10469}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010470
10471/*
10472 * "fmod()" function
10473 */
10474 static void
10475f_fmod(argvars, rettv)
10476 typval_T *argvars;
10477 typval_T *rettv;
10478{
10479 float_T fx, fy;
10480
10481 rettv->v_type = VAR_FLOAT;
10482 if (get_float_arg(argvars, &fx) == OK
10483 && get_float_arg(&argvars[1], &fy) == OK)
10484 rettv->vval.v_float = fmod(fx, fy);
10485 else
10486 rettv->vval.v_float = 0.0;
10487}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010488#endif
10489
Bram Moolenaar0d660222005-01-07 21:51:51 +000010490/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010491 * "fnameescape({string})" function
10492 */
10493 static void
10494f_fnameescape(argvars, rettv)
10495 typval_T *argvars;
10496 typval_T *rettv;
10497{
10498 rettv->vval.v_string = vim_strsave_fnameescape(
10499 get_tv_string(&argvars[0]), FALSE);
10500 rettv->v_type = VAR_STRING;
10501}
10502
10503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 * "fnamemodify({fname}, {mods})" function
10505 */
10506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010507f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010508 typval_T *argvars;
10509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510{
10511 char_u *fname;
10512 char_u *mods;
10513 int usedlen = 0;
10514 int len;
10515 char_u *fbuf = NULL;
10516 char_u buf[NUMBUFLEN];
10517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010518 fname = get_tv_string_chk(&argvars[0]);
10519 mods = get_tv_string_buf_chk(&argvars[1], buf);
10520 if (fname == NULL || mods == NULL)
10521 fname = NULL;
10522 else
10523 {
10524 len = (int)STRLEN(fname);
10525 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 vim_free(fbuf);
10534}
10535
Bram Moolenaar33570922005-01-25 22:26:29 +000010536static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537
10538/*
10539 * "foldclosed()" function
10540 */
10541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010543 typval_T *argvars;
10544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 int end;
10546{
10547#ifdef FEAT_FOLDING
10548 linenr_T lnum;
10549 linenr_T first, last;
10550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10553 {
10554 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10555 {
10556 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 return;
10561 }
10562 }
10563#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565}
10566
10567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010568 * "foldclosed()" function
10569 */
10570 static void
10571f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 typval_T *argvars;
10573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010574{
10575 foldclosed_both(argvars, rettv, FALSE);
10576}
10577
10578/*
10579 * "foldclosedend()" function
10580 */
10581 static void
10582f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 typval_T *argvars;
10584 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010585{
10586 foldclosed_both(argvars, rettv, TRUE);
10587}
10588
10589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 * "foldlevel()" function
10591 */
10592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010594 typval_T *argvars;
10595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596{
10597#ifdef FEAT_FOLDING
10598 linenr_T lnum;
10599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604}
10605
10606/*
10607 * "foldtext()" function
10608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010611 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
10614#ifdef FEAT_FOLDING
10615 linenr_T lnum;
10616 char_u *s;
10617 char_u *r;
10618 int len;
10619 char *txt;
10620#endif
10621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 rettv->v_type = VAR_STRING;
10623 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010625 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10626 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10627 <= curbuf->b_ml.ml_line_count
10628 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 {
10630 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10632 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 {
10634 if (!linewhite(lnum))
10635 break;
10636 ++lnum;
10637 }
10638
10639 /* Find interesting text in this line. */
10640 s = skipwhite(ml_get(lnum));
10641 /* skip C comment-start */
10642 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010645 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010646 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010647 {
10648 s = skipwhite(ml_get(lnum + 1));
10649 if (*s == '*')
10650 s = skipwhite(s + 1);
10651 }
10652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 txt = _("+-%s%3ld lines: ");
10654 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010655 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 + 20 /* for %3ld */
10657 + STRLEN(s))); /* concatenated */
10658 if (r != NULL)
10659 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010660 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10661 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10662 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 len = (int)STRLEN(r);
10664 STRCAT(r, s);
10665 /* remove 'foldmarker' and 'commentstring' */
10666 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 }
10669 }
10670#endif
10671}
10672
10673/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010674 * "foldtextresult(lnum)" function
10675 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010677f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010678 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010679 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010680{
10681#ifdef FEAT_FOLDING
10682 linenr_T lnum;
10683 char_u *text;
10684 char_u buf[51];
10685 foldinfo_T foldinfo;
10686 int fold_count;
10687#endif
10688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->v_type = VAR_STRING;
10690 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010691#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 /* treat illegal types and illegal string values for {lnum} the same */
10694 if (lnum < 0)
10695 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010696 fold_count = foldedCount(curwin, lnum, &foldinfo);
10697 if (fold_count > 0)
10698 {
10699 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10700 &foldinfo, buf);
10701 if (text == buf)
10702 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010704 }
10705#endif
10706}
10707
10708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 * "foreground()" function
10710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010713 typval_T *argvars UNUSED;
10714 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716#ifdef FEAT_GUI
10717 if (gui.in_use)
10718 gui_mch_set_foreground();
10719#else
10720# ifdef WIN32
10721 win32_set_foreground();
10722# endif
10723#endif
10724}
10725
10726/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010727 * "function()" function
10728 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010730f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010731 typval_T *argvars;
10732 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010733{
10734 char_u *s;
10735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010737 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010738 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010739 /* Don't check an autoload name for existence here. */
10740 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010741 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010742 else
10743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744 rettv->vval.v_string = vim_strsave(s);
10745 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010746 }
10747}
10748
10749/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010750 * "garbagecollect()" function
10751 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010752 static void
10753f_garbagecollect(argvars, rettv)
10754 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010755 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010756{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010757 /* This is postponed until we are back at the toplevel, because we may be
10758 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10759 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010760
10761 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10762 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010763}
10764
10765/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010766 * "get()" function
10767 */
10768 static void
10769f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010770 typval_T *argvars;
10771 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010772{
Bram Moolenaar33570922005-01-25 22:26:29 +000010773 listitem_T *li;
10774 list_T *l;
10775 dictitem_T *di;
10776 dict_T *d;
10777 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010778
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010780 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010781 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 int error = FALSE;
10784
10785 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10786 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010788 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010789 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010790 else if (argvars[0].v_type == VAR_DICT)
10791 {
10792 if ((d = argvars[0].vval.v_dict) != NULL)
10793 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010794 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010795 if (di != NULL)
10796 tv = &di->di_tv;
10797 }
10798 }
10799 else
10800 EMSG2(_(e_listdictarg), "get()");
10801
10802 if (tv == NULL)
10803 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 copy_tv(&argvars[2], rettv);
10806 }
10807 else
10808 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010809}
10810
Bram Moolenaar342337a2005-07-21 21:11:17 +000010811static 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 +000010812
10813/*
10814 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010815 * Return a range (from start to end) of lines in rettv from the specified
10816 * buffer.
10817 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010818 */
10819 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010820get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010821 buf_T *buf;
10822 linenr_T start;
10823 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010824 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010825 typval_T *rettv;
10826{
10827 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010828
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010829 if (retlist && rettv_list_alloc(rettv) == FAIL)
10830 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010831
10832 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10833 return;
10834
10835 if (!retlist)
10836 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010837 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10838 p = ml_get_buf(buf, start, FALSE);
10839 else
10840 p = (char_u *)"";
10841
10842 rettv->v_type = VAR_STRING;
10843 rettv->vval.v_string = vim_strsave(p);
10844 }
10845 else
10846 {
10847 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010848 return;
10849
10850 if (start < 1)
10851 start = 1;
10852 if (end > buf->b_ml.ml_line_count)
10853 end = buf->b_ml.ml_line_count;
10854 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010855 if (list_append_string(rettv->vval.v_list,
10856 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010857 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010858 }
10859}
10860
10861/*
10862 * "getbufline()" function
10863 */
10864 static void
10865f_getbufline(argvars, rettv)
10866 typval_T *argvars;
10867 typval_T *rettv;
10868{
10869 linenr_T lnum;
10870 linenr_T end;
10871 buf_T *buf;
10872
10873 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10874 ++emsg_off;
10875 buf = get_buf_tv(&argvars[0]);
10876 --emsg_off;
10877
Bram Moolenaar661b1822005-07-28 22:36:45 +000010878 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010879 if (argvars[2].v_type == VAR_UNKNOWN)
10880 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010881 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010882 end = get_tv_lnum_buf(&argvars[2], buf);
10883
Bram Moolenaar342337a2005-07-21 21:11:17 +000010884 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010885}
10886
Bram Moolenaar0d660222005-01-07 21:51:51 +000010887/*
10888 * "getbufvar()" function
10889 */
10890 static void
10891f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 typval_T *argvars;
10893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010894{
10895 buf_T *buf;
10896 buf_T *save_curbuf;
10897 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10901 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010902 ++emsg_off;
10903 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010904
10905 rettv->v_type = VAR_STRING;
10906 rettv->vval.v_string = NULL;
10907
10908 if (buf != NULL && varname != NULL)
10909 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010910 /* set curbuf to be our buf, temporarily */
10911 save_curbuf = curbuf;
10912 curbuf = buf;
10913
Bram Moolenaar0d660222005-01-07 21:51:51 +000010914 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010915 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010916 else if (STRCMP(varname, "changedtick") == 0)
10917 {
10918 rettv->v_type = VAR_NUMBER;
10919 rettv->vval.v_number = curbuf->b_changedtick;
10920 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010921 else
10922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 if (*varname == NUL)
10924 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10925 * scope prefix before the NUL byte is required by
10926 * find_var_in_ht(). */
10927 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010928 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010929 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010930 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010931 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010932 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010933
10934 /* restore previous notion of curbuf */
10935 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010936 }
10937
10938 --emsg_off;
10939}
10940
10941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 * "getchar()" function
10943 */
10944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010945f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010946 typval_T *argvars;
10947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948{
10949 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010952 /* Position the cursor. Needed after a message that ends in a space. */
10953 windgoto(msg_row, msg_col);
10954
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 ++no_mapping;
10956 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010957 for (;;)
10958 {
10959 if (argvars[0].v_type == VAR_UNKNOWN)
10960 /* getchar(): blocking wait. */
10961 n = safe_vgetc();
10962 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10963 /* getchar(1): only check if char avail */
10964 n = vpeekc();
10965 else if (error || vpeekc() == NUL)
10966 /* illegal argument or getchar(0) and no char avail: return zero */
10967 n = 0;
10968 else
10969 /* getchar(0) and char avail: return char */
10970 n = safe_vgetc();
10971 if (n == K_IGNORE)
10972 continue;
10973 break;
10974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 --no_mapping;
10976 --allow_keys;
10977
Bram Moolenaar219b8702006-11-01 14:32:36 +000010978 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10979 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10980 vimvars[VV_MOUSE_COL].vv_nr = 0;
10981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 if (IS_SPECIAL(n) || mod_mask != 0)
10984 {
10985 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10986 int i = 0;
10987
10988 /* Turn a special key into three bytes, plus modifier. */
10989 if (mod_mask != 0)
10990 {
10991 temp[i++] = K_SPECIAL;
10992 temp[i++] = KS_MODIFIER;
10993 temp[i++] = mod_mask;
10994 }
10995 if (IS_SPECIAL(n))
10996 {
10997 temp[i++] = K_SPECIAL;
10998 temp[i++] = K_SECOND(n);
10999 temp[i++] = K_THIRD(n);
11000 }
11001#ifdef FEAT_MBYTE
11002 else if (has_mbyte)
11003 i += (*mb_char2bytes)(n, temp + i);
11004#endif
11005 else
11006 temp[i++] = n;
11007 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 rettv->v_type = VAR_STRING;
11009 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011010
11011#ifdef FEAT_MOUSE
11012 if (n == K_LEFTMOUSE
11013 || n == K_LEFTMOUSE_NM
11014 || n == K_LEFTDRAG
11015 || n == K_LEFTRELEASE
11016 || n == K_LEFTRELEASE_NM
11017 || n == K_MIDDLEMOUSE
11018 || n == K_MIDDLEDRAG
11019 || n == K_MIDDLERELEASE
11020 || n == K_RIGHTMOUSE
11021 || n == K_RIGHTDRAG
11022 || n == K_RIGHTRELEASE
11023 || n == K_X1MOUSE
11024 || n == K_X1DRAG
11025 || n == K_X1RELEASE
11026 || n == K_X2MOUSE
11027 || n == K_X2DRAG
11028 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011029 || n == K_MOUSELEFT
11030 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011031 || n == K_MOUSEDOWN
11032 || n == K_MOUSEUP)
11033 {
11034 int row = mouse_row;
11035 int col = mouse_col;
11036 win_T *win;
11037 linenr_T lnum;
11038# ifdef FEAT_WINDOWS
11039 win_T *wp;
11040# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011041 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011042
11043 if (row >= 0 && col >= 0)
11044 {
11045 /* Find the window at the mouse coordinates and compute the
11046 * text position. */
11047 win = mouse_find_win(&row, &col);
11048 (void)mouse_comp_pos(win, &row, &col, &lnum);
11049# ifdef FEAT_WINDOWS
11050 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011051 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011052# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011053 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011054 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11055 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11056 }
11057 }
11058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 }
11060}
11061
11062/*
11063 * "getcharmod()" function
11064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011067 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071}
11072
11073/*
11074 * "getcmdline()" function
11075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011078 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 rettv->v_type = VAR_STRING;
11082 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083}
11084
11085/*
11086 * "getcmdpos()" function
11087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011090 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094}
11095
11096/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011097 * "getcmdtype()" function
11098 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011099 static void
11100f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011101 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011102 typval_T *rettv;
11103{
11104 rettv->v_type = VAR_STRING;
11105 rettv->vval.v_string = alloc(2);
11106 if (rettv->vval.v_string != NULL)
11107 {
11108 rettv->vval.v_string[0] = get_cmdline_type();
11109 rettv->vval.v_string[1] = NUL;
11110 }
11111}
11112
11113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 * "getcwd()" function
11115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011118 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011121 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011124 rettv->vval.v_string = NULL;
11125 cwd = alloc(MAXPATHL);
11126 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011128 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11129 {
11130 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011132 if (rettv->vval.v_string != NULL)
11133 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011135 }
11136 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 }
11138}
11139
11140/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011141 * "getfontname()" function
11142 */
11143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011145 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011146 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->v_type = VAR_STRING;
11149 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011150#ifdef FEAT_GUI
11151 if (gui.in_use)
11152 {
11153 GuiFont font;
11154 char_u *name = NULL;
11155
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011156 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011157 {
11158 /* Get the "Normal" font. Either the name saved by
11159 * hl_set_font_name() or from the font ID. */
11160 font = gui.norm_font;
11161 name = hl_get_font_name();
11162 }
11163 else
11164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011166 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11167 return;
11168 font = gui_mch_get_font(name, FALSE);
11169 if (font == NOFONT)
11170 return; /* Invalid font name, return empty string. */
11171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011173 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011174 gui_mch_free_font(font);
11175 }
11176#endif
11177}
11178
11179/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011180 * "getfperm({fname})" function
11181 */
11182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011184 typval_T *argvars;
11185 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011186{
11187 char_u *fname;
11188 struct stat st;
11189 char_u *perm = NULL;
11190 char_u flags[] = "rwx";
11191 int i;
11192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011196 if (mch_stat((char *)fname, &st) >= 0)
11197 {
11198 perm = vim_strsave((char_u *)"---------");
11199 if (perm != NULL)
11200 {
11201 for (i = 0; i < 9; i++)
11202 {
11203 if (st.st_mode & (1 << (8 - i)))
11204 perm[i] = flags[i % 3];
11205 }
11206 }
11207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011209}
11210
11211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 * "getfsize({fname})" function
11213 */
11214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011216 typval_T *argvars;
11217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218{
11219 char_u *fname;
11220 struct stat st;
11221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225
11226 if (mch_stat((char *)fname, &st) >= 0)
11227 {
11228 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011233
11234 /* non-perfect check for overflow */
11235 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11236 rettv->vval.v_number = -2;
11237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 }
11239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241}
11242
11243/*
11244 * "getftime({fname})" function
11245 */
11246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011248 typval_T *argvars;
11249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250{
11251 char_u *fname;
11252 struct stat st;
11253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255
11256 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260}
11261
11262/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011263 * "getftype({fname})" function
11264 */
11265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *argvars;
11268 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011269{
11270 char_u *fname;
11271 struct stat st;
11272 char_u *type = NULL;
11273 char *t;
11274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011278 if (mch_lstat((char *)fname, &st) >= 0)
11279 {
11280#ifdef S_ISREG
11281 if (S_ISREG(st.st_mode))
11282 t = "file";
11283 else if (S_ISDIR(st.st_mode))
11284 t = "dir";
11285# ifdef S_ISLNK
11286 else if (S_ISLNK(st.st_mode))
11287 t = "link";
11288# endif
11289# ifdef S_ISBLK
11290 else if (S_ISBLK(st.st_mode))
11291 t = "bdev";
11292# endif
11293# ifdef S_ISCHR
11294 else if (S_ISCHR(st.st_mode))
11295 t = "cdev";
11296# endif
11297# ifdef S_ISFIFO
11298 else if (S_ISFIFO(st.st_mode))
11299 t = "fifo";
11300# endif
11301# ifdef S_ISSOCK
11302 else if (S_ISSOCK(st.st_mode))
11303 t = "fifo";
11304# endif
11305 else
11306 t = "other";
11307#else
11308# ifdef S_IFMT
11309 switch (st.st_mode & S_IFMT)
11310 {
11311 case S_IFREG: t = "file"; break;
11312 case S_IFDIR: t = "dir"; break;
11313# ifdef S_IFLNK
11314 case S_IFLNK: t = "link"; break;
11315# endif
11316# ifdef S_IFBLK
11317 case S_IFBLK: t = "bdev"; break;
11318# endif
11319# ifdef S_IFCHR
11320 case S_IFCHR: t = "cdev"; break;
11321# endif
11322# ifdef S_IFIFO
11323 case S_IFIFO: t = "fifo"; break;
11324# endif
11325# ifdef S_IFSOCK
11326 case S_IFSOCK: t = "socket"; break;
11327# endif
11328 default: t = "other";
11329 }
11330# else
11331 if (mch_isdir(fname))
11332 t = "dir";
11333 else
11334 t = "file";
11335# endif
11336#endif
11337 type = vim_strsave((char_u *)t);
11338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011340}
11341
11342/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011343 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011344 */
11345 static void
11346f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *argvars;
11348 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349{
11350 linenr_T lnum;
11351 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011352 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011353
11354 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011355 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011356 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011357 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011358 retlist = FALSE;
11359 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011360 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011361 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011362 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011363 retlist = TRUE;
11364 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011365
Bram Moolenaar342337a2005-07-21 21:11:17 +000011366 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011367}
11368
11369/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011370 * "getmatches()" function
11371 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011372 static void
11373f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011374 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011375 typval_T *rettv;
11376{
11377#ifdef FEAT_SEARCH_EXTRA
11378 dict_T *dict;
11379 matchitem_T *cur = curwin->w_match_head;
11380
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011381 if (rettv_list_alloc(rettv) == OK)
11382 {
11383 while (cur != NULL)
11384 {
11385 dict = dict_alloc();
11386 if (dict == NULL)
11387 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011388 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11389 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11390 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11391 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11392 list_append_dict(rettv->vval.v_list, dict);
11393 cur = cur->next;
11394 }
11395 }
11396#endif
11397}
11398
11399/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011400 * "getpid()" function
11401 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011402 static void
11403f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011404 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011405 typval_T *rettv;
11406{
11407 rettv->vval.v_number = mch_get_pid();
11408}
11409
11410/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011411 * "getpos(string)" function
11412 */
11413 static void
11414f_getpos(argvars, rettv)
11415 typval_T *argvars;
11416 typval_T *rettv;
11417{
11418 pos_T *fp;
11419 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011420 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011421
11422 if (rettv_list_alloc(rettv) == OK)
11423 {
11424 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011425 fp = var2fpos(&argvars[0], TRUE, &fnum);
11426 if (fnum != -1)
11427 list_append_number(l, (varnumber_T)fnum);
11428 else
11429 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011430 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11431 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011432 list_append_number(l, (fp != NULL)
11433 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011434 : (varnumber_T)0);
11435 list_append_number(l,
11436#ifdef FEAT_VIRTUALEDIT
11437 (fp != NULL) ? (varnumber_T)fp->coladd :
11438#endif
11439 (varnumber_T)0);
11440 }
11441 else
11442 rettv->vval.v_number = FALSE;
11443}
11444
11445/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011446 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011447 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011448 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011449f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011450 typval_T *argvars UNUSED;
11451 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011452{
11453#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011454 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011455#endif
11456
Bram Moolenaar2641f772005-03-25 21:58:17 +000011457#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011458 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011459 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011460 wp = NULL;
11461 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11462 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011463 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011464 if (wp == NULL)
11465 return;
11466 }
11467
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011468 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011469 }
11470#endif
11471}
11472
11473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 * "getreg()" function
11475 */
11476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T *argvars;
11479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480{
11481 char_u *strregname;
11482 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011483 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011486 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011488 strregname = get_tv_string_chk(&argvars[0]);
11489 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011490 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011491 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 regname = (strregname == NULL ? '"' : *strregname);
11496 if (regname == 0)
11497 regname = '"';
11498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 rettv->vval.v_string = error ? NULL :
11501 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502}
11503
11504/*
11505 * "getregtype()" function
11506 */
11507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011509 typval_T *argvars;
11510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511{
11512 char_u *strregname;
11513 int regname;
11514 char_u buf[NUMBUFLEN + 2];
11515 long reglen = 0;
11516
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011517 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011518 {
11519 strregname = get_tv_string_chk(&argvars[0]);
11520 if (strregname == NULL) /* type error; errmsg already given */
11521 {
11522 rettv->v_type = VAR_STRING;
11523 rettv->vval.v_string = NULL;
11524 return;
11525 }
11526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527 else
11528 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011529 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530
11531 regname = (strregname == NULL ? '"' : *strregname);
11532 if (regname == 0)
11533 regname = '"';
11534
11535 buf[0] = NUL;
11536 buf[1] = NUL;
11537 switch (get_reg_type(regname, &reglen))
11538 {
11539 case MLINE: buf[0] = 'V'; break;
11540 case MCHAR: buf[0] = 'v'; break;
11541#ifdef FEAT_VISUAL
11542 case MBLOCK:
11543 buf[0] = Ctrl_V;
11544 sprintf((char *)buf + 1, "%ld", reglen + 1);
11545 break;
11546#endif
11547 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->v_type = VAR_STRING;
11549 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550}
11551
11552/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011553 * "gettabvar()" function
11554 */
11555 static void
11556f_gettabvar(argvars, rettv)
11557 typval_T *argvars;
11558 typval_T *rettv;
11559{
11560 tabpage_T *tp;
11561 dictitem_T *v;
11562 char_u *varname;
11563
11564 rettv->v_type = VAR_STRING;
11565 rettv->vval.v_string = NULL;
11566
11567 varname = get_tv_string_chk(&argvars[1]);
11568 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11569 if (tp != NULL && varname != NULL)
11570 {
11571 /* look up the variable */
11572 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11573 if (v != NULL)
11574 copy_tv(&v->di_tv, rettv);
11575 }
11576}
11577
11578/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011579 * "gettabwinvar()" function
11580 */
11581 static void
11582f_gettabwinvar(argvars, rettv)
11583 typval_T *argvars;
11584 typval_T *rettv;
11585{
11586 getwinvar(argvars, rettv, 1);
11587}
11588
11589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 * "getwinposx()" function
11591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011594 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598#ifdef FEAT_GUI
11599 if (gui.in_use)
11600 {
11601 int x, y;
11602
11603 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 }
11606#endif
11607}
11608
11609/*
11610 * "getwinposy()" function
11611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011613f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011614 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618#ifdef FEAT_GUI
11619 if (gui.in_use)
11620 {
11621 int x, y;
11622
11623 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 }
11626#endif
11627}
11628
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011629/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011630 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011631 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011632 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011633find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011634 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011635 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011636{
11637#ifdef FEAT_WINDOWS
11638 win_T *wp;
11639#endif
11640 int nr;
11641
11642 nr = get_tv_number_chk(vp, NULL);
11643
11644#ifdef FEAT_WINDOWS
11645 if (nr < 0)
11646 return NULL;
11647 if (nr == 0)
11648 return curwin;
11649
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011650 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11651 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011652 if (--nr <= 0)
11653 break;
11654 return wp;
11655#else
11656 if (nr == 0 || nr == 1)
11657 return curwin;
11658 return NULL;
11659#endif
11660}
11661
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662/*
11663 * "getwinvar()" function
11664 */
11665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011667 typval_T *argvars;
11668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011670 getwinvar(argvars, rettv, 0);
11671}
11672
11673/*
11674 * getwinvar() and gettabwinvar()
11675 */
11676 static void
11677getwinvar(argvars, rettv, off)
11678 typval_T *argvars;
11679 typval_T *rettv;
11680 int off; /* 1 for gettabwinvar() */
11681{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 win_T *win, *oldcurwin;
11683 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011685 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011687#ifdef FEAT_WINDOWS
11688 if (off == 1)
11689 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11690 else
11691 tp = curtab;
11692#endif
11693 win = find_win_by_nr(&argvars[off], tp);
11694 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011697 rettv->v_type = VAR_STRING;
11698 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699
11700 if (win != NULL && varname != NULL)
11701 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011702 /* Set curwin to be our win, temporarily. Also set curbuf, so
11703 * that we can get buffer-local options. */
11704 oldcurwin = curwin;
11705 curwin = win;
11706 curbuf = win->w_buffer;
11707
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 else
11711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 if (*varname == NUL)
11713 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11714 * scope prefix before the NUL byte is required by
11715 * find_var_in_ht(). */
11716 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011718 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011720 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011722
11723 /* restore previous notion of curwin */
11724 curwin = oldcurwin;
11725 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 }
11727
11728 --emsg_off;
11729}
11730
11731/*
11732 * "glob()" function
11733 */
11734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011736 typval_T *argvars;
11737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011739 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011741 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011743 /* When the optional second argument is non-zero, don't remove matches
11744 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11745 if (argvars[1].v_type != VAR_UNKNOWN
11746 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011747 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011749 if (!error)
11750 {
11751 ExpandInit(&xpc);
11752 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011753 if (p_wic)
11754 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011755 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011756 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011757 }
11758 else
11759 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760}
11761
11762/*
11763 * "globpath()" function
11764 */
11765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011767 typval_T *argvars;
11768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011770 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011772 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011773 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011775 /* When the optional second argument is non-zero, don't remove matches
11776 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11777 if (argvars[2].v_type != VAR_UNKNOWN
11778 && get_tv_number_chk(&argvars[2], &error))
11779 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011780 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011781 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 rettv->vval.v_string = NULL;
11783 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011784 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11785 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786}
11787
11788/*
11789 * "has()" function
11790 */
11791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 typval_T *argvars;
11794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795{
11796 int i;
11797 char_u *name;
11798 int n = FALSE;
11799 static char *(has_list[]) =
11800 {
11801#ifdef AMIGA
11802 "amiga",
11803# ifdef FEAT_ARP
11804 "arp",
11805# endif
11806#endif
11807#ifdef __BEOS__
11808 "beos",
11809#endif
11810#ifdef MSDOS
11811# ifdef DJGPP
11812 "dos32",
11813# else
11814 "dos16",
11815# endif
11816#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011817#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 "mac",
11819#endif
11820#if defined(MACOS_X_UNIX)
11821 "macunix",
11822#endif
11823#ifdef OS2
11824 "os2",
11825#endif
11826#ifdef __QNX__
11827 "qnx",
11828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829#ifdef UNIX
11830 "unix",
11831#endif
11832#ifdef VMS
11833 "vms",
11834#endif
11835#ifdef WIN16
11836 "win16",
11837#endif
11838#ifdef WIN32
11839 "win32",
11840#endif
11841#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11842 "win32unix",
11843#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011844#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 "win64",
11846#endif
11847#ifdef EBCDIC
11848 "ebcdic",
11849#endif
11850#ifndef CASE_INSENSITIVE_FILENAME
11851 "fname_case",
11852#endif
11853#ifdef FEAT_ARABIC
11854 "arabic",
11855#endif
11856#ifdef FEAT_AUTOCMD
11857 "autocmd",
11858#endif
11859#ifdef FEAT_BEVAL
11860 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011861# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11862 "balloon_multiline",
11863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864#endif
11865#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11866 "builtin_terms",
11867# ifdef ALL_BUILTIN_TCAPS
11868 "all_builtin_terms",
11869# endif
11870#endif
11871#ifdef FEAT_BYTEOFF
11872 "byte_offset",
11873#endif
11874#ifdef FEAT_CINDENT
11875 "cindent",
11876#endif
11877#ifdef FEAT_CLIENTSERVER
11878 "clientserver",
11879#endif
11880#ifdef FEAT_CLIPBOARD
11881 "clipboard",
11882#endif
11883#ifdef FEAT_CMDL_COMPL
11884 "cmdline_compl",
11885#endif
11886#ifdef FEAT_CMDHIST
11887 "cmdline_hist",
11888#endif
11889#ifdef FEAT_COMMENTS
11890 "comments",
11891#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011892#ifdef FEAT_CONCEAL
11893 "conceal",
11894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895#ifdef FEAT_CRYPT
11896 "cryptv",
11897#endif
11898#ifdef FEAT_CSCOPE
11899 "cscope",
11900#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011901#ifdef FEAT_CURSORBIND
11902 "cursorbind",
11903#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011904#ifdef CURSOR_SHAPE
11905 "cursorshape",
11906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907#ifdef DEBUG
11908 "debug",
11909#endif
11910#ifdef FEAT_CON_DIALOG
11911 "dialog_con",
11912#endif
11913#ifdef FEAT_GUI_DIALOG
11914 "dialog_gui",
11915#endif
11916#ifdef FEAT_DIFF
11917 "diff",
11918#endif
11919#ifdef FEAT_DIGRAPHS
11920 "digraphs",
11921#endif
11922#ifdef FEAT_DND
11923 "dnd",
11924#endif
11925#ifdef FEAT_EMACS_TAGS
11926 "emacs_tags",
11927#endif
11928 "eval", /* always present, of course! */
11929#ifdef FEAT_EX_EXTRA
11930 "ex_extra",
11931#endif
11932#ifdef FEAT_SEARCH_EXTRA
11933 "extra_search",
11934#endif
11935#ifdef FEAT_FKMAP
11936 "farsi",
11937#endif
11938#ifdef FEAT_SEARCHPATH
11939 "file_in_path",
11940#endif
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020011941#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011942 "filterpipe",
11943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944#ifdef FEAT_FIND_ID
11945 "find_in_path",
11946#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011947#ifdef FEAT_FLOAT
11948 "float",
11949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950#ifdef FEAT_FOLDING
11951 "folding",
11952#endif
11953#ifdef FEAT_FOOTER
11954 "footer",
11955#endif
11956#if !defined(USE_SYSTEM) && defined(UNIX)
11957 "fork",
11958#endif
11959#ifdef FEAT_GETTEXT
11960 "gettext",
11961#endif
11962#ifdef FEAT_GUI
11963 "gui",
11964#endif
11965#ifdef FEAT_GUI_ATHENA
11966# ifdef FEAT_GUI_NEXTAW
11967 "gui_neXtaw",
11968# else
11969 "gui_athena",
11970# endif
11971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972#ifdef FEAT_GUI_GTK
11973 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011976#ifdef FEAT_GUI_GNOME
11977 "gui_gnome",
11978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979#ifdef FEAT_GUI_MAC
11980 "gui_mac",
11981#endif
11982#ifdef FEAT_GUI_MOTIF
11983 "gui_motif",
11984#endif
11985#ifdef FEAT_GUI_PHOTON
11986 "gui_photon",
11987#endif
11988#ifdef FEAT_GUI_W16
11989 "gui_win16",
11990#endif
11991#ifdef FEAT_GUI_W32
11992 "gui_win32",
11993#endif
11994#ifdef FEAT_HANGULIN
11995 "hangul_input",
11996#endif
11997#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11998 "iconv",
11999#endif
12000#ifdef FEAT_INS_EXPAND
12001 "insert_expand",
12002#endif
12003#ifdef FEAT_JUMPLIST
12004 "jumplist",
12005#endif
12006#ifdef FEAT_KEYMAP
12007 "keymap",
12008#endif
12009#ifdef FEAT_LANGMAP
12010 "langmap",
12011#endif
12012#ifdef FEAT_LIBCALL
12013 "libcall",
12014#endif
12015#ifdef FEAT_LINEBREAK
12016 "linebreak",
12017#endif
12018#ifdef FEAT_LISP
12019 "lispindent",
12020#endif
12021#ifdef FEAT_LISTCMDS
12022 "listcmds",
12023#endif
12024#ifdef FEAT_LOCALMAP
12025 "localmap",
12026#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012027#ifdef FEAT_LUA
12028# ifndef DYNAMIC_LUA
12029 "lua",
12030# endif
12031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032#ifdef FEAT_MENU
12033 "menu",
12034#endif
12035#ifdef FEAT_SESSION
12036 "mksession",
12037#endif
12038#ifdef FEAT_MODIFY_FNAME
12039 "modify_fname",
12040#endif
12041#ifdef FEAT_MOUSE
12042 "mouse",
12043#endif
12044#ifdef FEAT_MOUSESHAPE
12045 "mouseshape",
12046#endif
12047#if defined(UNIX) || defined(VMS)
12048# ifdef FEAT_MOUSE_DEC
12049 "mouse_dec",
12050# endif
12051# ifdef FEAT_MOUSE_GPM
12052 "mouse_gpm",
12053# endif
12054# ifdef FEAT_MOUSE_JSB
12055 "mouse_jsbterm",
12056# endif
12057# ifdef FEAT_MOUSE_NET
12058 "mouse_netterm",
12059# endif
12060# ifdef FEAT_MOUSE_PTERM
12061 "mouse_pterm",
12062# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012063# ifdef FEAT_SYSMOUSE
12064 "mouse_sysmouse",
12065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066# ifdef FEAT_MOUSE_XTERM
12067 "mouse_xterm",
12068# endif
12069#endif
12070#ifdef FEAT_MBYTE
12071 "multi_byte",
12072#endif
12073#ifdef FEAT_MBYTE_IME
12074 "multi_byte_ime",
12075#endif
12076#ifdef FEAT_MULTI_LANG
12077 "multi_lang",
12078#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012079#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012080#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012081 "mzscheme",
12082#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084#ifdef FEAT_OLE
12085 "ole",
12086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087#ifdef FEAT_PATH_EXTRA
12088 "path_extra",
12089#endif
12090#ifdef FEAT_PERL
12091#ifndef DYNAMIC_PERL
12092 "perl",
12093#endif
12094#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012095#ifdef FEAT_PERSISTENT_UNDO
12096 "persistent_undo",
12097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098#ifdef FEAT_PYTHON
12099#ifndef DYNAMIC_PYTHON
12100 "python",
12101#endif
12102#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012103#ifdef FEAT_PYTHON3
12104#ifndef DYNAMIC_PYTHON3
12105 "python3",
12106#endif
12107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108#ifdef FEAT_POSTSCRIPT
12109 "postscript",
12110#endif
12111#ifdef FEAT_PRINTER
12112 "printer",
12113#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012114#ifdef FEAT_PROFILE
12115 "profile",
12116#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012117#ifdef FEAT_RELTIME
12118 "reltime",
12119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120#ifdef FEAT_QUICKFIX
12121 "quickfix",
12122#endif
12123#ifdef FEAT_RIGHTLEFT
12124 "rightleft",
12125#endif
12126#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12127 "ruby",
12128#endif
12129#ifdef FEAT_SCROLLBIND
12130 "scrollbind",
12131#endif
12132#ifdef FEAT_CMDL_INFO
12133 "showcmd",
12134 "cmdline_info",
12135#endif
12136#ifdef FEAT_SIGNS
12137 "signs",
12138#endif
12139#ifdef FEAT_SMARTINDENT
12140 "smartindent",
12141#endif
12142#ifdef FEAT_SNIFF
12143 "sniff",
12144#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012145#ifdef STARTUPTIME
12146 "startuptime",
12147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148#ifdef FEAT_STL_OPT
12149 "statusline",
12150#endif
12151#ifdef FEAT_SUN_WORKSHOP
12152 "sun_workshop",
12153#endif
12154#ifdef FEAT_NETBEANS_INTG
12155 "netbeans_intg",
12156#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012157#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012158 "spell",
12159#endif
12160#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 "syntax",
12162#endif
12163#if defined(USE_SYSTEM) || !defined(UNIX)
12164 "system",
12165#endif
12166#ifdef FEAT_TAG_BINS
12167 "tag_binary",
12168#endif
12169#ifdef FEAT_TAG_OLDSTATIC
12170 "tag_old_static",
12171#endif
12172#ifdef FEAT_TAG_ANYWHITE
12173 "tag_any_white",
12174#endif
12175#ifdef FEAT_TCL
12176# ifndef DYNAMIC_TCL
12177 "tcl",
12178# endif
12179#endif
12180#ifdef TERMINFO
12181 "terminfo",
12182#endif
12183#ifdef FEAT_TERMRESPONSE
12184 "termresponse",
12185#endif
12186#ifdef FEAT_TEXTOBJ
12187 "textobjects",
12188#endif
12189#ifdef HAVE_TGETENT
12190 "tgetent",
12191#endif
12192#ifdef FEAT_TITLE
12193 "title",
12194#endif
12195#ifdef FEAT_TOOLBAR
12196 "toolbar",
12197#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012198#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12199 "unnamedplus",
12200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201#ifdef FEAT_USR_CMDS
12202 "user-commands", /* was accidentally included in 5.4 */
12203 "user_commands",
12204#endif
12205#ifdef FEAT_VIMINFO
12206 "viminfo",
12207#endif
12208#ifdef FEAT_VERTSPLIT
12209 "vertsplit",
12210#endif
12211#ifdef FEAT_VIRTUALEDIT
12212 "virtualedit",
12213#endif
12214#ifdef FEAT_VISUAL
12215 "visual",
12216#endif
12217#ifdef FEAT_VISUALEXTRA
12218 "visualextra",
12219#endif
12220#ifdef FEAT_VREPLACE
12221 "vreplace",
12222#endif
12223#ifdef FEAT_WILDIGN
12224 "wildignore",
12225#endif
12226#ifdef FEAT_WILDMENU
12227 "wildmenu",
12228#endif
12229#ifdef FEAT_WINDOWS
12230 "windows",
12231#endif
12232#ifdef FEAT_WAK
12233 "winaltkeys",
12234#endif
12235#ifdef FEAT_WRITEBACKUP
12236 "writebackup",
12237#endif
12238#ifdef FEAT_XIM
12239 "xim",
12240#endif
12241#ifdef FEAT_XFONTSET
12242 "xfontset",
12243#endif
12244#ifdef USE_XSMP
12245 "xsmp",
12246#endif
12247#ifdef USE_XSMP_INTERACT
12248 "xsmp_interact",
12249#endif
12250#ifdef FEAT_XCLIPBOARD
12251 "xterm_clipboard",
12252#endif
12253#ifdef FEAT_XTERM_SAVE
12254 "xterm_save",
12255#endif
12256#if defined(UNIX) && defined(FEAT_X11)
12257 "X11",
12258#endif
12259 NULL
12260 };
12261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 for (i = 0; has_list[i] != NULL; ++i)
12264 if (STRICMP(name, has_list[i]) == 0)
12265 {
12266 n = TRUE;
12267 break;
12268 }
12269
12270 if (n == FALSE)
12271 {
12272 if (STRNICMP(name, "patch", 5) == 0)
12273 n = has_patch(atoi((char *)name + 5));
12274 else if (STRICMP(name, "vim_starting") == 0)
12275 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012276#ifdef FEAT_MBYTE
12277 else if (STRICMP(name, "multi_byte_encoding") == 0)
12278 n = has_mbyte;
12279#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012280#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12281 else if (STRICMP(name, "balloon_multiline") == 0)
12282 n = multiline_balloon_available();
12283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284#ifdef DYNAMIC_TCL
12285 else if (STRICMP(name, "tcl") == 0)
12286 n = tcl_enabled(FALSE);
12287#endif
12288#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12289 else if (STRICMP(name, "iconv") == 0)
12290 n = iconv_enabled(FALSE);
12291#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012292#ifdef DYNAMIC_LUA
12293 else if (STRICMP(name, "lua") == 0)
12294 n = lua_enabled(FALSE);
12295#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012296#ifdef DYNAMIC_MZSCHEME
12297 else if (STRICMP(name, "mzscheme") == 0)
12298 n = mzscheme_enabled(FALSE);
12299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300#ifdef DYNAMIC_RUBY
12301 else if (STRICMP(name, "ruby") == 0)
12302 n = ruby_enabled(FALSE);
12303#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012304#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305#ifdef DYNAMIC_PYTHON
12306 else if (STRICMP(name, "python") == 0)
12307 n = python_enabled(FALSE);
12308#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012309#endif
12310#ifdef FEAT_PYTHON3
12311#ifdef DYNAMIC_PYTHON3
12312 else if (STRICMP(name, "python3") == 0)
12313 n = python3_enabled(FALSE);
12314#endif
12315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316#ifdef DYNAMIC_PERL
12317 else if (STRICMP(name, "perl") == 0)
12318 n = perl_enabled(FALSE);
12319#endif
12320#ifdef FEAT_GUI
12321 else if (STRICMP(name, "gui_running") == 0)
12322 n = (gui.in_use || gui.starting);
12323# ifdef FEAT_GUI_W32
12324 else if (STRICMP(name, "gui_win32s") == 0)
12325 n = gui_is_win32s();
12326# endif
12327# ifdef FEAT_BROWSE
12328 else if (STRICMP(name, "browse") == 0)
12329 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12330# endif
12331#endif
12332#ifdef FEAT_SYN_HL
12333 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012334 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335#endif
12336#if defined(WIN3264)
12337 else if (STRICMP(name, "win95") == 0)
12338 n = mch_windows95();
12339#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012340#ifdef FEAT_NETBEANS_INTG
12341 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012342 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 }
12345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012346 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347}
12348
12349/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012350 * "has_key()" function
12351 */
12352 static void
12353f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012354 typval_T *argvars;
12355 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012356{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012357 if (argvars[0].v_type != VAR_DICT)
12358 {
12359 EMSG(_(e_dictreq));
12360 return;
12361 }
12362 if (argvars[0].vval.v_dict == NULL)
12363 return;
12364
12365 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012366 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012367}
12368
12369/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012370 * "haslocaldir()" function
12371 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012372 static void
12373f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012374 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012375 typval_T *rettv;
12376{
12377 rettv->vval.v_number = (curwin->w_localdir != NULL);
12378}
12379
12380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 * "hasmapto()" function
12382 */
12383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012385 typval_T *argvars;
12386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
12388 char_u *name;
12389 char_u *mode;
12390 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012391 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012394 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 mode = (char_u *)"nvo";
12396 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012399 if (argvars[2].v_type != VAR_UNKNOWN)
12400 abbr = get_tv_number(&argvars[2]);
12401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402
Bram Moolenaar2c932302006-03-18 21:42:09 +000012403 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012404 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407}
12408
12409/*
12410 * "histadd()" function
12411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012414 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416{
12417#ifdef FEAT_CMDHIST
12418 int histype;
12419 char_u *str;
12420 char_u buf[NUMBUFLEN];
12421#endif
12422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 if (check_restricted() || check_secure())
12425 return;
12426#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012427 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12428 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 if (histype >= 0)
12430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 if (*str != NUL)
12433 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012434 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012436 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 return;
12438 }
12439 }
12440#endif
12441}
12442
12443/*
12444 * "histdel()" function
12445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012448 typval_T *argvars UNUSED;
12449 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450{
12451#ifdef FEAT_CMDHIST
12452 int n;
12453 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012454 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012456 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12457 if (str == NULL)
12458 n = 0;
12459 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012461 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012462 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012464 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 else
12467 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469 get_tv_string_buf(&argvars[1], buf));
12470 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471#endif
12472}
12473
12474/*
12475 * "histget()" function
12476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012479 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481{
12482#ifdef FEAT_CMDHIST
12483 int type;
12484 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012485 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012487 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12488 if (str == NULL)
12489 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012491 {
12492 type = get_histtype(str);
12493 if (argvars[1].v_type == VAR_UNKNOWN)
12494 idx = get_history_idx(type);
12495 else
12496 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12497 /* -1 on type error */
12498 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012503 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504}
12505
12506/*
12507 * "histnr()" function
12508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012511 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513{
12514 int i;
12515
12516#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 char_u *history = get_tv_string_chk(&argvars[0]);
12518
12519 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520 if (i >= HIST_CMD && i < HIST_COUNT)
12521 i = get_history_idx(i);
12522 else
12523#endif
12524 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526}
12527
12528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 * "highlightID(name)" function
12530 */
12531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012533 typval_T *argvars;
12534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537}
12538
12539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 * "highlight_exists()" function
12541 */
12542 static void
12543f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012544 typval_T *argvars;
12545 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012546{
12547 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12548}
12549
12550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 * "hostname()" function
12552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012554f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012555 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557{
12558 char_u hostname[256];
12559
12560 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 rettv->v_type = VAR_STRING;
12562 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563}
12564
12565/*
12566 * iconv() function
12567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012569f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012570 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572{
12573#ifdef FEAT_MBYTE
12574 char_u buf1[NUMBUFLEN];
12575 char_u buf2[NUMBUFLEN];
12576 char_u *from, *to, *str;
12577 vimconv_T vimconv;
12578#endif
12579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580 rettv->v_type = VAR_STRING;
12581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582
12583#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584 str = get_tv_string(&argvars[0]);
12585 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12586 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587 vimconv.vc_type = CONV_NONE;
12588 convert_setup(&vimconv, from, to);
12589
12590 /* If the encodings are equal, no conversion needed. */
12591 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012594 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595
12596 convert_setup(&vimconv, NULL, NULL);
12597 vim_free(from);
12598 vim_free(to);
12599#endif
12600}
12601
12602/*
12603 * "indent()" function
12604 */
12605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012606f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012607 typval_T *argvars;
12608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609{
12610 linenr_T lnum;
12611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012612 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012614 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617}
12618
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012619/*
12620 * "index()" function
12621 */
12622 static void
12623f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012624 typval_T *argvars;
12625 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012626{
Bram Moolenaar33570922005-01-25 22:26:29 +000012627 list_T *l;
12628 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012629 long idx = 0;
12630 int ic = FALSE;
12631
12632 rettv->vval.v_number = -1;
12633 if (argvars[0].v_type != VAR_LIST)
12634 {
12635 EMSG(_(e_listreq));
12636 return;
12637 }
12638 l = argvars[0].vval.v_list;
12639 if (l != NULL)
12640 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012641 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012642 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012644 int error = FALSE;
12645
Bram Moolenaar758711c2005-02-02 23:11:38 +000012646 /* Start at specified item. Use the cached index that list_find()
12647 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012648 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012649 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012650 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012651 ic = get_tv_number_chk(&argvars[3], &error);
12652 if (error)
12653 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012654 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012655
Bram Moolenaar758711c2005-02-02 23:11:38 +000012656 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012657 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012658 {
12659 rettv->vval.v_number = idx;
12660 break;
12661 }
12662 }
12663}
12664
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665static int inputsecret_flag = 0;
12666
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012667static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12668
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012670 * This function is used by f_input() and f_inputdialog() functions. The third
12671 * argument to f_input() specifies the type of completion to use at the
12672 * prompt. The third argument to f_inputdialog() specifies the value to return
12673 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 */
12675 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012676get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012677 typval_T *argvars;
12678 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012679 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012681 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 char_u *p = NULL;
12683 int c;
12684 char_u buf[NUMBUFLEN];
12685 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012687 int xp_type = EXPAND_NOTHING;
12688 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012690 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012691 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692
12693#ifdef NO_CONSOLE_INPUT
12694 /* While starting up, there is no place to enter text. */
12695 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697#endif
12698
12699 cmd_silent = FALSE; /* Want to see the prompt. */
12700 if (prompt != NULL)
12701 {
12702 /* Only the part of the message after the last NL is considered as
12703 * prompt for the command line */
12704 p = vim_strrchr(prompt, '\n');
12705 if (p == NULL)
12706 p = prompt;
12707 else
12708 {
12709 ++p;
12710 c = *p;
12711 *p = NUL;
12712 msg_start();
12713 msg_clr_eos();
12714 msg_puts_attr(prompt, echo_attr);
12715 msg_didout = FALSE;
12716 msg_starthere();
12717 *p = c;
12718 }
12719 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012721 if (argvars[1].v_type != VAR_UNKNOWN)
12722 {
12723 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12724 if (defstr != NULL)
12725 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012727 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012728 {
12729 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012730 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012731 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012732
Bram Moolenaar4463f292005-09-25 22:20:24 +000012733 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012734
Bram Moolenaar4463f292005-09-25 22:20:24 +000012735 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12736 if (xp_name == NULL)
12737 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012738
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012739 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012740
Bram Moolenaar4463f292005-09-25 22:20:24 +000012741 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12742 &xp_arg) == FAIL)
12743 return;
12744 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012745 }
12746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012747 if (defstr != NULL)
12748 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012749 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12750 xp_type, xp_arg);
12751
12752 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 /* since the user typed this, no need to wait for return */
12755 need_wait_return = FALSE;
12756 msg_didout = FALSE;
12757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 cmd_silent = cmd_silent_save;
12759}
12760
12761/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012762 * "input()" function
12763 * Also handles inputsecret() when inputsecret is set.
12764 */
12765 static void
12766f_input(argvars, rettv)
12767 typval_T *argvars;
12768 typval_T *rettv;
12769{
12770 get_user_input(argvars, rettv, FALSE);
12771}
12772
12773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 * "inputdialog()" function
12775 */
12776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012778 typval_T *argvars;
12779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780{
12781#if defined(FEAT_GUI_TEXTDIALOG)
12782 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12783 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12784 {
12785 char_u *message;
12786 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012787 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012789 message = get_tv_string_chk(&argvars[0]);
12790 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012791 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012792 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 else
12794 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 if (message != NULL && defstr != NULL
12796 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012797 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 else
12800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012801 if (message != NULL && defstr != NULL
12802 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012803 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 rettv->vval.v_string = vim_strsave(
12805 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 }
12811 else
12812#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012813 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814}
12815
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012816/*
12817 * "inputlist()" function
12818 */
12819 static void
12820f_inputlist(argvars, rettv)
12821 typval_T *argvars;
12822 typval_T *rettv;
12823{
12824 listitem_T *li;
12825 int selected;
12826 int mouse_used;
12827
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012828#ifdef NO_CONSOLE_INPUT
12829 /* While starting up, there is no place to enter text. */
12830 if (no_console_input())
12831 return;
12832#endif
12833 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12834 {
12835 EMSG2(_(e_listarg), "inputlist()");
12836 return;
12837 }
12838
12839 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012840 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012841 lines_left = Rows; /* avoid more prompt */
12842 msg_scroll = TRUE;
12843 msg_clr_eos();
12844
12845 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12846 {
12847 msg_puts(get_tv_string(&li->li_tv));
12848 msg_putchar('\n');
12849 }
12850
12851 /* Ask for choice. */
12852 selected = prompt_for_number(&mouse_used);
12853 if (mouse_used)
12854 selected -= lines_left;
12855
12856 rettv->vval.v_number = selected;
12857}
12858
12859
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12861
12862/*
12863 * "inputrestore()" function
12864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012867 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869{
12870 if (ga_userinput.ga_len > 0)
12871 {
12872 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12874 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012875 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 }
12877 else if (p_verbose > 1)
12878 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012879 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012880 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 }
12882}
12883
12884/*
12885 * "inputsave()" function
12886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012889 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012892 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 if (ga_grow(&ga_userinput, 1) == OK)
12894 {
12895 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12896 + ga_userinput.ga_len);
12897 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012898 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 }
12900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902}
12903
12904/*
12905 * "inputsecret()" function
12906 */
12907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012909 typval_T *argvars;
12910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911{
12912 ++cmdline_star;
12913 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 --cmdline_star;
12916 --inputsecret_flag;
12917}
12918
12919/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012920 * "insert()" function
12921 */
12922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012924 typval_T *argvars;
12925 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012926{
12927 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012928 listitem_T *item;
12929 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012930 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012931
12932 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012933 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012934 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012935 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012936 {
12937 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012938 before = get_tv_number_chk(&argvars[2], &error);
12939 if (error)
12940 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012941
Bram Moolenaar758711c2005-02-02 23:11:38 +000012942 if (before == l->lv_len)
12943 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012944 else
12945 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012946 item = list_find(l, before);
12947 if (item == NULL)
12948 {
12949 EMSGN(_(e_listidx), before);
12950 l = NULL;
12951 }
12952 }
12953 if (l != NULL)
12954 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012955 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012956 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012957 }
12958 }
12959}
12960
12961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 * "isdirectory()" function
12963 */
12964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012966 typval_T *argvars;
12967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012969 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970}
12971
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012972/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012973 * "islocked()" function
12974 */
12975 static void
12976f_islocked(argvars, rettv)
12977 typval_T *argvars;
12978 typval_T *rettv;
12979{
12980 lval_T lv;
12981 char_u *end;
12982 dictitem_T *di;
12983
12984 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012985 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12986 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012987 if (end != NULL && lv.ll_name != NULL)
12988 {
12989 if (*end != NUL)
12990 EMSG(_(e_trailing));
12991 else
12992 {
12993 if (lv.ll_tv == NULL)
12994 {
12995 if (check_changedtick(lv.ll_name))
12996 rettv->vval.v_number = 1; /* always locked */
12997 else
12998 {
12999 di = find_var(lv.ll_name, NULL);
13000 if (di != NULL)
13001 {
13002 /* Consider a variable locked when:
13003 * 1. the variable itself is locked
13004 * 2. the value of the variable is locked.
13005 * 3. the List or Dict value is locked.
13006 */
13007 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13008 || tv_islocked(&di->di_tv));
13009 }
13010 }
13011 }
13012 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013013 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013014 else if (lv.ll_newkey != NULL)
13015 EMSG2(_(e_dictkey), lv.ll_newkey);
13016 else if (lv.ll_list != NULL)
13017 /* List item. */
13018 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13019 else
13020 /* Dictionary item. */
13021 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13022 }
13023 }
13024
13025 clear_lval(&lv);
13026}
13027
Bram Moolenaar33570922005-01-25 22:26:29 +000013028static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013029
13030/*
13031 * Turn a dict into a list:
13032 * "what" == 0: list of keys
13033 * "what" == 1: list of values
13034 * "what" == 2: list of items
13035 */
13036 static void
13037dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013038 typval_T *argvars;
13039 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013040 int what;
13041{
Bram Moolenaar33570922005-01-25 22:26:29 +000013042 list_T *l2;
13043 dictitem_T *di;
13044 hashitem_T *hi;
13045 listitem_T *li;
13046 listitem_T *li2;
13047 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013048 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013049
Bram Moolenaar8c711452005-01-14 21:53:12 +000013050 if (argvars[0].v_type != VAR_DICT)
13051 {
13052 EMSG(_(e_dictreq));
13053 return;
13054 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013055 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013056 return;
13057
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013058 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013059 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013060
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013061 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013064 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013065 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013066 --todo;
13067 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013068
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013069 li = listitem_alloc();
13070 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013071 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013072 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013073
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013074 if (what == 0)
13075 {
13076 /* keys() */
13077 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013078 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013079 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13080 }
13081 else if (what == 1)
13082 {
13083 /* values() */
13084 copy_tv(&di->di_tv, &li->li_tv);
13085 }
13086 else
13087 {
13088 /* items() */
13089 l2 = list_alloc();
13090 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013091 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013092 li->li_tv.vval.v_list = l2;
13093 if (l2 == NULL)
13094 break;
13095 ++l2->lv_refcount;
13096
13097 li2 = listitem_alloc();
13098 if (li2 == NULL)
13099 break;
13100 list_append(l2, li2);
13101 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013102 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013103 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13104
13105 li2 = listitem_alloc();
13106 if (li2 == NULL)
13107 break;
13108 list_append(l2, li2);
13109 copy_tv(&di->di_tv, &li2->li_tv);
13110 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013111 }
13112 }
13113}
13114
13115/*
13116 * "items(dict)" function
13117 */
13118 static void
13119f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013120 typval_T *argvars;
13121 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013122{
13123 dict_list(argvars, rettv, 2);
13124}
13125
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013127 * "join()" function
13128 */
13129 static void
13130f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013131 typval_T *argvars;
13132 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013133{
13134 garray_T ga;
13135 char_u *sep;
13136
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013137 if (argvars[0].v_type != VAR_LIST)
13138 {
13139 EMSG(_(e_listreq));
13140 return;
13141 }
13142 if (argvars[0].vval.v_list == NULL)
13143 return;
13144 if (argvars[1].v_type == VAR_UNKNOWN)
13145 sep = (char_u *)" ";
13146 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013147 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013148
13149 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013150
13151 if (sep != NULL)
13152 {
13153 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013154 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 ga_append(&ga, NUL);
13156 rettv->vval.v_string = (char_u *)ga.ga_data;
13157 }
13158 else
13159 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013160}
13161
13162/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013163 * "keys()" function
13164 */
13165 static void
13166f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013167 typval_T *argvars;
13168 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013169{
13170 dict_list(argvars, rettv, 0);
13171}
13172
13173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 * "last_buffer_nr()" function.
13175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180{
13181 int n = 0;
13182 buf_T *buf;
13183
13184 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13185 if (n < buf->b_fnum)
13186 n = buf->b_fnum;
13187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013189}
13190
13191/*
13192 * "len()" function
13193 */
13194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013196 typval_T *argvars;
13197 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013198{
13199 switch (argvars[0].v_type)
13200 {
13201 case VAR_STRING:
13202 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 rettv->vval.v_number = (varnumber_T)STRLEN(
13204 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205 break;
13206 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013208 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013209 case VAR_DICT:
13210 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13211 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013212 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013213 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013214 break;
13215 }
13216}
13217
Bram Moolenaar33570922005-01-25 22:26:29 +000013218static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013219
13220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *argvars;
13223 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013224 int type;
13225{
13226#ifdef FEAT_LIBCALL
13227 char_u *string_in;
13228 char_u **string_result;
13229 int nr_result;
13230#endif
13231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013233 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013235
13236 if (check_restricted() || check_secure())
13237 return;
13238
13239#ifdef FEAT_LIBCALL
13240 /* The first two args must be strings, otherwise its meaningless */
13241 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13242 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013243 string_in = NULL;
13244 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013245 string_in = argvars[2].vval.v_string;
13246 if (type == VAR_NUMBER)
13247 string_result = NULL;
13248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013250 if (mch_libcall(argvars[0].vval.v_string,
13251 argvars[1].vval.v_string,
13252 string_in,
13253 argvars[2].vval.v_number,
13254 string_result,
13255 &nr_result) == OK
13256 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013258 }
13259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260}
13261
13262/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013263 * "libcall()" function
13264 */
13265 static void
13266f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013267 typval_T *argvars;
13268 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013269{
13270 libcall_common(argvars, rettv, VAR_STRING);
13271}
13272
13273/*
13274 * "libcallnr()" function
13275 */
13276 static void
13277f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013278 typval_T *argvars;
13279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013280{
13281 libcall_common(argvars, rettv, VAR_NUMBER);
13282}
13283
13284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 * "line(string)" function
13286 */
13287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013289 typval_T *argvars;
13290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291{
13292 linenr_T lnum = 0;
13293 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013294 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013296 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 if (fp != NULL)
13298 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013299 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300}
13301
13302/*
13303 * "line2byte(lnum)" function
13304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013307 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309{
13310#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312#else
13313 linenr_T lnum;
13314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013317 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13320 if (rettv->vval.v_number >= 0)
13321 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322#endif
13323}
13324
13325/*
13326 * "lispindent(lnum)" function
13327 */
13328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013329f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013330 typval_T *argvars;
13331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332{
13333#ifdef FEAT_LISP
13334 pos_T pos;
13335 linenr_T lnum;
13336
13337 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013338 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13340 {
13341 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013342 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 curwin->w_cursor = pos;
13344 }
13345 else
13346#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013347 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348}
13349
13350/*
13351 * "localtime()" function
13352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013355 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013358 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359}
13360
Bram Moolenaar33570922005-01-25 22:26:29 +000013361static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362
13363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013364get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013365 typval_T *argvars;
13366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 int exact;
13368{
13369 char_u *keys;
13370 char_u *which;
13371 char_u buf[NUMBUFLEN];
13372 char_u *keys_buf = NULL;
13373 char_u *rhs;
13374 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013375 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013376 int get_dict = FALSE;
13377 mapblock_T *mp;
13378 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379
13380 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381 rettv->v_type = VAR_STRING;
13382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 if (*keys == NUL)
13386 return;
13387
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013388 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013390 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013391 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013392 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013393 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013394 if (argvars[3].v_type != VAR_UNKNOWN)
13395 get_dict = get_tv_number(&argvars[3]);
13396 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 else
13399 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013400 if (which == NULL)
13401 return;
13402
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 mode = get_map_mode(&which, 0);
13404
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013405 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013406 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013408
13409 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013411 /* Return a string. */
13412 if (rhs != NULL)
13413 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414
Bram Moolenaarbd743252010-10-20 21:23:33 +020013415 }
13416 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13417 {
13418 /* Return a dictionary. */
13419 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13420 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13421 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422
Bram Moolenaarbd743252010-10-20 21:23:33 +020013423 dict_add_nr_str(dict, "lhs", 0L, lhs);
13424 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13425 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13426 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13427 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13428 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13429 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13430 dict_add_nr_str(dict, "mode", 0L, mapmode);
13431
13432 vim_free(lhs);
13433 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434 }
13435}
13436
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013437#ifdef FEAT_FLOAT
13438/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013439 * "log()" function
13440 */
13441 static void
13442f_log(argvars, rettv)
13443 typval_T *argvars;
13444 typval_T *rettv;
13445{
13446 float_T f;
13447
13448 rettv->v_type = VAR_FLOAT;
13449 if (get_float_arg(argvars, &f) == OK)
13450 rettv->vval.v_float = log(f);
13451 else
13452 rettv->vval.v_float = 0.0;
13453}
13454
13455/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013456 * "log10()" function
13457 */
13458 static void
13459f_log10(argvars, rettv)
13460 typval_T *argvars;
13461 typval_T *rettv;
13462{
13463 float_T f;
13464
13465 rettv->v_type = VAR_FLOAT;
13466 if (get_float_arg(argvars, &f) == OK)
13467 rettv->vval.v_float = log10(f);
13468 else
13469 rettv->vval.v_float = 0.0;
13470}
13471#endif
13472
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013474 * "map()" function
13475 */
13476 static void
13477f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 typval_T *argvars;
13479 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013480{
13481 filter_map(argvars, rettv, TRUE);
13482}
13483
13484/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013485 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 */
13487 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013488f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013489 typval_T *argvars;
13490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493}
13494
13495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013496 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 */
13498 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013503 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504}
13505
Bram Moolenaar33570922005-01-25 22:26:29 +000013506static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507
13508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013509find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013510 typval_T *argvars;
13511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 int type;
13513{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013514 char_u *str = NULL;
13515 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 char_u *pat;
13517 regmatch_T regmatch;
13518 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013519 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 char_u *save_cpo;
13521 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013522 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013523 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013524 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013525 list_T *l = NULL;
13526 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013527 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013528 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529
13530 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13531 save_cpo = p_cpo;
13532 p_cpo = (char_u *)"";
13533
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013534 rettv->vval.v_number = -1;
13535 if (type == 3)
13536 {
13537 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013538 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013539 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013540 }
13541 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543 rettv->v_type = VAR_STRING;
13544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013547 if (argvars[0].v_type == VAR_LIST)
13548 {
13549 if ((l = argvars[0].vval.v_list) == NULL)
13550 goto theend;
13551 li = l->lv_first;
13552 }
13553 else
13554 expr = str = get_tv_string(&argvars[0]);
13555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13557 if (pat == NULL)
13558 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013559
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013562 int error = FALSE;
13563
13564 start = get_tv_number_chk(&argvars[2], &error);
13565 if (error)
13566 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013567 if (l != NULL)
13568 {
13569 li = list_find(l, start);
13570 if (li == NULL)
13571 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013572 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013573 }
13574 else
13575 {
13576 if (start < 0)
13577 start = 0;
13578 if (start > (long)STRLEN(str))
13579 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013580 /* When "count" argument is there ignore matches before "start",
13581 * otherwise skip part of the string. Differs when pattern is "^"
13582 * or "\<". */
13583 if (argvars[3].v_type != VAR_UNKNOWN)
13584 startcol = start;
13585 else
13586 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013587 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013588
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 nth = get_tv_number_chk(&argvars[3], &error);
13591 if (error)
13592 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 }
13594
13595 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13596 if (regmatch.regprog != NULL)
13597 {
13598 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013599
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013600 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013601 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013602 if (l != NULL)
13603 {
13604 if (li == NULL)
13605 {
13606 match = FALSE;
13607 break;
13608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013609 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013610 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013611 if (str == NULL)
13612 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013613 }
13614
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013615 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013616
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013617 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013618 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013619 if (l == NULL && !match)
13620 break;
13621
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013622 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013623 if (l != NULL)
13624 {
13625 li = li->li_next;
13626 ++idx;
13627 }
13628 else
13629 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013630#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013631 startcol = (colnr_T)(regmatch.startp[0]
13632 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013633#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013634 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013635#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013636 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013637 }
13638
13639 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013641 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013642 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013643 int i;
13644
13645 /* return list with matched string and submatches */
13646 for (i = 0; i < NSUBEXP; ++i)
13647 {
13648 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013649 {
13650 if (list_append_string(rettv->vval.v_list,
13651 (char_u *)"", 0) == FAIL)
13652 break;
13653 }
13654 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013655 regmatch.startp[i],
13656 (int)(regmatch.endp[i] - regmatch.startp[i]))
13657 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013658 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013659 }
13660 }
13661 else if (type == 2)
13662 {
13663 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013664 if (l != NULL)
13665 copy_tv(&li->li_tv, rettv);
13666 else
13667 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013669 }
13670 else if (l != NULL)
13671 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 else
13673 {
13674 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013675 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 (varnumber_T)(regmatch.startp[0] - str);
13677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013680 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 }
13682 }
13683 vim_free(regmatch.regprog);
13684 }
13685
13686theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013687 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 p_cpo = save_cpo;
13689}
13690
13691/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692 * "match()" function
13693 */
13694 static void
13695f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013696 typval_T *argvars;
13697 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013698{
13699 find_some_match(argvars, rettv, 1);
13700}
13701
13702/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013703 * "matchadd()" function
13704 */
13705 static void
13706f_matchadd(argvars, rettv)
13707 typval_T *argvars;
13708 typval_T *rettv;
13709{
13710#ifdef FEAT_SEARCH_EXTRA
13711 char_u buf[NUMBUFLEN];
13712 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13713 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13714 int prio = 10; /* default priority */
13715 int id = -1;
13716 int error = FALSE;
13717
13718 rettv->vval.v_number = -1;
13719
13720 if (grp == NULL || pat == NULL)
13721 return;
13722 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013723 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013724 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013725 if (argvars[3].v_type != VAR_UNKNOWN)
13726 id = get_tv_number_chk(&argvars[3], &error);
13727 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013728 if (error == TRUE)
13729 return;
13730 if (id >= 1 && id <= 3)
13731 {
13732 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13733 return;
13734 }
13735
13736 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13737#endif
13738}
13739
13740/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013741 * "matcharg()" function
13742 */
13743 static void
13744f_matcharg(argvars, rettv)
13745 typval_T *argvars;
13746 typval_T *rettv;
13747{
13748 if (rettv_list_alloc(rettv) == OK)
13749 {
13750#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013751 int id = get_tv_number(&argvars[0]);
13752 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013753
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013754 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013755 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013756 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13757 {
13758 list_append_string(rettv->vval.v_list,
13759 syn_id2name(m->hlg_id), -1);
13760 list_append_string(rettv->vval.v_list, m->pattern, -1);
13761 }
13762 else
13763 {
13764 list_append_string(rettv->vval.v_list, NUL, -1);
13765 list_append_string(rettv->vval.v_list, NUL, -1);
13766 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013767 }
13768#endif
13769 }
13770}
13771
13772/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013773 * "matchdelete()" function
13774 */
13775 static void
13776f_matchdelete(argvars, rettv)
13777 typval_T *argvars;
13778 typval_T *rettv;
13779{
13780#ifdef FEAT_SEARCH_EXTRA
13781 rettv->vval.v_number = match_delete(curwin,
13782 (int)get_tv_number(&argvars[0]), TRUE);
13783#endif
13784}
13785
13786/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787 * "matchend()" function
13788 */
13789 static void
13790f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013791 typval_T *argvars;
13792 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793{
13794 find_some_match(argvars, rettv, 0);
13795}
13796
13797/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013798 * "matchlist()" function
13799 */
13800 static void
13801f_matchlist(argvars, rettv)
13802 typval_T *argvars;
13803 typval_T *rettv;
13804{
13805 find_some_match(argvars, rettv, 3);
13806}
13807
13808/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013809 * "matchstr()" function
13810 */
13811 static void
13812f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 typval_T *argvars;
13814 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013815{
13816 find_some_match(argvars, rettv, 2);
13817}
13818
Bram Moolenaar33570922005-01-25 22:26:29 +000013819static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013820
13821 static void
13822max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013823 typval_T *argvars;
13824 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013825 int domax;
13826{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013827 long n = 0;
13828 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013830
13831 if (argvars[0].v_type == VAR_LIST)
13832 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013833 list_T *l;
13834 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013835
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013836 l = argvars[0].vval.v_list;
13837 if (l != NULL)
13838 {
13839 li = l->lv_first;
13840 if (li != NULL)
13841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013842 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013843 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013844 {
13845 li = li->li_next;
13846 if (li == NULL)
13847 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013849 if (domax ? i > n : i < n)
13850 n = i;
13851 }
13852 }
13853 }
13854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013855 else if (argvars[0].v_type == VAR_DICT)
13856 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013857 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013858 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013859 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013860 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013861
13862 d = argvars[0].vval.v_dict;
13863 if (d != NULL)
13864 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013865 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013866 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013868 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013870 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013871 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013872 if (first)
13873 {
13874 n = i;
13875 first = FALSE;
13876 }
13877 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013878 n = i;
13879 }
13880 }
13881 }
13882 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013883 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013884 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013885 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013886}
13887
13888/*
13889 * "max()" function
13890 */
13891 static void
13892f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013893 typval_T *argvars;
13894 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013895{
13896 max_min(argvars, rettv, TRUE);
13897}
13898
13899/*
13900 * "min()" function
13901 */
13902 static void
13903f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013904 typval_T *argvars;
13905 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013906{
13907 max_min(argvars, rettv, FALSE);
13908}
13909
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013910static int mkdir_recurse __ARGS((char_u *dir, int prot));
13911
13912/*
13913 * Create the directory in which "dir" is located, and higher levels when
13914 * needed.
13915 */
13916 static int
13917mkdir_recurse(dir, prot)
13918 char_u *dir;
13919 int prot;
13920{
13921 char_u *p;
13922 char_u *updir;
13923 int r = FAIL;
13924
13925 /* Get end of directory name in "dir".
13926 * We're done when it's "/" or "c:/". */
13927 p = gettail_sep(dir);
13928 if (p <= get_past_head(dir))
13929 return OK;
13930
13931 /* If the directory exists we're done. Otherwise: create it.*/
13932 updir = vim_strnsave(dir, (int)(p - dir));
13933 if (updir == NULL)
13934 return FAIL;
13935 if (mch_isdir(updir))
13936 r = OK;
13937 else if (mkdir_recurse(updir, prot) == OK)
13938 r = vim_mkdir_emsg(updir, prot);
13939 vim_free(updir);
13940 return r;
13941}
13942
13943#ifdef vim_mkdir
13944/*
13945 * "mkdir()" function
13946 */
13947 static void
13948f_mkdir(argvars, rettv)
13949 typval_T *argvars;
13950 typval_T *rettv;
13951{
13952 char_u *dir;
13953 char_u buf[NUMBUFLEN];
13954 int prot = 0755;
13955
13956 rettv->vval.v_number = FAIL;
13957 if (check_restricted() || check_secure())
13958 return;
13959
13960 dir = get_tv_string_buf(&argvars[0], buf);
13961 if (argvars[1].v_type != VAR_UNKNOWN)
13962 {
13963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013964 prot = get_tv_number_chk(&argvars[2], NULL);
13965 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013966 mkdir_recurse(dir, prot);
13967 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013969}
13970#endif
13971
Bram Moolenaar0d660222005-01-07 21:51:51 +000013972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 * "mode()" function
13974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013977 typval_T *argvars;
13978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013980 char_u buf[3];
13981
13982 buf[1] = NUL;
13983 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984
13985#ifdef FEAT_VISUAL
13986 if (VIsual_active)
13987 {
13988 if (VIsual_select)
13989 buf[0] = VIsual_mode + 's' - 'v';
13990 else
13991 buf[0] = VIsual_mode;
13992 }
13993 else
13994#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013995 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13996 || State == CONFIRM)
13997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013999 if (State == ASKMORE)
14000 buf[1] = 'm';
14001 else if (State == CONFIRM)
14002 buf[1] = '?';
14003 }
14004 else if (State == EXTERNCMD)
14005 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 else if (State & INSERT)
14007 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014008#ifdef FEAT_VREPLACE
14009 if (State & VREPLACE_FLAG)
14010 {
14011 buf[0] = 'R';
14012 buf[1] = 'v';
14013 }
14014 else
14015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 if (State & REPLACE_FLAG)
14017 buf[0] = 'R';
14018 else
14019 buf[0] = 'i';
14020 }
14021 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014024 if (exmode_active)
14025 buf[1] = 'v';
14026 }
14027 else if (exmode_active)
14028 {
14029 buf[0] = 'c';
14030 buf[1] = 'e';
14031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014035 if (finish_op)
14036 buf[1] = 'o';
14037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014039 /* Clear out the minor mode when the argument is not a non-zero number or
14040 * non-empty string. */
14041 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014042 buf[1] = NUL;
14043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 rettv->vval.v_string = vim_strsave(buf);
14045 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046}
14047
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014048#ifdef FEAT_MZSCHEME
14049/*
14050 * "mzeval()" function
14051 */
14052 static void
14053f_mzeval(argvars, rettv)
14054 typval_T *argvars;
14055 typval_T *rettv;
14056{
14057 char_u *str;
14058 char_u buf[NUMBUFLEN];
14059
14060 str = get_tv_string_buf(&argvars[0], buf);
14061 do_mzeval(str, rettv);
14062}
14063#endif
14064
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 * "nextnonblank()" function
14067 */
14068 static void
14069f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014070 typval_T *argvars;
14071 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014072{
14073 linenr_T lnum;
14074
14075 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078 {
14079 lnum = 0;
14080 break;
14081 }
14082 if (*skipwhite(ml_get(lnum)) != NUL)
14083 break;
14084 }
14085 rettv->vval.v_number = lnum;
14086}
14087
14088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 * "nr2char()" function
14090 */
14091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014093 typval_T *argvars;
14094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095{
14096 char_u buf[NUMBUFLEN];
14097
14098#ifdef FEAT_MBYTE
14099 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 else
14102#endif
14103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014104 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 buf[1] = NUL;
14106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 rettv->v_type = VAR_STRING;
14108 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014109}
14110
14111/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014112 * "pathshorten()" function
14113 */
14114 static void
14115f_pathshorten(argvars, rettv)
14116 typval_T *argvars;
14117 typval_T *rettv;
14118{
14119 char_u *p;
14120
14121 rettv->v_type = VAR_STRING;
14122 p = get_tv_string_chk(&argvars[0]);
14123 if (p == NULL)
14124 rettv->vval.v_string = NULL;
14125 else
14126 {
14127 p = vim_strsave(p);
14128 rettv->vval.v_string = p;
14129 if (p != NULL)
14130 shorten_dir(p);
14131 }
14132}
14133
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014134#ifdef FEAT_FLOAT
14135/*
14136 * "pow()" function
14137 */
14138 static void
14139f_pow(argvars, rettv)
14140 typval_T *argvars;
14141 typval_T *rettv;
14142{
14143 float_T fx, fy;
14144
14145 rettv->v_type = VAR_FLOAT;
14146 if (get_float_arg(argvars, &fx) == OK
14147 && get_float_arg(&argvars[1], &fy) == OK)
14148 rettv->vval.v_float = pow(fx, fy);
14149 else
14150 rettv->vval.v_float = 0.0;
14151}
14152#endif
14153
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014154/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155 * "prevnonblank()" function
14156 */
14157 static void
14158f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014159 typval_T *argvars;
14160 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014161{
14162 linenr_T lnum;
14163
14164 lnum = get_tv_lnum(argvars);
14165 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14166 lnum = 0;
14167 else
14168 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14169 --lnum;
14170 rettv->vval.v_number = lnum;
14171}
14172
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014173#ifdef HAVE_STDARG_H
14174/* This dummy va_list is here because:
14175 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14176 * - locally in the function results in a "used before set" warning
14177 * - using va_start() to initialize it gives "function with fixed args" error */
14178static va_list ap;
14179#endif
14180
Bram Moolenaar8c711452005-01-14 21:53:12 +000014181/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014182 * "printf()" function
14183 */
14184 static void
14185f_printf(argvars, rettv)
14186 typval_T *argvars;
14187 typval_T *rettv;
14188{
14189 rettv->v_type = VAR_STRING;
14190 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014191#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014192 {
14193 char_u buf[NUMBUFLEN];
14194 int len;
14195 char_u *s;
14196 int saved_did_emsg = did_emsg;
14197 char *fmt;
14198
14199 /* Get the required length, allocate the buffer and do it for real. */
14200 did_emsg = FALSE;
14201 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014202 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014203 if (!did_emsg)
14204 {
14205 s = alloc(len + 1);
14206 if (s != NULL)
14207 {
14208 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014209 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014210 }
14211 }
14212 did_emsg |= saved_did_emsg;
14213 }
14214#endif
14215}
14216
14217/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014218 * "pumvisible()" function
14219 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014220 static void
14221f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014222 typval_T *argvars UNUSED;
14223 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014224{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014225#ifdef FEAT_INS_EXPAND
14226 if (pum_visible())
14227 rettv->vval.v_number = 1;
14228#endif
14229}
14230
14231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014232 * "range()" function
14233 */
14234 static void
14235f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014236 typval_T *argvars;
14237 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014238{
14239 long start;
14240 long end;
14241 long stride = 1;
14242 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014245 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014246 if (argvars[1].v_type == VAR_UNKNOWN)
14247 {
14248 end = start - 1;
14249 start = 0;
14250 }
14251 else
14252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014253 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014254 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014256 }
14257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014258 if (error)
14259 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014260 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014261 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014262 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014263 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014264 else
14265 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014266 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014267 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014268 if (list_append_number(rettv->vval.v_list,
14269 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014270 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014271 }
14272}
14273
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014274/*
14275 * "readfile()" function
14276 */
14277 static void
14278f_readfile(argvars, rettv)
14279 typval_T *argvars;
14280 typval_T *rettv;
14281{
14282 int binary = FALSE;
14283 char_u *fname;
14284 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014285 listitem_T *li;
14286#define FREAD_SIZE 200 /* optimized for text lines */
14287 char_u buf[FREAD_SIZE];
14288 int readlen; /* size of last fread() */
14289 int buflen; /* nr of valid chars in buf[] */
14290 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14291 int tolist; /* first byte in buf[] still to be put in list */
14292 int chop; /* how many CR to chop off */
14293 char_u *prev = NULL; /* previously read bytes, if any */
14294 int prevlen = 0; /* length of "prev" if not NULL */
14295 char_u *s;
14296 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014297 long maxline = MAXLNUM;
14298 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014299
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014300 if (argvars[1].v_type != VAR_UNKNOWN)
14301 {
14302 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14303 binary = TRUE;
14304 if (argvars[2].v_type != VAR_UNKNOWN)
14305 maxline = get_tv_number(&argvars[2]);
14306 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014308 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014309 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014310
14311 /* Always open the file in binary mode, library functions have a mind of
14312 * their own about CR-LF conversion. */
14313 fname = get_tv_string(&argvars[0]);
14314 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14315 {
14316 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14317 return;
14318 }
14319
14320 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014321 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014322 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014323 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014324 buflen = filtd + readlen;
14325 tolist = 0;
14326 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14327 {
Bram Moolenaarf56a6de2011-07-07 17:36:56 +020014328 if (readlen <= 0 || buf[filtd] == '\n')
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014329 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014330 /* In binary mode add an empty list item when the last
14331 * non-empty line ends in a '\n'. */
14332 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014333 break;
14334
14335 /* Found end-of-line or end-of-file: add a text line to the
14336 * list. */
14337 chop = 0;
14338 if (!binary)
14339 while (filtd - chop - 1 >= tolist
14340 && buf[filtd - chop - 1] == '\r')
14341 ++chop;
14342 len = filtd - tolist - chop;
14343 if (prev == NULL)
14344 s = vim_strnsave(buf + tolist, len);
14345 else
14346 {
14347 s = alloc((unsigned)(prevlen + len + 1));
14348 if (s != NULL)
14349 {
14350 mch_memmove(s, prev, prevlen);
14351 vim_free(prev);
14352 prev = NULL;
14353 mch_memmove(s + prevlen, buf + tolist, len);
14354 s[prevlen + len] = NUL;
14355 }
14356 }
14357 tolist = filtd + 1;
14358
14359 li = listitem_alloc();
14360 if (li == NULL)
14361 {
14362 vim_free(s);
14363 break;
14364 }
14365 li->li_tv.v_type = VAR_STRING;
14366 li->li_tv.v_lock = 0;
14367 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014368 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014369
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014370 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014371 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014372 if (readlen <= 0)
14373 break;
14374 }
14375 else if (buf[filtd] == NUL)
14376 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014377#ifdef FEAT_MBYTE
14378 else if (buf[filtd] == 0xef
14379 && enc_utf8
14380 && filtd + 2 < buflen
14381 && !binary
14382 && buf[filtd + 1] == 0xbb
14383 && buf[filtd + 2] == 0xbf)
14384 {
14385 /* remove utf-8 byte order mark */
14386 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14387 --filtd;
14388 buflen -= 3;
14389 }
14390#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014391 }
14392 if (readlen <= 0)
14393 break;
14394
14395 if (tolist == 0)
14396 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014397 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014398 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014399 /* "buf" is full, need to move text to an allocated buffer */
14400 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014401 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014402 prev = vim_strnsave(buf, buflen);
14403 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014404 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014405 else
14406 {
14407 s = alloc((unsigned)(prevlen + buflen));
14408 if (s != NULL)
14409 {
14410 mch_memmove(s, prev, prevlen);
14411 mch_memmove(s + prevlen, buf, buflen);
14412 vim_free(prev);
14413 prev = s;
14414 prevlen += buflen;
14415 }
14416 }
14417 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014418 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014419 }
14420 else
14421 {
14422 mch_memmove(buf, buf + tolist, buflen - tolist);
14423 filtd -= tolist;
14424 }
14425 }
14426
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014427 /*
14428 * For a negative line count use only the lines at the end of the file,
14429 * free the rest.
14430 */
14431 if (maxline < 0)
14432 while (cnt > -maxline)
14433 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014434 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014435 --cnt;
14436 }
14437
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014438 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014439 fclose(fd);
14440}
14441
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014442#if defined(FEAT_RELTIME)
14443static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14444
14445/*
14446 * Convert a List to proftime_T.
14447 * Return FAIL when there is something wrong.
14448 */
14449 static int
14450list2proftime(arg, tm)
14451 typval_T *arg;
14452 proftime_T *tm;
14453{
14454 long n1, n2;
14455 int error = FALSE;
14456
14457 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14458 || arg->vval.v_list->lv_len != 2)
14459 return FAIL;
14460 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14461 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14462# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014463 tm->HighPart = n1;
14464 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014465# else
14466 tm->tv_sec = n1;
14467 tm->tv_usec = n2;
14468# endif
14469 return error ? FAIL : OK;
14470}
14471#endif /* FEAT_RELTIME */
14472
14473/*
14474 * "reltime()" function
14475 */
14476 static void
14477f_reltime(argvars, rettv)
14478 typval_T *argvars;
14479 typval_T *rettv;
14480{
14481#ifdef FEAT_RELTIME
14482 proftime_T res;
14483 proftime_T start;
14484
14485 if (argvars[0].v_type == VAR_UNKNOWN)
14486 {
14487 /* No arguments: get current time. */
14488 profile_start(&res);
14489 }
14490 else if (argvars[1].v_type == VAR_UNKNOWN)
14491 {
14492 if (list2proftime(&argvars[0], &res) == FAIL)
14493 return;
14494 profile_end(&res);
14495 }
14496 else
14497 {
14498 /* Two arguments: compute the difference. */
14499 if (list2proftime(&argvars[0], &start) == FAIL
14500 || list2proftime(&argvars[1], &res) == FAIL)
14501 return;
14502 profile_sub(&res, &start);
14503 }
14504
14505 if (rettv_list_alloc(rettv) == OK)
14506 {
14507 long n1, n2;
14508
14509# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014510 n1 = res.HighPart;
14511 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014512# else
14513 n1 = res.tv_sec;
14514 n2 = res.tv_usec;
14515# endif
14516 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14517 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14518 }
14519#endif
14520}
14521
14522/*
14523 * "reltimestr()" function
14524 */
14525 static void
14526f_reltimestr(argvars, rettv)
14527 typval_T *argvars;
14528 typval_T *rettv;
14529{
14530#ifdef FEAT_RELTIME
14531 proftime_T tm;
14532#endif
14533
14534 rettv->v_type = VAR_STRING;
14535 rettv->vval.v_string = NULL;
14536#ifdef FEAT_RELTIME
14537 if (list2proftime(&argvars[0], &tm) == OK)
14538 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14539#endif
14540}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014541
Bram Moolenaar0d660222005-01-07 21:51:51 +000014542#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14543static void make_connection __ARGS((void));
14544static int check_connection __ARGS((void));
14545
14546 static void
14547make_connection()
14548{
14549 if (X_DISPLAY == NULL
14550# ifdef FEAT_GUI
14551 && !gui.in_use
14552# endif
14553 )
14554 {
14555 x_force_connect = TRUE;
14556 setup_term_clip();
14557 x_force_connect = FALSE;
14558 }
14559}
14560
14561 static int
14562check_connection()
14563{
14564 make_connection();
14565 if (X_DISPLAY == NULL)
14566 {
14567 EMSG(_("E240: No connection to Vim server"));
14568 return FAIL;
14569 }
14570 return OK;
14571}
14572#endif
14573
14574#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014575static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576
14577 static void
14578remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 typval_T *argvars;
14580 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581 int expr;
14582{
14583 char_u *server_name;
14584 char_u *keys;
14585 char_u *r = NULL;
14586 char_u buf[NUMBUFLEN];
14587# ifdef WIN32
14588 HWND w;
14589# else
14590 Window w;
14591# endif
14592
14593 if (check_restricted() || check_secure())
14594 return;
14595
14596# ifdef FEAT_X11
14597 if (check_connection() == FAIL)
14598 return;
14599# endif
14600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 server_name = get_tv_string_chk(&argvars[0]);
14602 if (server_name == NULL)
14603 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604 keys = get_tv_string_buf(&argvars[1], buf);
14605# ifdef WIN32
14606 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14607# else
14608 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14609 < 0)
14610# endif
14611 {
14612 if (r != NULL)
14613 EMSG(r); /* sending worked but evaluation failed */
14614 else
14615 EMSG2(_("E241: Unable to send to %s"), server_name);
14616 return;
14617 }
14618
14619 rettv->vval.v_string = r;
14620
14621 if (argvars[2].v_type != VAR_UNKNOWN)
14622 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014623 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014624 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014626
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014627 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 v.di_tv.v_type = VAR_STRING;
14629 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 idvar = get_tv_string_chk(&argvars[2]);
14631 if (idvar != NULL)
14632 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014633 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634 }
14635}
14636#endif
14637
14638/*
14639 * "remote_expr()" function
14640 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641 static void
14642f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014643 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014644 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645{
14646 rettv->v_type = VAR_STRING;
14647 rettv->vval.v_string = NULL;
14648#ifdef FEAT_CLIENTSERVER
14649 remote_common(argvars, rettv, TRUE);
14650#endif
14651}
14652
14653/*
14654 * "remote_foreground()" function
14655 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656 static void
14657f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014658 typval_T *argvars UNUSED;
14659 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014660{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014661#ifdef FEAT_CLIENTSERVER
14662# ifdef WIN32
14663 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 {
14665 char_u *server_name = get_tv_string_chk(&argvars[0]);
14666
14667 if (server_name != NULL)
14668 serverForeground(server_name);
14669 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670# else
14671 /* Send a foreground() expression to the server. */
14672 argvars[1].v_type = VAR_STRING;
14673 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14674 argvars[2].v_type = VAR_UNKNOWN;
14675 remote_common(argvars, rettv, TRUE);
14676 vim_free(argvars[1].vval.v_string);
14677# endif
14678#endif
14679}
14680
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681 static void
14682f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014684 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014685{
14686#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014687 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014688 char_u *s = NULL;
14689# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014690 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014693
14694 if (check_restricted() || check_secure())
14695 {
14696 rettv->vval.v_number = -1;
14697 return;
14698 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014699 serverid = get_tv_string_chk(&argvars[0]);
14700 if (serverid == NULL)
14701 {
14702 rettv->vval.v_number = -1;
14703 return; /* type error; errmsg already given */
14704 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014706 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707 if (n == 0)
14708 rettv->vval.v_number = -1;
14709 else
14710 {
14711 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14712 rettv->vval.v_number = (s != NULL);
14713 }
14714# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014715 if (check_connection() == FAIL)
14716 return;
14717
14718 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014719 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720# endif
14721
14722 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724 char_u *retvar;
14725
Bram Moolenaar33570922005-01-25 22:26:29 +000014726 v.di_tv.v_type = VAR_STRING;
14727 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014728 retvar = get_tv_string_chk(&argvars[1]);
14729 if (retvar != NULL)
14730 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014731 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014732 }
14733#else
14734 rettv->vval.v_number = -1;
14735#endif
14736}
14737
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738 static void
14739f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014740 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014742{
14743 char_u *r = NULL;
14744
14745#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 char_u *serverid = get_tv_string_chk(&argvars[0]);
14747
14748 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749 {
14750# ifdef WIN32
14751 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014752 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014754 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014755 if (n != 0)
14756 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14757 if (r == NULL)
14758# else
14759 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014761# endif
14762 EMSG(_("E277: Unable to read a server reply"));
14763 }
14764#endif
14765 rettv->v_type = VAR_STRING;
14766 rettv->vval.v_string = r;
14767}
14768
14769/*
14770 * "remote_send()" function
14771 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014772 static void
14773f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014774 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014775 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014776{
14777 rettv->v_type = VAR_STRING;
14778 rettv->vval.v_string = NULL;
14779#ifdef FEAT_CLIENTSERVER
14780 remote_common(argvars, rettv, FALSE);
14781#endif
14782}
14783
14784/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014785 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014786 */
14787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014789 typval_T *argvars;
14790 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014791{
Bram Moolenaar33570922005-01-25 22:26:29 +000014792 list_T *l;
14793 listitem_T *item, *item2;
14794 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014795 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014796 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014797 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014798 dict_T *d;
14799 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014800 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801
Bram Moolenaar8c711452005-01-14 21:53:12 +000014802 if (argvars[0].v_type == VAR_DICT)
14803 {
14804 if (argvars[2].v_type != VAR_UNKNOWN)
14805 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014806 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014807 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014809 key = get_tv_string_chk(&argvars[1]);
14810 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014812 di = dict_find(d, key, -1);
14813 if (di == NULL)
14814 EMSG2(_(e_dictkey), key);
14815 else
14816 {
14817 *rettv = di->di_tv;
14818 init_tv(&di->di_tv);
14819 dictitem_remove(d, di);
14820 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014822 }
14823 }
14824 else if (argvars[0].v_type != VAR_LIST)
14825 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014826 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014827 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014829 int error = FALSE;
14830
14831 idx = get_tv_number_chk(&argvars[1], &error);
14832 if (error)
14833 ; /* type error: do nothing, errmsg already given */
14834 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014835 EMSGN(_(e_listidx), idx);
14836 else
14837 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014838 if (argvars[2].v_type == VAR_UNKNOWN)
14839 {
14840 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014841 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014842 *rettv = item->li_tv;
14843 vim_free(item);
14844 }
14845 else
14846 {
14847 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014848 end = get_tv_number_chk(&argvars[2], &error);
14849 if (error)
14850 ; /* type error: do nothing */
14851 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014852 EMSGN(_(e_listidx), end);
14853 else
14854 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014855 int cnt = 0;
14856
14857 for (li = item; li != NULL; li = li->li_next)
14858 {
14859 ++cnt;
14860 if (li == item2)
14861 break;
14862 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014863 if (li == NULL) /* didn't find "item2" after "item" */
14864 EMSG(_(e_invrange));
14865 else
14866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014867 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014868 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014869 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014870 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014871 l->lv_first = item;
14872 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014873 item->li_prev = NULL;
14874 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014875 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014876 }
14877 }
14878 }
14879 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014880 }
14881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882}
14883
14884/*
14885 * "rename({from}, {to})" function
14886 */
14887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014889 typval_T *argvars;
14890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891{
14892 char_u buf[NUMBUFLEN];
14893
14894 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014895 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014897 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14898 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899}
14900
14901/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014902 * "repeat()" function
14903 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014904 static void
14905f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014906 typval_T *argvars;
14907 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014908{
14909 char_u *p;
14910 int n;
14911 int slen;
14912 int len;
14913 char_u *r;
14914 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014915
14916 n = get_tv_number(&argvars[1]);
14917 if (argvars[0].v_type == VAR_LIST)
14918 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014919 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014920 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014921 if (list_extend(rettv->vval.v_list,
14922 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014923 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014924 }
14925 else
14926 {
14927 p = get_tv_string(&argvars[0]);
14928 rettv->v_type = VAR_STRING;
14929 rettv->vval.v_string = NULL;
14930
14931 slen = (int)STRLEN(p);
14932 len = slen * n;
14933 if (len <= 0)
14934 return;
14935
14936 r = alloc(len + 1);
14937 if (r != NULL)
14938 {
14939 for (i = 0; i < n; i++)
14940 mch_memmove(r + i * slen, p, (size_t)slen);
14941 r[len] = NUL;
14942 }
14943
14944 rettv->vval.v_string = r;
14945 }
14946}
14947
14948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 * "resolve()" function
14950 */
14951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014952f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 typval_T *argvars;
14954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955{
14956 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020014957#ifdef HAVE_READLINK
14958 char_u *buf = NULL;
14959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962#ifdef FEAT_SHORTCUT
14963 {
14964 char_u *v = NULL;
14965
14966 v = mch_resolve_shortcut(p);
14967 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014968 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014970 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 }
14972#else
14973# ifdef HAVE_READLINK
14974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 char_u *cpy;
14976 int len;
14977 char_u *remain = NULL;
14978 char_u *q;
14979 int is_relative_to_current = FALSE;
14980 int has_trailing_pathsep = FALSE;
14981 int limit = 100;
14982
14983 p = vim_strsave(p);
14984
14985 if (p[0] == '.' && (vim_ispathsep(p[1])
14986 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14987 is_relative_to_current = TRUE;
14988
14989 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014990 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014993 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
14994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
14996 q = getnextcomp(p);
14997 if (*q != NUL)
14998 {
14999 /* Separate the first path component in "p", and keep the
15000 * remainder (beginning with the path separator). */
15001 remain = vim_strsave(q - 1);
15002 q[-1] = NUL;
15003 }
15004
Bram Moolenaard9462e32011-04-11 21:35:11 +020015005 buf = alloc(MAXPATHL + 1);
15006 if (buf == NULL)
15007 goto fail;
15008
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 for (;;)
15010 {
15011 for (;;)
15012 {
15013 len = readlink((char *)p, (char *)buf, MAXPATHL);
15014 if (len <= 0)
15015 break;
15016 buf[len] = NUL;
15017
15018 if (limit-- == 0)
15019 {
15020 vim_free(p);
15021 vim_free(remain);
15022 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 goto fail;
15025 }
15026
15027 /* Ensure that the result will have a trailing path separator
15028 * if the argument has one. */
15029 if (remain == NULL && has_trailing_pathsep)
15030 add_pathsep(buf);
15031
15032 /* Separate the first path component in the link value and
15033 * concatenate the remainders. */
15034 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15035 if (*q != NUL)
15036 {
15037 if (remain == NULL)
15038 remain = vim_strsave(q - 1);
15039 else
15040 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015041 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 if (cpy != NULL)
15043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 vim_free(remain);
15045 remain = cpy;
15046 }
15047 }
15048 q[-1] = NUL;
15049 }
15050
15051 q = gettail(p);
15052 if (q > p && *q == NUL)
15053 {
15054 /* Ignore trailing path separator. */
15055 q[-1] = NUL;
15056 q = gettail(p);
15057 }
15058 if (q > p && !mch_isFullName(buf))
15059 {
15060 /* symlink is relative to directory of argument */
15061 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15062 if (cpy != NULL)
15063 {
15064 STRCPY(cpy, p);
15065 STRCPY(gettail(cpy), buf);
15066 vim_free(p);
15067 p = cpy;
15068 }
15069 }
15070 else
15071 {
15072 vim_free(p);
15073 p = vim_strsave(buf);
15074 }
15075 }
15076
15077 if (remain == NULL)
15078 break;
15079
15080 /* Append the first path component of "remain" to "p". */
15081 q = getnextcomp(remain + 1);
15082 len = q - remain - (*q != NUL);
15083 cpy = vim_strnsave(p, STRLEN(p) + len);
15084 if (cpy != NULL)
15085 {
15086 STRNCAT(cpy, remain, len);
15087 vim_free(p);
15088 p = cpy;
15089 }
15090 /* Shorten "remain". */
15091 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015092 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 else
15094 {
15095 vim_free(remain);
15096 remain = NULL;
15097 }
15098 }
15099
15100 /* If the result is a relative path name, make it explicitly relative to
15101 * the current directory if and only if the argument had this form. */
15102 if (!vim_ispathsep(*p))
15103 {
15104 if (is_relative_to_current
15105 && *p != NUL
15106 && !(p[0] == '.'
15107 && (p[1] == NUL
15108 || vim_ispathsep(p[1])
15109 || (p[1] == '.'
15110 && (p[2] == NUL
15111 || vim_ispathsep(p[2]))))))
15112 {
15113 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015114 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 if (cpy != NULL)
15116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 vim_free(p);
15118 p = cpy;
15119 }
15120 }
15121 else if (!is_relative_to_current)
15122 {
15123 /* Strip leading "./". */
15124 q = p;
15125 while (q[0] == '.' && vim_ispathsep(q[1]))
15126 q += 2;
15127 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015128 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 }
15130 }
15131
15132 /* Ensure that the result will have no trailing path separator
15133 * if the argument had none. But keep "/" or "//". */
15134 if (!has_trailing_pathsep)
15135 {
15136 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015137 if (after_pathsep(p, q))
15138 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139 }
15140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 }
15143# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015144 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145# endif
15146#endif
15147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015148 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149
15150#ifdef HAVE_READLINK
15151fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015152 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015154 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155}
15156
15157/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159 */
15160 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015161f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015162 typval_T *argvars;
15163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164{
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 list_T *l;
15166 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168 if (argvars[0].v_type != VAR_LIST)
15169 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015170 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015171 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172 {
15173 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015174 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015175 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 while (li != NULL)
15177 {
15178 ni = li->li_prev;
15179 list_append(l, li);
15180 li = ni;
15181 }
15182 rettv->vval.v_list = l;
15183 rettv->v_type = VAR_LIST;
15184 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015185 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187}
15188
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015189#define SP_NOMOVE 0x01 /* don't move cursor */
15190#define SP_REPEAT 0x02 /* repeat to find outer pair */
15191#define SP_RETCOUNT 0x04 /* return matchcount */
15192#define SP_SETPCMARK 0x08 /* set previous context mark */
15193#define SP_START 0x10 /* accept match at start position */
15194#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15195#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015196
Bram Moolenaar33570922005-01-25 22:26:29 +000015197static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198
15199/*
15200 * Get flags for a search function.
15201 * Possibly sets "p_ws".
15202 * Returns BACKWARD, FORWARD or zero (for an error).
15203 */
15204 static int
15205get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015206 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207 int *flagsp;
15208{
15209 int dir = FORWARD;
15210 char_u *flags;
15211 char_u nbuf[NUMBUFLEN];
15212 int mask;
15213
15214 if (varp->v_type != VAR_UNKNOWN)
15215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 flags = get_tv_string_buf_chk(varp, nbuf);
15217 if (flags == NULL)
15218 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 while (*flags != NUL)
15220 {
15221 switch (*flags)
15222 {
15223 case 'b': dir = BACKWARD; break;
15224 case 'w': p_ws = TRUE; break;
15225 case 'W': p_ws = FALSE; break;
15226 default: mask = 0;
15227 if (flagsp != NULL)
15228 switch (*flags)
15229 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015230 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015231 case 'e': mask = SP_END; break;
15232 case 'm': mask = SP_RETCOUNT; break;
15233 case 'n': mask = SP_NOMOVE; break;
15234 case 'p': mask = SP_SUBPAT; break;
15235 case 'r': mask = SP_REPEAT; break;
15236 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237 }
15238 if (mask == 0)
15239 {
15240 EMSG2(_(e_invarg2), flags);
15241 dir = 0;
15242 }
15243 else
15244 *flagsp |= mask;
15245 }
15246 if (dir == 0)
15247 break;
15248 ++flags;
15249 }
15250 }
15251 return dir;
15252}
15253
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015255 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015257 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015258search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015259 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015260 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015261 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015263 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 char_u *pat;
15265 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015266 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 int save_p_ws = p_ws;
15268 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015269 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015270 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015271 proftime_T tm;
15272#ifdef FEAT_RELTIME
15273 long time_limit = 0;
15274#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015275 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015276 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015279 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015280 if (dir == 0)
15281 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015282 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015283 if (flags & SP_START)
15284 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015285 if (flags & SP_END)
15286 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015287
Bram Moolenaar76929292008-01-06 19:07:36 +000015288 /* Optional arguments: line number to stop searching and timeout. */
15289 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015290 {
15291 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15292 if (lnum_stop < 0)
15293 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015294#ifdef FEAT_RELTIME
15295 if (argvars[3].v_type != VAR_UNKNOWN)
15296 {
15297 time_limit = get_tv_number_chk(&argvars[3], NULL);
15298 if (time_limit < 0)
15299 goto theend;
15300 }
15301#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015302 }
15303
Bram Moolenaar76929292008-01-06 19:07:36 +000015304#ifdef FEAT_RELTIME
15305 /* Set the time limit, if there is one. */
15306 profile_setlimit(time_limit, &tm);
15307#endif
15308
Bram Moolenaar231334e2005-07-25 20:46:57 +000015309 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015310 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015311 * Check to make sure only those flags are set.
15312 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15313 * flags cannot be set. Check for that condition also.
15314 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015315 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015316 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015318 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015319 goto theend;
15320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015322 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015323 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015324 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015325 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015327 if (flags & SP_SUBPAT)
15328 retval = subpatnum;
15329 else
15330 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015331 if (flags & SP_SETPCMARK)
15332 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015334 if (match_pos != NULL)
15335 {
15336 /* Store the match cursor position */
15337 match_pos->lnum = pos.lnum;
15338 match_pos->col = pos.col + 1;
15339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340 /* "/$" will put the cursor after the end of the line, may need to
15341 * correct that here */
15342 check_cursor();
15343 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015344
15345 /* If 'n' flag is used: restore cursor position. */
15346 if (flags & SP_NOMOVE)
15347 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015348 else
15349 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015350theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015352
15353 return retval;
15354}
15355
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015356#ifdef FEAT_FLOAT
15357/*
15358 * "round({float})" function
15359 */
15360 static void
15361f_round(argvars, rettv)
15362 typval_T *argvars;
15363 typval_T *rettv;
15364{
15365 float_T f;
15366
15367 rettv->v_type = VAR_FLOAT;
15368 if (get_float_arg(argvars, &f) == OK)
15369 /* round() is not in C90, use ceil() or floor() instead. */
15370 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15371 else
15372 rettv->vval.v_float = 0.0;
15373}
15374#endif
15375
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015376/*
15377 * "search()" function
15378 */
15379 static void
15380f_search(argvars, rettv)
15381 typval_T *argvars;
15382 typval_T *rettv;
15383{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015384 int flags = 0;
15385
15386 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387}
15388
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015390 * "searchdecl()" function
15391 */
15392 static void
15393f_searchdecl(argvars, rettv)
15394 typval_T *argvars;
15395 typval_T *rettv;
15396{
15397 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015398 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015399 int error = FALSE;
15400 char_u *name;
15401
15402 rettv->vval.v_number = 1; /* default: FAIL */
15403
15404 name = get_tv_string_chk(&argvars[0]);
15405 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015406 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015407 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015408 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15409 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15410 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015411 if (!error && name != NULL)
15412 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015413 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015414}
15415
15416/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015417 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015419 static int
15420searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015422 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423{
15424 char_u *spat, *mpat, *epat;
15425 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 int dir;
15428 int flags = 0;
15429 char_u nbuf1[NUMBUFLEN];
15430 char_u nbuf2[NUMBUFLEN];
15431 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015432 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015433 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015434 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015437 spat = get_tv_string_chk(&argvars[0]);
15438 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15439 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15440 if (spat == NULL || mpat == NULL || epat == NULL)
15441 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 /* Handle the optional fourth argument: flags */
15444 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015445 if (dir == 0)
15446 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015447
15448 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015449 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15450 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015451 if ((flags & (SP_END | SP_SUBPAT)) != 0
15452 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015453 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015454 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015455 goto theend;
15456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015458 /* Using 'r' implies 'W', otherwise it doesn't work. */
15459 if (flags & SP_REPEAT)
15460 p_ws = FALSE;
15461
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015462 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015463 if (argvars[3].v_type == VAR_UNKNOWN
15464 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 skip = (char_u *)"";
15466 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015468 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015469 if (argvars[5].v_type != VAR_UNKNOWN)
15470 {
15471 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15472 if (lnum_stop < 0)
15473 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015474#ifdef FEAT_RELTIME
15475 if (argvars[6].v_type != VAR_UNKNOWN)
15476 {
15477 time_limit = get_tv_number_chk(&argvars[6], NULL);
15478 if (time_limit < 0)
15479 goto theend;
15480 }
15481#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015482 }
15483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015484 if (skip == NULL)
15485 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015487 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015488 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015489
15490theend:
15491 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015492
15493 return retval;
15494}
15495
15496/*
15497 * "searchpair()" function
15498 */
15499 static void
15500f_searchpair(argvars, rettv)
15501 typval_T *argvars;
15502 typval_T *rettv;
15503{
15504 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15505}
15506
15507/*
15508 * "searchpairpos()" function
15509 */
15510 static void
15511f_searchpairpos(argvars, rettv)
15512 typval_T *argvars;
15513 typval_T *rettv;
15514{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015515 pos_T match_pos;
15516 int lnum = 0;
15517 int col = 0;
15518
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015519 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015520 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015521
15522 if (searchpair_cmn(argvars, &match_pos) > 0)
15523 {
15524 lnum = match_pos.lnum;
15525 col = match_pos.col;
15526 }
15527
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015528 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15529 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015530}
15531
15532/*
15533 * Search for a start/middle/end thing.
15534 * Used by searchpair(), see its documentation for the details.
15535 * Returns 0 or -1 for no match,
15536 */
15537 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015538do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15539 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015540 char_u *spat; /* start pattern */
15541 char_u *mpat; /* middle pattern */
15542 char_u *epat; /* end pattern */
15543 int dir; /* BACKWARD or FORWARD */
15544 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015545 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015546 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015547 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015548 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015549{
15550 char_u *save_cpo;
15551 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15552 long retval = 0;
15553 pos_T pos;
15554 pos_T firstpos;
15555 pos_T foundpos;
15556 pos_T save_cursor;
15557 pos_T save_pos;
15558 int n;
15559 int r;
15560 int nest = 1;
15561 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015562 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015563 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015564
15565 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15566 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015567 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015568
Bram Moolenaar76929292008-01-06 19:07:36 +000015569#ifdef FEAT_RELTIME
15570 /* Set the time limit, if there is one. */
15571 profile_setlimit(time_limit, &tm);
15572#endif
15573
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015574 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15575 * start/middle/end (pat3, for the top pair). */
15576 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15577 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15578 if (pat2 == NULL || pat3 == NULL)
15579 goto theend;
15580 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15581 if (*mpat == NUL)
15582 STRCPY(pat3, pat2);
15583 else
15584 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15585 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015586 if (flags & SP_START)
15587 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015588
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 save_cursor = curwin->w_cursor;
15590 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015591 clearpos(&firstpos);
15592 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 pat = pat3;
15594 for (;;)
15595 {
15596 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015597 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15599 /* didn't find it or found the first match again: FAIL */
15600 break;
15601
15602 if (firstpos.lnum == 0)
15603 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015604 if (equalpos(pos, foundpos))
15605 {
15606 /* Found the same position again. Can happen with a pattern that
15607 * has "\zs" at the end and searching backwards. Advance one
15608 * character and try again. */
15609 if (dir == BACKWARD)
15610 decl(&pos);
15611 else
15612 incl(&pos);
15613 }
15614 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015616 /* clear the start flag to avoid getting stuck here */
15617 options &= ~SEARCH_START;
15618
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 /* If the skip pattern matches, ignore this match. */
15620 if (*skip != NUL)
15621 {
15622 save_pos = curwin->w_cursor;
15623 curwin->w_cursor = pos;
15624 r = eval_to_bool(skip, &err, NULL, FALSE);
15625 curwin->w_cursor = save_pos;
15626 if (err)
15627 {
15628 /* Evaluating {skip} caused an error, break here. */
15629 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015630 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 break;
15632 }
15633 if (r)
15634 continue;
15635 }
15636
15637 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15638 {
15639 /* Found end when searching backwards or start when searching
15640 * forward: nested pair. */
15641 ++nest;
15642 pat = pat2; /* nested, don't search for middle */
15643 }
15644 else
15645 {
15646 /* Found end when searching forward or start when searching
15647 * backward: end of (nested) pair; or found middle in outer pair. */
15648 if (--nest == 1)
15649 pat = pat3; /* outer level, search for middle */
15650 }
15651
15652 if (nest == 0)
15653 {
15654 /* Found the match: return matchcount or line number. */
15655 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015656 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015658 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015659 if (flags & SP_SETPCMARK)
15660 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 curwin->w_cursor = pos;
15662 if (!(flags & SP_REPEAT))
15663 break;
15664 nest = 1; /* search for next unmatched */
15665 }
15666 }
15667
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015668 if (match_pos != NULL)
15669 {
15670 /* Store the match cursor position */
15671 match_pos->lnum = curwin->w_cursor.lnum;
15672 match_pos->col = curwin->w_cursor.col + 1;
15673 }
15674
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015676 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 curwin->w_cursor = save_cursor;
15678
15679theend:
15680 vim_free(pat2);
15681 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015682 if (p_cpo == empty_option)
15683 p_cpo = save_cpo;
15684 else
15685 /* Darn, evaluating the {skip} expression changed the value. */
15686 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015687
15688 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689}
15690
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015691/*
15692 * "searchpos()" function
15693 */
15694 static void
15695f_searchpos(argvars, rettv)
15696 typval_T *argvars;
15697 typval_T *rettv;
15698{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015699 pos_T match_pos;
15700 int lnum = 0;
15701 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015702 int n;
15703 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015704
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015705 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015706 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015707
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015708 n = search_cmn(argvars, &match_pos, &flags);
15709 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015710 {
15711 lnum = match_pos.lnum;
15712 col = match_pos.col;
15713 }
15714
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015715 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15716 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015717 if (flags & SP_SUBPAT)
15718 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015719}
15720
15721
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722 static void
15723f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015724 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015727#ifdef FEAT_CLIENTSERVER
15728 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015729 char_u *server = get_tv_string_chk(&argvars[0]);
15730 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731
Bram Moolenaar0d660222005-01-07 21:51:51 +000015732 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015733 if (server == NULL || reply == NULL)
15734 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015735 if (check_restricted() || check_secure())
15736 return;
15737# ifdef FEAT_X11
15738 if (check_connection() == FAIL)
15739 return;
15740# endif
15741
15742 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015744 EMSG(_("E258: Unable to send to client"));
15745 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747 rettv->vval.v_number = 0;
15748#else
15749 rettv->vval.v_number = -1;
15750#endif
15751}
15752
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 static void
15754f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015757{
15758 char_u *r = NULL;
15759
15760#ifdef FEAT_CLIENTSERVER
15761# ifdef WIN32
15762 r = serverGetVimNames();
15763# else
15764 make_connection();
15765 if (X_DISPLAY != NULL)
15766 r = serverGetVimNames(X_DISPLAY);
15767# endif
15768#endif
15769 rettv->v_type = VAR_STRING;
15770 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771}
15772
15773/*
15774 * "setbufvar()" function
15775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015777f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015778 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015779 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780{
15781 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015784 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 char_u nbuf[NUMBUFLEN];
15786
15787 if (check_restricted() || check_secure())
15788 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015789 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15790 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015791 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 varp = &argvars[2];
15793
15794 if (buf != NULL && varname != NULL && varp != NULL)
15795 {
15796 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798
15799 if (*varname == '&')
15800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015801 long numval;
15802 char_u *strval;
15803 int error = FALSE;
15804
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015806 numval = get_tv_number_chk(varp, &error);
15807 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015808 if (!error && strval != NULL)
15809 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810 }
15811 else
15812 {
15813 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15814 if (bufvarname != NULL)
15815 {
15816 STRCPY(bufvarname, "b:");
15817 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015818 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 vim_free(bufvarname);
15820 }
15821 }
15822
15823 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826}
15827
15828/*
15829 * "setcmdpos()" function
15830 */
15831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015832f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015833 typval_T *argvars;
15834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015836 int pos = (int)get_tv_number(&argvars[0]) - 1;
15837
15838 if (pos >= 0)
15839 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840}
15841
15842/*
15843 * "setline()" function
15844 */
15845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015846f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 typval_T *argvars;
15848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849{
15850 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015851 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015852 list_T *l = NULL;
15853 listitem_T *li = NULL;
15854 long added = 0;
15855 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015857 lnum = get_tv_lnum(&argvars[0]);
15858 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015860 l = argvars[1].vval.v_list;
15861 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015863 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015864 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015865
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015866 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015867 for (;;)
15868 {
15869 if (l != NULL)
15870 {
15871 /* list argument, get next string */
15872 if (li == NULL)
15873 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015874 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015875 li = li->li_next;
15876 }
15877
15878 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015879 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015880 break;
15881 if (lnum <= curbuf->b_ml.ml_line_count)
15882 {
15883 /* existing line, replace it */
15884 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15885 {
15886 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015887 if (lnum == curwin->w_cursor.lnum)
15888 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015889 rettv->vval.v_number = 0; /* OK */
15890 }
15891 }
15892 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15893 {
15894 /* lnum is one past the last line, append the line */
15895 ++added;
15896 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15897 rettv->vval.v_number = 0; /* OK */
15898 }
15899
15900 if (l == NULL) /* only one string argument */
15901 break;
15902 ++lnum;
15903 }
15904
15905 if (added > 0)
15906 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907}
15908
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015909static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15910
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015912 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015913 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015914 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015915set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015916 win_T *wp UNUSED;
15917 typval_T *list_arg UNUSED;
15918 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015919 typval_T *rettv;
15920{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015921#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015922 char_u *act;
15923 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015924#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015925
Bram Moolenaar2641f772005-03-25 21:58:17 +000015926 rettv->vval.v_number = -1;
15927
15928#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015929 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015930 EMSG(_(e_listreq));
15931 else
15932 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015933 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015934
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015935 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015936 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015937 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015938 if (act == NULL)
15939 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015940 if (*act == 'a' || *act == 'r')
15941 action = *act;
15942 }
15943
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015944 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015945 rettv->vval.v_number = 0;
15946 }
15947#endif
15948}
15949
15950/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015951 * "setloclist()" function
15952 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015953 static void
15954f_setloclist(argvars, rettv)
15955 typval_T *argvars;
15956 typval_T *rettv;
15957{
15958 win_T *win;
15959
15960 rettv->vval.v_number = -1;
15961
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015962 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015963 if (win != NULL)
15964 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15965}
15966
15967/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015968 * "setmatches()" function
15969 */
15970 static void
15971f_setmatches(argvars, rettv)
15972 typval_T *argvars;
15973 typval_T *rettv;
15974{
15975#ifdef FEAT_SEARCH_EXTRA
15976 list_T *l;
15977 listitem_T *li;
15978 dict_T *d;
15979
15980 rettv->vval.v_number = -1;
15981 if (argvars[0].v_type != VAR_LIST)
15982 {
15983 EMSG(_(e_listreq));
15984 return;
15985 }
15986 if ((l = argvars[0].vval.v_list) != NULL)
15987 {
15988
15989 /* To some extent make sure that we are dealing with a list from
15990 * "getmatches()". */
15991 li = l->lv_first;
15992 while (li != NULL)
15993 {
15994 if (li->li_tv.v_type != VAR_DICT
15995 || (d = li->li_tv.vval.v_dict) == NULL)
15996 {
15997 EMSG(_(e_invarg));
15998 return;
15999 }
16000 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16001 && dict_find(d, (char_u *)"pattern", -1) != NULL
16002 && dict_find(d, (char_u *)"priority", -1) != NULL
16003 && dict_find(d, (char_u *)"id", -1) != NULL))
16004 {
16005 EMSG(_(e_invarg));
16006 return;
16007 }
16008 li = li->li_next;
16009 }
16010
16011 clear_matches(curwin);
16012 li = l->lv_first;
16013 while (li != NULL)
16014 {
16015 d = li->li_tv.vval.v_dict;
16016 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16017 get_dict_string(d, (char_u *)"pattern", FALSE),
16018 (int)get_dict_number(d, (char_u *)"priority"),
16019 (int)get_dict_number(d, (char_u *)"id"));
16020 li = li->li_next;
16021 }
16022 rettv->vval.v_number = 0;
16023 }
16024#endif
16025}
16026
16027/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016028 * "setpos()" function
16029 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016030 static void
16031f_setpos(argvars, rettv)
16032 typval_T *argvars;
16033 typval_T *rettv;
16034{
16035 pos_T pos;
16036 int fnum;
16037 char_u *name;
16038
Bram Moolenaar08250432008-02-13 11:42:46 +000016039 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016040 name = get_tv_string_chk(argvars);
16041 if (name != NULL)
16042 {
16043 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16044 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016045 if (--pos.col < 0)
16046 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016047 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016048 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016049 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016050 if (fnum == curbuf->b_fnum)
16051 {
16052 curwin->w_cursor = pos;
16053 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016054 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016055 }
16056 else
16057 EMSG(_(e_invarg));
16058 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016059 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16060 {
16061 /* set mark */
16062 if (setmark_pos(name[1], &pos, fnum) == OK)
16063 rettv->vval.v_number = 0;
16064 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016065 else
16066 EMSG(_(e_invarg));
16067 }
16068 }
16069}
16070
16071/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016072 * "setqflist()" function
16073 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016074 static void
16075f_setqflist(argvars, rettv)
16076 typval_T *argvars;
16077 typval_T *rettv;
16078{
16079 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16080}
16081
16082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 * "setreg()" function
16084 */
16085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016086f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 typval_T *argvars;
16088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089{
16090 int regname;
16091 char_u *strregname;
16092 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016093 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094 int append;
16095 char_u yank_type;
16096 long block_len;
16097
16098 block_len = -1;
16099 yank_type = MAUTO;
16100 append = FALSE;
16101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016102 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016103 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016105 if (strregname == NULL)
16106 return; /* type error; errmsg already given */
16107 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 if (regname == 0 || regname == '@')
16109 regname = '"';
16110 else if (regname == '=')
16111 return;
16112
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016113 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016115 stropt = get_tv_string_chk(&argvars[2]);
16116 if (stropt == NULL)
16117 return; /* type error */
16118 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 switch (*stropt)
16120 {
16121 case 'a': case 'A': /* append */
16122 append = TRUE;
16123 break;
16124 case 'v': case 'c': /* character-wise selection */
16125 yank_type = MCHAR;
16126 break;
16127 case 'V': case 'l': /* line-wise selection */
16128 yank_type = MLINE;
16129 break;
16130#ifdef FEAT_VISUAL
16131 case 'b': case Ctrl_V: /* block-wise selection */
16132 yank_type = MBLOCK;
16133 if (VIM_ISDIGIT(stropt[1]))
16134 {
16135 ++stropt;
16136 block_len = getdigits(&stropt) - 1;
16137 --stropt;
16138 }
16139 break;
16140#endif
16141 }
16142 }
16143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 strval = get_tv_string_chk(&argvars[1]);
16145 if (strval != NULL)
16146 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016148 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149}
16150
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016151/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016152 * "settabvar()" function
16153 */
16154 static void
16155f_settabvar(argvars, rettv)
16156 typval_T *argvars;
16157 typval_T *rettv;
16158{
16159 tabpage_T *save_curtab;
16160 char_u *varname, *tabvarname;
16161 typval_T *varp;
16162 tabpage_T *tp;
16163
16164 rettv->vval.v_number = 0;
16165
16166 if (check_restricted() || check_secure())
16167 return;
16168
16169 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16170 varname = get_tv_string_chk(&argvars[1]);
16171 varp = &argvars[2];
16172
16173 if (tp != NULL && varname != NULL && varp != NULL)
16174 {
16175 save_curtab = curtab;
16176 goto_tabpage_tp(tp);
16177
16178 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16179 if (tabvarname != NULL)
16180 {
16181 STRCPY(tabvarname, "t:");
16182 STRCPY(tabvarname + 2, varname);
16183 set_var(tabvarname, varp, TRUE);
16184 vim_free(tabvarname);
16185 }
16186
16187 /* Restore current tabpage */
16188 if (valid_tabpage(save_curtab))
16189 goto_tabpage_tp(save_curtab);
16190 }
16191}
16192
16193/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016194 * "settabwinvar()" function
16195 */
16196 static void
16197f_settabwinvar(argvars, rettv)
16198 typval_T *argvars;
16199 typval_T *rettv;
16200{
16201 setwinvar(argvars, rettv, 1);
16202}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203
16204/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016205 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016208f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016209 typval_T *argvars;
16210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016212 setwinvar(argvars, rettv, 0);
16213}
16214
16215/*
16216 * "setwinvar()" and "settabwinvar()" functions
16217 */
16218 static void
16219setwinvar(argvars, rettv, off)
16220 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016221 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016222 int off;
16223{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 win_T *win;
16225#ifdef FEAT_WINDOWS
16226 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016227 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228#endif
16229 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016232 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233
16234 if (check_restricted() || check_secure())
16235 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016236
16237#ifdef FEAT_WINDOWS
16238 if (off == 1)
16239 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16240 else
16241 tp = curtab;
16242#endif
16243 win = find_win_by_nr(&argvars[off], tp);
16244 varname = get_tv_string_chk(&argvars[off + 1]);
16245 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246
16247 if (win != NULL && varname != NULL && varp != NULL)
16248 {
16249#ifdef FEAT_WINDOWS
16250 /* set curwin to be our win, temporarily */
16251 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016252 save_curtab = curtab;
16253 goto_tabpage_tp(tp);
16254 if (!win_valid(win))
16255 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 curwin = win;
16257 curbuf = curwin->w_buffer;
16258#endif
16259
16260 if (*varname == '&')
16261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 long numval;
16263 char_u *strval;
16264 int error = FALSE;
16265
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016267 numval = get_tv_number_chk(varp, &error);
16268 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016269 if (!error && strval != NULL)
16270 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 }
16272 else
16273 {
16274 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16275 if (winvarname != NULL)
16276 {
16277 STRCPY(winvarname, "w:");
16278 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016279 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280 vim_free(winvarname);
16281 }
16282 }
16283
16284#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016285 /* Restore current tabpage and window, if still valid (autocomands can
16286 * make them invalid). */
16287 if (valid_tabpage(save_curtab))
16288 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 if (win_valid(save_curwin))
16290 {
16291 curwin = save_curwin;
16292 curbuf = curwin->w_buffer;
16293 }
16294#endif
16295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296}
16297
16298/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016299 * "shellescape({string})" function
16300 */
16301 static void
16302f_shellescape(argvars, rettv)
16303 typval_T *argvars;
16304 typval_T *rettv;
16305{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016306 rettv->vval.v_string = vim_strsave_shellescape(
16307 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016308 rettv->v_type = VAR_STRING;
16309}
16310
16311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 */
16314 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016316 typval_T *argvars;
16317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016319 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320
Bram Moolenaar0d660222005-01-07 21:51:51 +000016321 p = get_tv_string(&argvars[0]);
16322 rettv->vval.v_string = vim_strsave(p);
16323 simplify_filename(rettv->vval.v_string); /* simplify in place */
16324 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325}
16326
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016327#ifdef FEAT_FLOAT
16328/*
16329 * "sin()" function
16330 */
16331 static void
16332f_sin(argvars, rettv)
16333 typval_T *argvars;
16334 typval_T *rettv;
16335{
16336 float_T f;
16337
16338 rettv->v_type = VAR_FLOAT;
16339 if (get_float_arg(argvars, &f) == OK)
16340 rettv->vval.v_float = sin(f);
16341 else
16342 rettv->vval.v_float = 0.0;
16343}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016344
16345/*
16346 * "sinh()" function
16347 */
16348 static void
16349f_sinh(argvars, rettv)
16350 typval_T *argvars;
16351 typval_T *rettv;
16352{
16353 float_T f;
16354
16355 rettv->v_type = VAR_FLOAT;
16356 if (get_float_arg(argvars, &f) == OK)
16357 rettv->vval.v_float = sinh(f);
16358 else
16359 rettv->vval.v_float = 0.0;
16360}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016361#endif
16362
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363static int
16364#ifdef __BORLANDC__
16365 _RTLENTRYF
16366#endif
16367 item_compare __ARGS((const void *s1, const void *s2));
16368static int
16369#ifdef __BORLANDC__
16370 _RTLENTRYF
16371#endif
16372 item_compare2 __ARGS((const void *s1, const void *s2));
16373
16374static int item_compare_ic;
16375static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016376static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016377static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016378#define ITEM_COMPARE_FAIL 999
16379
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383 static int
16384#ifdef __BORLANDC__
16385_RTLENTRYF
16386#endif
16387item_compare(s1, s2)
16388 const void *s1;
16389 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391 char_u *p1, *p2;
16392 char_u *tofree1, *tofree2;
16393 int res;
16394 char_u numbuf1[NUMBUFLEN];
16395 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016397 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16398 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016399 if (p1 == NULL)
16400 p1 = (char_u *)"";
16401 if (p2 == NULL)
16402 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016403 if (item_compare_ic)
16404 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406 res = STRCMP(p1, p2);
16407 vim_free(tofree1);
16408 vim_free(tofree2);
16409 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410}
16411
16412 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016413#ifdef __BORLANDC__
16414_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016416item_compare2(s1, s2)
16417 const void *s1;
16418 const void *s2;
16419{
16420 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016421 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016422 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016423 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016425 /* shortcut after failure in previous call; compare all items equal */
16426 if (item_compare_func_err)
16427 return 0;
16428
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016429 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16430 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016431 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16432 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433
16434 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016435 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016436 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16437 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016438 clear_tv(&argv[0]);
16439 clear_tv(&argv[1]);
16440
16441 if (res == FAIL)
16442 res = ITEM_COMPARE_FAIL;
16443 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016444 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16445 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016446 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447 clear_tv(&rettv);
16448 return res;
16449}
16450
16451/*
16452 * "sort({list})" function
16453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016456 typval_T *argvars;
16457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458{
Bram Moolenaar33570922005-01-25 22:26:29 +000016459 list_T *l;
16460 listitem_T *li;
16461 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 long len;
16463 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 if (argvars[0].v_type != VAR_LIST)
16466 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 else
16468 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016469 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016470 if (l == NULL || tv_check_lock(l->lv_lock,
16471 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016472 return;
16473 rettv->vval.v_list = l;
16474 rettv->v_type = VAR_LIST;
16475 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477 len = list_len(l);
16478 if (len <= 1)
16479 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481 item_compare_ic = FALSE;
16482 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016483 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484 if (argvars[1].v_type != VAR_UNKNOWN)
16485 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016486 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016488 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489 else
16490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016491 int error = FALSE;
16492
16493 i = get_tv_number_chk(&argvars[1], &error);
16494 if (error)
16495 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016496 if (i == 1)
16497 item_compare_ic = TRUE;
16498 else
16499 item_compare_func = get_tv_string(&argvars[1]);
16500 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016501
16502 if (argvars[2].v_type != VAR_UNKNOWN)
16503 {
16504 /* optional third argument: {dict} */
16505 if (argvars[2].v_type != VAR_DICT)
16506 {
16507 EMSG(_(e_dictreq));
16508 return;
16509 }
16510 item_compare_selfdict = argvars[2].vval.v_dict;
16511 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516 if (ptrs == NULL)
16517 return;
16518 i = 0;
16519 for (li = l->lv_first; li != NULL; li = li->li_next)
16520 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016522 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016523 /* test the compare function */
16524 if (item_compare_func != NULL
16525 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16526 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016527 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 {
16530 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016531 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532 item_compare_func == NULL ? item_compare : item_compare2);
16533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016534 if (!item_compare_func_err)
16535 {
16536 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016537 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016538 l->lv_len = 0;
16539 for (i = 0; i < len; ++i)
16540 list_append(l, ptrs[i]);
16541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016542 }
16543
16544 vim_free(ptrs);
16545 }
16546}
16547
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016548/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016549 * "soundfold({word})" function
16550 */
16551 static void
16552f_soundfold(argvars, rettv)
16553 typval_T *argvars;
16554 typval_T *rettv;
16555{
16556 char_u *s;
16557
16558 rettv->v_type = VAR_STRING;
16559 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016560#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016561 rettv->vval.v_string = eval_soundfold(s);
16562#else
16563 rettv->vval.v_string = vim_strsave(s);
16564#endif
16565}
16566
16567/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016568 * "spellbadword()" function
16569 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016570 static void
16571f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016572 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016573 typval_T *rettv;
16574{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016575 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016576 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016577 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016578
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016579 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016580 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016581
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016582#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016583 if (argvars[0].v_type == VAR_UNKNOWN)
16584 {
16585 /* Find the start and length of the badly spelled word. */
16586 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16587 if (len != 0)
16588 word = ml_get_cursor();
16589 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016590 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016591 {
16592 char_u *str = get_tv_string_chk(&argvars[0]);
16593 int capcol = -1;
16594
16595 if (str != NULL)
16596 {
16597 /* Check the argument for spelling. */
16598 while (*str != NUL)
16599 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016600 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016601 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016602 {
16603 word = str;
16604 break;
16605 }
16606 str += len;
16607 }
16608 }
16609 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016610#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016611
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016612 list_append_string(rettv->vval.v_list, word, len);
16613 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016614 attr == HLF_SPB ? "bad" :
16615 attr == HLF_SPR ? "rare" :
16616 attr == HLF_SPL ? "local" :
16617 attr == HLF_SPC ? "caps" :
16618 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016619}
16620
16621/*
16622 * "spellsuggest()" function
16623 */
16624 static void
16625f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016626 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016627 typval_T *rettv;
16628{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016629#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016630 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016631 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016632 int maxcount;
16633 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016634 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016635 listitem_T *li;
16636 int need_capital = FALSE;
16637#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016638
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016639 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016640 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016641
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016642#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016643 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016644 {
16645 str = get_tv_string(&argvars[0]);
16646 if (argvars[1].v_type != VAR_UNKNOWN)
16647 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016648 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016649 if (maxcount <= 0)
16650 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016651 if (argvars[2].v_type != VAR_UNKNOWN)
16652 {
16653 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16654 if (typeerr)
16655 return;
16656 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016657 }
16658 else
16659 maxcount = 25;
16660
Bram Moolenaar4770d092006-01-12 23:22:24 +000016661 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016662
16663 for (i = 0; i < ga.ga_len; ++i)
16664 {
16665 str = ((char_u **)ga.ga_data)[i];
16666
16667 li = listitem_alloc();
16668 if (li == NULL)
16669 vim_free(str);
16670 else
16671 {
16672 li->li_tv.v_type = VAR_STRING;
16673 li->li_tv.v_lock = 0;
16674 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016675 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016676 }
16677 }
16678 ga_clear(&ga);
16679 }
16680#endif
16681}
16682
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016684f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016685 typval_T *argvars;
16686 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016687{
16688 char_u *str;
16689 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016690 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691 regmatch_T regmatch;
16692 char_u patbuf[NUMBUFLEN];
16693 char_u *save_cpo;
16694 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016696 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016697 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016698
16699 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16700 save_cpo = p_cpo;
16701 p_cpo = (char_u *)"";
16702
16703 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016704 if (argvars[1].v_type != VAR_UNKNOWN)
16705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016706 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16707 if (pat == NULL)
16708 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016709 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016710 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016711 }
16712 if (pat == NULL || *pat == NUL)
16713 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016714
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016715 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016717 if (typeerr)
16718 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719
Bram Moolenaar0d660222005-01-07 21:51:51 +000016720 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16721 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016723 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016724 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016725 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016726 if (*str == NUL)
16727 match = FALSE; /* empty item at the end */
16728 else
16729 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016730 if (match)
16731 end = regmatch.startp[0];
16732 else
16733 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016734 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16735 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016736 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016737 if (list_append_string(rettv->vval.v_list, str,
16738 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016739 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016740 }
16741 if (!match)
16742 break;
16743 /* Advance to just after the match. */
16744 if (regmatch.endp[0] > str)
16745 col = 0;
16746 else
16747 {
16748 /* Don't get stuck at the same match. */
16749#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016750 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016751#else
16752 col = 1;
16753#endif
16754 }
16755 str = regmatch.endp[0];
16756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757
Bram Moolenaar0d660222005-01-07 21:51:51 +000016758 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760
Bram Moolenaar0d660222005-01-07 21:51:51 +000016761 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762}
16763
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016764#ifdef FEAT_FLOAT
16765/*
16766 * "sqrt()" function
16767 */
16768 static void
16769f_sqrt(argvars, rettv)
16770 typval_T *argvars;
16771 typval_T *rettv;
16772{
16773 float_T f;
16774
16775 rettv->v_type = VAR_FLOAT;
16776 if (get_float_arg(argvars, &f) == OK)
16777 rettv->vval.v_float = sqrt(f);
16778 else
16779 rettv->vval.v_float = 0.0;
16780}
16781
16782/*
16783 * "str2float()" function
16784 */
16785 static void
16786f_str2float(argvars, rettv)
16787 typval_T *argvars;
16788 typval_T *rettv;
16789{
16790 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16791
16792 if (*p == '+')
16793 p = skipwhite(p + 1);
16794 (void)string2float(p, &rettv->vval.v_float);
16795 rettv->v_type = VAR_FLOAT;
16796}
16797#endif
16798
Bram Moolenaar2c932302006-03-18 21:42:09 +000016799/*
16800 * "str2nr()" function
16801 */
16802 static void
16803f_str2nr(argvars, rettv)
16804 typval_T *argvars;
16805 typval_T *rettv;
16806{
16807 int base = 10;
16808 char_u *p;
16809 long n;
16810
16811 if (argvars[1].v_type != VAR_UNKNOWN)
16812 {
16813 base = get_tv_number(&argvars[1]);
16814 if (base != 8 && base != 10 && base != 16)
16815 {
16816 EMSG(_(e_invarg));
16817 return;
16818 }
16819 }
16820
16821 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016822 if (*p == '+')
16823 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016824 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16825 rettv->vval.v_number = n;
16826}
16827
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828#ifdef HAVE_STRFTIME
16829/*
16830 * "strftime({format}[, {time}])" function
16831 */
16832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016833f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016834 typval_T *argvars;
16835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836{
16837 char_u result_buf[256];
16838 struct tm *curtime;
16839 time_t seconds;
16840 char_u *p;
16841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016842 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016844 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016845 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 seconds = time(NULL);
16847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016848 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 curtime = localtime(&seconds);
16850 /* MSVC returns NULL for an invalid value of seconds. */
16851 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016852 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 else
16854 {
16855# ifdef FEAT_MBYTE
16856 vimconv_T conv;
16857 char_u *enc;
16858
16859 conv.vc_type = CONV_NONE;
16860 enc = enc_locale();
16861 convert_setup(&conv, p_enc, enc);
16862 if (conv.vc_type != CONV_NONE)
16863 p = string_convert(&conv, p, NULL);
16864# endif
16865 if (p != NULL)
16866 (void)strftime((char *)result_buf, sizeof(result_buf),
16867 (char *)p, curtime);
16868 else
16869 result_buf[0] = NUL;
16870
16871# ifdef FEAT_MBYTE
16872 if (conv.vc_type != CONV_NONE)
16873 vim_free(p);
16874 convert_setup(&conv, enc, p_enc);
16875 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016876 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 else
16878# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016879 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880
16881# ifdef FEAT_MBYTE
16882 /* Release conversion descriptors */
16883 convert_setup(&conv, NULL, NULL);
16884 vim_free(enc);
16885# endif
16886 }
16887}
16888#endif
16889
16890/*
16891 * "stridx()" function
16892 */
16893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016894f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016895 typval_T *argvars;
16896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897{
16898 char_u buf[NUMBUFLEN];
16899 char_u *needle;
16900 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016901 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016903 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016905 needle = get_tv_string_chk(&argvars[1]);
16906 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016907 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016908 if (needle == NULL || haystack == NULL)
16909 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910
Bram Moolenaar33570922005-01-25 22:26:29 +000016911 if (argvars[2].v_type != VAR_UNKNOWN)
16912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016913 int error = FALSE;
16914
16915 start_idx = get_tv_number_chk(&argvars[2], &error);
16916 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016917 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016918 if (start_idx >= 0)
16919 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016920 }
16921
16922 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16923 if (pos != NULL)
16924 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925}
16926
16927/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016928 * "string()" function
16929 */
16930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016932 typval_T *argvars;
16933 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016934{
16935 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016936 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016938 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016939 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016940 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016941 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016942 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943}
16944
16945/*
16946 * "strlen()" function
16947 */
16948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016949f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016950 typval_T *argvars;
16951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016953 rettv->vval.v_number = (varnumber_T)(STRLEN(
16954 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955}
16956
16957/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016958 * "strchars()" function
16959 */
16960 static void
16961f_strchars(argvars, rettv)
16962 typval_T *argvars;
16963 typval_T *rettv;
16964{
16965 char_u *s = get_tv_string(&argvars[0]);
16966#ifdef FEAT_MBYTE
16967 varnumber_T len = 0;
16968
16969 while (*s != NUL)
16970 {
16971 mb_cptr2char_adv(&s);
16972 ++len;
16973 }
16974 rettv->vval.v_number = len;
16975#else
16976 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16977#endif
16978}
16979
16980/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016981 * "strdisplaywidth()" function
16982 */
16983 static void
16984f_strdisplaywidth(argvars, rettv)
16985 typval_T *argvars;
16986 typval_T *rettv;
16987{
16988 char_u *s = get_tv_string(&argvars[0]);
16989 int col = 0;
16990
16991 if (argvars[1].v_type != VAR_UNKNOWN)
16992 col = get_tv_number(&argvars[1]);
16993
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016994 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016995}
16996
16997/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016998 * "strwidth()" function
16999 */
17000 static void
17001f_strwidth(argvars, rettv)
17002 typval_T *argvars;
17003 typval_T *rettv;
17004{
17005 char_u *s = get_tv_string(&argvars[0]);
17006
17007 rettv->vval.v_number = (varnumber_T)(
17008#ifdef FEAT_MBYTE
17009 mb_string2cells(s, -1)
17010#else
17011 STRLEN(s)
17012#endif
17013 );
17014}
17015
17016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 * "strpart()" function
17018 */
17019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017020f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 typval_T *argvars;
17022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023{
17024 char_u *p;
17025 int n;
17026 int len;
17027 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017028 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017030 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 slen = (int)STRLEN(p);
17032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033 n = get_tv_number_chk(&argvars[1], &error);
17034 if (error)
17035 len = 0;
17036 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017037 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 else
17039 len = slen - n; /* default len: all bytes that are available. */
17040
17041 /*
17042 * Only return the overlap between the specified part and the actual
17043 * string.
17044 */
17045 if (n < 0)
17046 {
17047 len += n;
17048 n = 0;
17049 }
17050 else if (n > slen)
17051 n = slen;
17052 if (len < 0)
17053 len = 0;
17054 else if (n + len > slen)
17055 len = slen - n;
17056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017057 rettv->v_type = VAR_STRING;
17058 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059}
17060
17061/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062 * "strridx()" function
17063 */
17064 static void
17065f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017066 typval_T *argvars;
17067 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068{
17069 char_u buf[NUMBUFLEN];
17070 char_u *needle;
17071 char_u *haystack;
17072 char_u *rest;
17073 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017074 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017076 needle = get_tv_string_chk(&argvars[1]);
17077 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078
17079 rettv->vval.v_number = -1;
17080 if (needle == NULL || haystack == NULL)
17081 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017082
17083 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017084 if (argvars[2].v_type != VAR_UNKNOWN)
17085 {
17086 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017087 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017088 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017089 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017090 }
17091 else
17092 end_idx = haystack_len;
17093
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017095 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017097 lastmatch = haystack + end_idx;
17098 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017100 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 for (rest = haystack; *rest != '\0'; ++rest)
17102 {
17103 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017104 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105 break;
17106 lastmatch = rest;
17107 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017108 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109
17110 if (lastmatch == NULL)
17111 rettv->vval.v_number = -1;
17112 else
17113 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17114}
17115
17116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 * "strtrans()" function
17118 */
17119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017121 typval_T *argvars;
17122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017124 rettv->v_type = VAR_STRING;
17125 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126}
17127
17128/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129 * "submatch()" function
17130 */
17131 static void
17132f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 typval_T *argvars;
17134 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135{
17136 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 rettv->vval.v_string =
17138 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139}
17140
17141/*
17142 * "substitute()" function
17143 */
17144 static void
17145f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017146 typval_T *argvars;
17147 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017148{
17149 char_u patbuf[NUMBUFLEN];
17150 char_u subbuf[NUMBUFLEN];
17151 char_u flagsbuf[NUMBUFLEN];
17152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017153 char_u *str = get_tv_string_chk(&argvars[0]);
17154 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17155 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17156 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17157
Bram Moolenaar0d660222005-01-07 21:51:51 +000017158 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017159 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17160 rettv->vval.v_string = NULL;
17161 else
17162 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163}
17164
17165/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017166 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172{
17173 int id = 0;
17174#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017175 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 long col;
17177 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017178 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017180 lnum = get_tv_lnum(argvars); /* -1 on type error */
17181 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17182 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017184 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017185 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017186 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187#endif
17188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017189 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190}
17191
17192/*
17193 * "synIDattr(id, what [, mode])" function
17194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017196f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017197 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199{
17200 char_u *p = NULL;
17201#ifdef FEAT_SYN_HL
17202 int id;
17203 char_u *what;
17204 char_u *mode;
17205 char_u modebuf[NUMBUFLEN];
17206 int modec;
17207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017208 id = get_tv_number(&argvars[0]);
17209 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017210 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017212 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017214 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 modec = 0; /* replace invalid with current */
17216 }
17217 else
17218 {
17219#ifdef FEAT_GUI
17220 if (gui.in_use)
17221 modec = 'g';
17222 else
17223#endif
17224 if (t_colors > 1)
17225 modec = 'c';
17226 else
17227 modec = 't';
17228 }
17229
17230
17231 switch (TOLOWER_ASC(what[0]))
17232 {
17233 case 'b':
17234 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17235 p = highlight_color(id, what, modec);
17236 else /* bold */
17237 p = highlight_has_attr(id, HL_BOLD, modec);
17238 break;
17239
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017240 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 p = highlight_color(id, what, modec);
17242 break;
17243
17244 case 'i':
17245 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17246 p = highlight_has_attr(id, HL_INVERSE, modec);
17247 else /* italic */
17248 p = highlight_has_attr(id, HL_ITALIC, modec);
17249 break;
17250
17251 case 'n': /* name */
17252 p = get_highlight_name(NULL, id - 1);
17253 break;
17254
17255 case 'r': /* reverse */
17256 p = highlight_has_attr(id, HL_INVERSE, modec);
17257 break;
17258
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017259 case 's':
17260 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17261 p = highlight_color(id, what, modec);
17262 else /* standout */
17263 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 break;
17265
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017266 case 'u':
17267 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17268 /* underline */
17269 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17270 else
17271 /* undercurl */
17272 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 break;
17274 }
17275
17276 if (p != NULL)
17277 p = vim_strsave(p);
17278#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 rettv->v_type = VAR_STRING;
17280 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281}
17282
17283/*
17284 * "synIDtrans(id)" function
17285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017287f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017288 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290{
17291 int id;
17292
17293#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017294 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295
17296 if (id > 0)
17297 id = syn_get_final_id(id);
17298 else
17299#endif
17300 id = 0;
17301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303}
17304
17305/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017306 * "synconcealed(lnum, col)" function
17307 */
17308 static void
17309f_synconcealed(argvars, rettv)
17310 typval_T *argvars UNUSED;
17311 typval_T *rettv;
17312{
17313#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17314 long lnum;
17315 long col;
17316 int syntax_flags = 0;
17317 int cchar;
17318 int matchid = 0;
17319 char_u str[NUMBUFLEN];
17320#endif
17321
17322 rettv->v_type = VAR_LIST;
17323 rettv->vval.v_list = NULL;
17324
17325#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17326 lnum = get_tv_lnum(argvars); /* -1 on type error */
17327 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17328
17329 vim_memset(str, NUL, sizeof(str));
17330
17331 if (rettv_list_alloc(rettv) != FAIL)
17332 {
17333 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17334 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17335 && curwin->w_p_cole > 0)
17336 {
17337 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17338 syntax_flags = get_syntax_info(&matchid);
17339
17340 /* get the conceal character */
17341 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17342 {
17343 cchar = syn_get_sub_char();
17344 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17345 cchar = lcs_conceal;
17346 if (cchar != NUL)
17347 {
17348# ifdef FEAT_MBYTE
17349 if (has_mbyte)
17350 (*mb_char2bytes)(cchar, str);
17351 else
17352# endif
17353 str[0] = cchar;
17354 }
17355 }
17356 }
17357
17358 list_append_number(rettv->vval.v_list,
17359 (syntax_flags & HL_CONCEAL) != 0);
17360 /* -1 to auto-determine strlen */
17361 list_append_string(rettv->vval.v_list, str, -1);
17362 list_append_number(rettv->vval.v_list, matchid);
17363 }
17364#endif
17365}
17366
17367/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017368 * "synstack(lnum, col)" function
17369 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017370 static void
17371f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017372 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017373 typval_T *rettv;
17374{
17375#ifdef FEAT_SYN_HL
17376 long lnum;
17377 long col;
17378 int i;
17379 int id;
17380#endif
17381
17382 rettv->v_type = VAR_LIST;
17383 rettv->vval.v_list = NULL;
17384
17385#ifdef FEAT_SYN_HL
17386 lnum = get_tv_lnum(argvars); /* -1 on type error */
17387 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17388
17389 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017390 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017391 && rettv_list_alloc(rettv) != FAIL)
17392 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017393 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017394 for (i = 0; ; ++i)
17395 {
17396 id = syn_get_stack_item(i);
17397 if (id < 0)
17398 break;
17399 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17400 break;
17401 }
17402 }
17403#endif
17404}
17405
17406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 * "system()" function
17408 */
17409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017411 typval_T *argvars;
17412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017414 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017416 char_u *infile = NULL;
17417 char_u buf[NUMBUFLEN];
17418 int err = FALSE;
17419 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017421 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017422 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017423
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017424 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017425 {
17426 /*
17427 * Write the string to a temp file, to be used for input of the shell
17428 * command.
17429 */
17430 if ((infile = vim_tempname('i')) == NULL)
17431 {
17432 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017433 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017434 }
17435
17436 fd = mch_fopen((char *)infile, WRITEBIN);
17437 if (fd == NULL)
17438 {
17439 EMSG2(_(e_notopen), infile);
17440 goto done;
17441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017442 p = get_tv_string_buf_chk(&argvars[1], buf);
17443 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017444 {
17445 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017446 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017447 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017448 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17449 err = TRUE;
17450 if (fclose(fd) != 0)
17451 err = TRUE;
17452 if (err)
17453 {
17454 EMSG(_("E677: Error writing temp file"));
17455 goto done;
17456 }
17457 }
17458
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017459 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17460 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017461
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462#ifdef USE_CR
17463 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017464 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 {
17466 char_u *s;
17467
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017468 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469 {
17470 if (*s == CAR)
17471 *s = NL;
17472 }
17473 }
17474#else
17475# ifdef USE_CRNL
17476 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017477 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 {
17479 char_u *s, *d;
17480
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017481 d = res;
17482 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 {
17484 if (s[0] == CAR && s[1] == NL)
17485 ++s;
17486 *d++ = *s;
17487 }
17488 *d = NUL;
17489 }
17490# endif
17491#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017492
17493done:
17494 if (infile != NULL)
17495 {
17496 mch_remove(infile);
17497 vim_free(infile);
17498 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017499 rettv->v_type = VAR_STRING;
17500 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501}
17502
17503/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017504 * "tabpagebuflist()" function
17505 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017506 static void
17507f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017508 typval_T *argvars UNUSED;
17509 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017510{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017511#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017512 tabpage_T *tp;
17513 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017514
17515 if (argvars[0].v_type == VAR_UNKNOWN)
17516 wp = firstwin;
17517 else
17518 {
17519 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17520 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017521 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017522 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017523 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017524 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017525 for (; wp != NULL; wp = wp->w_next)
17526 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017527 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017528 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017529 }
17530#endif
17531}
17532
17533
17534/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017535 * "tabpagenr()" function
17536 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017537 static void
17538f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017539 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017540 typval_T *rettv;
17541{
17542 int nr = 1;
17543#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017544 char_u *arg;
17545
17546 if (argvars[0].v_type != VAR_UNKNOWN)
17547 {
17548 arg = get_tv_string_chk(&argvars[0]);
17549 nr = 0;
17550 if (arg != NULL)
17551 {
17552 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017553 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017554 else
17555 EMSG2(_(e_invexpr2), arg);
17556 }
17557 }
17558 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017559 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017560#endif
17561 rettv->vval.v_number = nr;
17562}
17563
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017564
17565#ifdef FEAT_WINDOWS
17566static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17567
17568/*
17569 * Common code for tabpagewinnr() and winnr().
17570 */
17571 static int
17572get_winnr(tp, argvar)
17573 tabpage_T *tp;
17574 typval_T *argvar;
17575{
17576 win_T *twin;
17577 int nr = 1;
17578 win_T *wp;
17579 char_u *arg;
17580
17581 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17582 if (argvar->v_type != VAR_UNKNOWN)
17583 {
17584 arg = get_tv_string_chk(argvar);
17585 if (arg == NULL)
17586 nr = 0; /* type error; errmsg already given */
17587 else if (STRCMP(arg, "$") == 0)
17588 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17589 else if (STRCMP(arg, "#") == 0)
17590 {
17591 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17592 if (twin == NULL)
17593 nr = 0;
17594 }
17595 else
17596 {
17597 EMSG2(_(e_invexpr2), arg);
17598 nr = 0;
17599 }
17600 }
17601
17602 if (nr > 0)
17603 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17604 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017605 {
17606 if (wp == NULL)
17607 {
17608 /* didn't find it in this tabpage */
17609 nr = 0;
17610 break;
17611 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017612 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017613 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017614 return nr;
17615}
17616#endif
17617
17618/*
17619 * "tabpagewinnr()" function
17620 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017621 static void
17622f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017623 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017624 typval_T *rettv;
17625{
17626 int nr = 1;
17627#ifdef FEAT_WINDOWS
17628 tabpage_T *tp;
17629
17630 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17631 if (tp == NULL)
17632 nr = 0;
17633 else
17634 nr = get_winnr(tp, &argvars[1]);
17635#endif
17636 rettv->vval.v_number = nr;
17637}
17638
17639
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017640/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017641 * "tagfiles()" function
17642 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017643 static void
17644f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017645 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017646 typval_T *rettv;
17647{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017648 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017649 tagname_T tn;
17650 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017651
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017652 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017653 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017654 fname = alloc(MAXPATHL);
17655 if (fname == NULL)
17656 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017657
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017658 for (first = TRUE; ; first = FALSE)
17659 if (get_tagfname(&tn, first, fname) == FAIL
17660 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017661 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017662 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017663 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017664}
17665
17666/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017667 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017668 */
17669 static void
17670f_taglist(argvars, rettv)
17671 typval_T *argvars;
17672 typval_T *rettv;
17673{
17674 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017675
17676 tag_pattern = get_tv_string(&argvars[0]);
17677
17678 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017679 if (*tag_pattern == NUL)
17680 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017681
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017682 if (rettv_list_alloc(rettv) == OK)
17683 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017684}
17685
17686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 * "tempname()" function
17688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017690f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017691 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693{
17694 static int x = 'A';
17695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017696 rettv->v_type = VAR_STRING;
17697 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698
17699 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17700 * names. Skip 'I' and 'O', they are used for shell redirection. */
17701 do
17702 {
17703 if (x == 'Z')
17704 x = '0';
17705 else if (x == '9')
17706 x = 'A';
17707 else
17708 {
17709#ifdef EBCDIC
17710 if (x == 'I')
17711 x = 'J';
17712 else if (x == 'R')
17713 x = 'S';
17714 else
17715#endif
17716 ++x;
17717 }
17718 } while (x == 'I' || x == 'O');
17719}
17720
17721/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017722 * "test(list)" function: Just checking the walls...
17723 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017724 static void
17725f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017726 typval_T *argvars UNUSED;
17727 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017728{
17729 /* Used for unit testing. Change the code below to your liking. */
17730#if 0
17731 listitem_T *li;
17732 list_T *l;
17733 char_u *bad, *good;
17734
17735 if (argvars[0].v_type != VAR_LIST)
17736 return;
17737 l = argvars[0].vval.v_list;
17738 if (l == NULL)
17739 return;
17740 li = l->lv_first;
17741 if (li == NULL)
17742 return;
17743 bad = get_tv_string(&li->li_tv);
17744 li = li->li_next;
17745 if (li == NULL)
17746 return;
17747 good = get_tv_string(&li->li_tv);
17748 rettv->vval.v_number = test_edit_score(bad, good);
17749#endif
17750}
17751
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017752#ifdef FEAT_FLOAT
17753/*
17754 * "tan()" function
17755 */
17756 static void
17757f_tan(argvars, rettv)
17758 typval_T *argvars;
17759 typval_T *rettv;
17760{
17761 float_T f;
17762
17763 rettv->v_type = VAR_FLOAT;
17764 if (get_float_arg(argvars, &f) == OK)
17765 rettv->vval.v_float = tan(f);
17766 else
17767 rettv->vval.v_float = 0.0;
17768}
17769
17770/*
17771 * "tanh()" function
17772 */
17773 static void
17774f_tanh(argvars, rettv)
17775 typval_T *argvars;
17776 typval_T *rettv;
17777{
17778 float_T f;
17779
17780 rettv->v_type = VAR_FLOAT;
17781 if (get_float_arg(argvars, &f) == OK)
17782 rettv->vval.v_float = tanh(f);
17783 else
17784 rettv->vval.v_float = 0.0;
17785}
17786#endif
17787
Bram Moolenaard52d9742005-08-21 22:20:28 +000017788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 * "tolower(string)" function
17790 */
17791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017793 typval_T *argvars;
17794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795{
17796 char_u *p;
17797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017798 p = vim_strsave(get_tv_string(&argvars[0]));
17799 rettv->v_type = VAR_STRING;
17800 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801
17802 if (p != NULL)
17803 while (*p != NUL)
17804 {
17805#ifdef FEAT_MBYTE
17806 int l;
17807
17808 if (enc_utf8)
17809 {
17810 int c, lc;
17811
17812 c = utf_ptr2char(p);
17813 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017814 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815 /* TODO: reallocate string when byte count changes. */
17816 if (utf_char2len(lc) == l)
17817 utf_char2bytes(lc, p);
17818 p += l;
17819 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017820 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821 p += l; /* skip multi-byte character */
17822 else
17823#endif
17824 {
17825 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17826 ++p;
17827 }
17828 }
17829}
17830
17831/*
17832 * "toupper(string)" function
17833 */
17834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017835f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017836 typval_T *argvars;
17837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017840 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841}
17842
17843/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017844 * "tr(string, fromstr, tostr)" function
17845 */
17846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017847f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017848 typval_T *argvars;
17849 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017850{
17851 char_u *instr;
17852 char_u *fromstr;
17853 char_u *tostr;
17854 char_u *p;
17855#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017856 int inlen;
17857 int fromlen;
17858 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017859 int idx;
17860 char_u *cpstr;
17861 int cplen;
17862 int first = TRUE;
17863#endif
17864 char_u buf[NUMBUFLEN];
17865 char_u buf2[NUMBUFLEN];
17866 garray_T ga;
17867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017868 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017869 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17870 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017871
17872 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017873 rettv->v_type = VAR_STRING;
17874 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017875 if (fromstr == NULL || tostr == NULL)
17876 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017877 ga_init2(&ga, (int)sizeof(char), 80);
17878
17879#ifdef FEAT_MBYTE
17880 if (!has_mbyte)
17881#endif
17882 /* not multi-byte: fromstr and tostr must be the same length */
17883 if (STRLEN(fromstr) != STRLEN(tostr))
17884 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017885#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017886error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017887#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017888 EMSG2(_(e_invarg2), fromstr);
17889 ga_clear(&ga);
17890 return;
17891 }
17892
17893 /* fromstr and tostr have to contain the same number of chars */
17894 while (*instr != NUL)
17895 {
17896#ifdef FEAT_MBYTE
17897 if (has_mbyte)
17898 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017899 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017900 cpstr = instr;
17901 cplen = inlen;
17902 idx = 0;
17903 for (p = fromstr; *p != NUL; p += fromlen)
17904 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017905 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017906 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17907 {
17908 for (p = tostr; *p != NUL; p += tolen)
17909 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017910 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017911 if (idx-- == 0)
17912 {
17913 cplen = tolen;
17914 cpstr = p;
17915 break;
17916 }
17917 }
17918 if (*p == NUL) /* tostr is shorter than fromstr */
17919 goto error;
17920 break;
17921 }
17922 ++idx;
17923 }
17924
17925 if (first && cpstr == instr)
17926 {
17927 /* Check that fromstr and tostr have the same number of
17928 * (multi-byte) characters. Done only once when a character
17929 * of instr doesn't appear in fromstr. */
17930 first = FALSE;
17931 for (p = tostr; *p != NUL; p += tolen)
17932 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017933 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017934 --idx;
17935 }
17936 if (idx != 0)
17937 goto error;
17938 }
17939
17940 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017941 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017942 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017943
17944 instr += inlen;
17945 }
17946 else
17947#endif
17948 {
17949 /* When not using multi-byte chars we can do it faster. */
17950 p = vim_strchr(fromstr, *instr);
17951 if (p != NULL)
17952 ga_append(&ga, tostr[p - fromstr]);
17953 else
17954 ga_append(&ga, *instr);
17955 ++instr;
17956 }
17957 }
17958
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017959 /* add a terminating NUL */
17960 ga_grow(&ga, 1);
17961 ga_append(&ga, NUL);
17962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017963 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017964}
17965
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017966#ifdef FEAT_FLOAT
17967/*
17968 * "trunc({float})" function
17969 */
17970 static void
17971f_trunc(argvars, rettv)
17972 typval_T *argvars;
17973 typval_T *rettv;
17974{
17975 float_T f;
17976
17977 rettv->v_type = VAR_FLOAT;
17978 if (get_float_arg(argvars, &f) == OK)
17979 /* trunc() is not in C90, use floor() or ceil() instead. */
17980 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17981 else
17982 rettv->vval.v_float = 0.0;
17983}
17984#endif
17985
Bram Moolenaar8299df92004-07-10 09:47:34 +000017986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987 * "type(expr)" function
17988 */
17989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017990f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017991 typval_T *argvars;
17992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017994 int n;
17995
17996 switch (argvars[0].v_type)
17997 {
17998 case VAR_NUMBER: n = 0; break;
17999 case VAR_STRING: n = 1; break;
18000 case VAR_FUNC: n = 2; break;
18001 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018002 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018003#ifdef FEAT_FLOAT
18004 case VAR_FLOAT: n = 5; break;
18005#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018006 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18007 }
18008 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009}
18010
18011/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018012 * "undofile(name)" function
18013 */
18014 static void
18015f_undofile(argvars, rettv)
18016 typval_T *argvars;
18017 typval_T *rettv;
18018{
18019 rettv->v_type = VAR_STRING;
18020#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018021 {
18022 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18023
18024 if (ffname != NULL)
18025 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18026 vim_free(ffname);
18027 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018028#else
18029 rettv->vval.v_string = NULL;
18030#endif
18031}
18032
18033/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018034 * "undotree()" function
18035 */
18036 static void
18037f_undotree(argvars, rettv)
18038 typval_T *argvars UNUSED;
18039 typval_T *rettv;
18040{
18041 if (rettv_dict_alloc(rettv) == OK)
18042 {
18043 dict_T *dict = rettv->vval.v_dict;
18044 list_T *list;
18045
Bram Moolenaar730cde92010-06-27 05:18:54 +020018046 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018047 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018048 dict_add_nr_str(dict, "save_last",
18049 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018050 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18051 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018052 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018053
18054 list = list_alloc();
18055 if (list != NULL)
18056 {
18057 u_eval_tree(curbuf->b_u_oldhead, list);
18058 dict_add_list(dict, "entries", list);
18059 }
18060 }
18061}
18062
18063/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018064 * "values(dict)" function
18065 */
18066 static void
18067f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018068 typval_T *argvars;
18069 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018070{
18071 dict_list(argvars, rettv, 1);
18072}
18073
18074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 * "virtcol(string)" function
18076 */
18077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018078f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018079 typval_T *argvars;
18080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081{
18082 colnr_T vcol = 0;
18083 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018084 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018086 fp = var2fpos(&argvars[0], FALSE, &fnum);
18087 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18088 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 {
18090 getvvcol(curwin, fp, NULL, NULL, &vcol);
18091 ++vcol;
18092 }
18093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018094 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095}
18096
18097/*
18098 * "visualmode()" function
18099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018101f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018102 typval_T *argvars UNUSED;
18103 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104{
18105#ifdef FEAT_VISUAL
18106 char_u str[2];
18107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018108 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 str[0] = curbuf->b_visual_mode_eval;
18110 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018111 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112
18113 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018114 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116#endif
18117}
18118
18119/*
18120 * "winbufnr(nr)" function
18121 */
18122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018123f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018124 typval_T *argvars;
18125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126{
18127 win_T *wp;
18128
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018129 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134}
18135
18136/*
18137 * "wincol()" function
18138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018140f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143{
18144 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018145 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146}
18147
18148/*
18149 * "winheight(nr)" function
18150 */
18151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018152f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018153 typval_T *argvars;
18154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155{
18156 win_T *wp;
18157
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018158 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018160 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018162 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163}
18164
18165/*
18166 * "winline()" function
18167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018169f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172{
18173 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018174 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175}
18176
18177/*
18178 * "winnr()" function
18179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018181f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184{
18185 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018186
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018188 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018190 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191}
18192
18193/*
18194 * "winrestcmd()" function
18195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018197f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018198 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200{
18201#ifdef FEAT_WINDOWS
18202 win_T *wp;
18203 int winnr = 1;
18204 garray_T ga;
18205 char_u buf[50];
18206
18207 ga_init2(&ga, (int)sizeof(char), 70);
18208 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18209 {
18210 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18211 ga_concat(&ga, buf);
18212# ifdef FEAT_VERTSPLIT
18213 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18214 ga_concat(&ga, buf);
18215# endif
18216 ++winnr;
18217 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018218 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018222 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018224 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225}
18226
18227/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018228 * "winrestview()" function
18229 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018230 static void
18231f_winrestview(argvars, rettv)
18232 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018233 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018234{
18235 dict_T *dict;
18236
18237 if (argvars[0].v_type != VAR_DICT
18238 || (dict = argvars[0].vval.v_dict) == NULL)
18239 EMSG(_(e_invarg));
18240 else
18241 {
18242 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18243 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18244#ifdef FEAT_VIRTUALEDIT
18245 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18246#endif
18247 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018248 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018249
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018250 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018251#ifdef FEAT_DIFF
18252 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18253#endif
18254 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18255 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18256
18257 check_cursor();
18258 changed_cline_bef_curs();
18259 invalidate_botline();
18260 redraw_later(VALID);
18261
18262 if (curwin->w_topline == 0)
18263 curwin->w_topline = 1;
18264 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18265 curwin->w_topline = curbuf->b_ml.ml_line_count;
18266#ifdef FEAT_DIFF
18267 check_topfill(curwin, TRUE);
18268#endif
18269 }
18270}
18271
18272/*
18273 * "winsaveview()" function
18274 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018275 static void
18276f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018277 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018278 typval_T *rettv;
18279{
18280 dict_T *dict;
18281
Bram Moolenaara800b422010-06-27 01:15:55 +020018282 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018283 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018284 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018285
18286 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18287 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18288#ifdef FEAT_VIRTUALEDIT
18289 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18290#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018291 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018292 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18293
18294 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18295#ifdef FEAT_DIFF
18296 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18297#endif
18298 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18299 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18300}
18301
18302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 * "winwidth(nr)" function
18304 */
18305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 typval_T *argvars;
18308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309{
18310 win_T *wp;
18311
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018312 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018314 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 else
18316#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018317 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018319 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320#endif
18321}
18322
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018324 * "writefile()" function
18325 */
18326 static void
18327f_writefile(argvars, rettv)
18328 typval_T *argvars;
18329 typval_T *rettv;
18330{
18331 int binary = FALSE;
18332 char_u *fname;
18333 FILE *fd;
18334 listitem_T *li;
18335 char_u *s;
18336 int ret = 0;
18337 int c;
18338
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018339 if (check_restricted() || check_secure())
18340 return;
18341
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018342 if (argvars[0].v_type != VAR_LIST)
18343 {
18344 EMSG2(_(e_listarg), "writefile()");
18345 return;
18346 }
18347 if (argvars[0].vval.v_list == NULL)
18348 return;
18349
18350 if (argvars[2].v_type != VAR_UNKNOWN
18351 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18352 binary = TRUE;
18353
18354 /* Always open the file in binary mode, library functions have a mind of
18355 * their own about CR-LF conversion. */
18356 fname = get_tv_string(&argvars[1]);
18357 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18358 {
18359 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18360 ret = -1;
18361 }
18362 else
18363 {
18364 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18365 li = li->li_next)
18366 {
18367 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18368 {
18369 if (*s == '\n')
18370 c = putc(NUL, fd);
18371 else
18372 c = putc(*s, fd);
18373 if (c == EOF)
18374 {
18375 ret = -1;
18376 break;
18377 }
18378 }
18379 if (!binary || li->li_next != NULL)
18380 if (putc('\n', fd) == EOF)
18381 {
18382 ret = -1;
18383 break;
18384 }
18385 if (ret < 0)
18386 {
18387 EMSG(_(e_write));
18388 break;
18389 }
18390 }
18391 fclose(fd);
18392 }
18393
18394 rettv->vval.v_number = ret;
18395}
18396
18397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018399 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 */
18401 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018402var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018403 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018404 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018405 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018407 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018409 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410
Bram Moolenaara5525202006-03-02 22:52:09 +000018411 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018412 if (varp->v_type == VAR_LIST)
18413 {
18414 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018415 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018416 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018417 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018418
18419 l = varp->vval.v_list;
18420 if (l == NULL)
18421 return NULL;
18422
18423 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018424 pos.lnum = list_find_nr(l, 0L, &error);
18425 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018426 return NULL; /* invalid line number */
18427
18428 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018429 pos.col = list_find_nr(l, 1L, &error);
18430 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018431 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018432 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018433
18434 /* We accept "$" for the column number: last column. */
18435 li = list_find(l, 1L);
18436 if (li != NULL && li->li_tv.v_type == VAR_STRING
18437 && li->li_tv.vval.v_string != NULL
18438 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18439 pos.col = len + 1;
18440
Bram Moolenaara5525202006-03-02 22:52:09 +000018441 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018442 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018443 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018444 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018445
Bram Moolenaara5525202006-03-02 22:52:09 +000018446#ifdef FEAT_VIRTUALEDIT
18447 /* Get the virtual offset. Defaults to zero. */
18448 pos.coladd = list_find_nr(l, 2L, &error);
18449 if (error)
18450 pos.coladd = 0;
18451#endif
18452
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018453 return &pos;
18454 }
18455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018456 name = get_tv_string_chk(varp);
18457 if (name == NULL)
18458 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018459 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018461#ifdef FEAT_VISUAL
18462 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18463 {
18464 if (VIsual_active)
18465 return &VIsual;
18466 return &curwin->w_cursor;
18467 }
18468#endif
18469 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018471 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18473 return NULL;
18474 return pp;
18475 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018476
18477#ifdef FEAT_VIRTUALEDIT
18478 pos.coladd = 0;
18479#endif
18480
Bram Moolenaar477933c2007-07-17 14:32:23 +000018481 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018482 {
18483 pos.col = 0;
18484 if (name[1] == '0') /* "w0": first visible line */
18485 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018486 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018487 pos.lnum = curwin->w_topline;
18488 return &pos;
18489 }
18490 else if (name[1] == '$') /* "w$": last visible line */
18491 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018492 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018493 pos.lnum = curwin->w_botline - 1;
18494 return &pos;
18495 }
18496 }
18497 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018499 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500 {
18501 pos.lnum = curbuf->b_ml.ml_line_count;
18502 pos.col = 0;
18503 }
18504 else
18505 {
18506 pos.lnum = curwin->w_cursor.lnum;
18507 pos.col = (colnr_T)STRLEN(ml_get_curline());
18508 }
18509 return &pos;
18510 }
18511 return NULL;
18512}
18513
18514/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018515 * Convert list in "arg" into a position and optional file number.
18516 * When "fnump" is NULL there is no file number, only 3 items.
18517 * Note that the column is passed on as-is, the caller may want to decrement
18518 * it to use 1 for the first column.
18519 * Return FAIL when conversion is not possible, doesn't check the position for
18520 * validity.
18521 */
18522 static int
18523list2fpos(arg, posp, fnump)
18524 typval_T *arg;
18525 pos_T *posp;
18526 int *fnump;
18527{
18528 list_T *l = arg->vval.v_list;
18529 long i = 0;
18530 long n;
18531
Bram Moolenaarbde35262006-07-23 20:12:24 +000018532 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18533 * when "fnump" isn't NULL and "coladd" is optional. */
18534 if (arg->v_type != VAR_LIST
18535 || l == NULL
18536 || l->lv_len < (fnump == NULL ? 2 : 3)
18537 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018538 return FAIL;
18539
18540 if (fnump != NULL)
18541 {
18542 n = list_find_nr(l, i++, NULL); /* fnum */
18543 if (n < 0)
18544 return FAIL;
18545 if (n == 0)
18546 n = curbuf->b_fnum; /* current buffer */
18547 *fnump = n;
18548 }
18549
18550 n = list_find_nr(l, i++, NULL); /* lnum */
18551 if (n < 0)
18552 return FAIL;
18553 posp->lnum = n;
18554
18555 n = list_find_nr(l, i++, NULL); /* col */
18556 if (n < 0)
18557 return FAIL;
18558 posp->col = n;
18559
18560#ifdef FEAT_VIRTUALEDIT
18561 n = list_find_nr(l, i, NULL);
18562 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018563 posp->coladd = 0;
18564 else
18565 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018566#endif
18567
18568 return OK;
18569}
18570
18571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 * Get the length of an environment variable name.
18573 * Advance "arg" to the first character after the name.
18574 * Return 0 for error.
18575 */
18576 static int
18577get_env_len(arg)
18578 char_u **arg;
18579{
18580 char_u *p;
18581 int len;
18582
18583 for (p = *arg; vim_isIDc(*p); ++p)
18584 ;
18585 if (p == *arg) /* no name found */
18586 return 0;
18587
18588 len = (int)(p - *arg);
18589 *arg = p;
18590 return len;
18591}
18592
18593/*
18594 * Get the length of the name of a function or internal variable.
18595 * "arg" is advanced to the first non-white character after the name.
18596 * Return 0 if something is wrong.
18597 */
18598 static int
18599get_id_len(arg)
18600 char_u **arg;
18601{
18602 char_u *p;
18603 int len;
18604
18605 /* Find the end of the name. */
18606 for (p = *arg; eval_isnamec(*p); ++p)
18607 ;
18608 if (p == *arg) /* no name found */
18609 return 0;
18610
18611 len = (int)(p - *arg);
18612 *arg = skipwhite(p);
18613
18614 return len;
18615}
18616
18617/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018618 * Get the length of the name of a variable or function.
18619 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018621 * Return -1 if curly braces expansion failed.
18622 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 * If the name contains 'magic' {}'s, expand them and return the
18624 * expanded name in an allocated string via 'alias' - caller must free.
18625 */
18626 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018627get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 char_u **arg;
18629 char_u **alias;
18630 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018631 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632{
18633 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 char_u *p;
18635 char_u *expr_start;
18636 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637
18638 *alias = NULL; /* default to no alias */
18639
18640 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18641 && (*arg)[2] == (int)KE_SNR)
18642 {
18643 /* hard coded <SNR>, already translated */
18644 *arg += 3;
18645 return get_id_len(arg) + 3;
18646 }
18647 len = eval_fname_script(*arg);
18648 if (len > 0)
18649 {
18650 /* literal "<SID>", "s:" or "<SNR>" */
18651 *arg += len;
18652 }
18653
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018655 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018657 p = find_name_end(*arg, &expr_start, &expr_end,
18658 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 if (expr_start != NULL)
18660 {
18661 char_u *temp_string;
18662
18663 if (!evaluate)
18664 {
18665 len += (int)(p - *arg);
18666 *arg = skipwhite(p);
18667 return len;
18668 }
18669
18670 /*
18671 * Include any <SID> etc in the expanded string:
18672 * Thus the -len here.
18673 */
18674 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18675 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018676 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 *alias = temp_string;
18678 *arg = skipwhite(p);
18679 return (int)STRLEN(temp_string);
18680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681
18682 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018683 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 EMSG2(_(e_invexpr2), *arg);
18685
18686 return len;
18687}
18688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018689/*
18690 * Find the end of a variable or function name, taking care of magic braces.
18691 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18692 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018693 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 * Return a pointer to just after the name. Equal to "arg" if there is no
18695 * valid name.
18696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018698find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 char_u *arg;
18700 char_u **expr_start;
18701 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018702 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704 int mb_nest = 0;
18705 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 char_u *p;
18707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018708 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018710 *expr_start = NULL;
18711 *expr_end = NULL;
18712 }
18713
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018714 /* Quick check for valid starting character. */
18715 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18716 return arg;
18717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018718 for (p = arg; *p != NUL
18719 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018720 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018721 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018723 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018724 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018725 if (*p == '\'')
18726 {
18727 /* skip over 'string' to avoid counting [ and ] inside it. */
18728 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18729 ;
18730 if (*p == NUL)
18731 break;
18732 }
18733 else if (*p == '"')
18734 {
18735 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18736 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18737 if (*p == '\\' && p[1] != NUL)
18738 ++p;
18739 if (*p == NUL)
18740 break;
18741 }
18742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018743 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018745 if (*p == '[')
18746 ++br_nest;
18747 else if (*p == ']')
18748 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018751 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018753 if (*p == '{')
18754 {
18755 mb_nest++;
18756 if (expr_start != NULL && *expr_start == NULL)
18757 *expr_start = p;
18758 }
18759 else if (*p == '}')
18760 {
18761 mb_nest--;
18762 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18763 *expr_end = p;
18764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 }
18767
18768 return p;
18769}
18770
18771/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018772 * Expands out the 'magic' {}'s in a variable/function name.
18773 * Note that this can call itself recursively, to deal with
18774 * constructs like foo{bar}{baz}{bam}
18775 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18776 * "in_start" ^
18777 * "expr_start" ^
18778 * "expr_end" ^
18779 * "in_end" ^
18780 *
18781 * Returns a new allocated string, which the caller must free.
18782 * Returns NULL for failure.
18783 */
18784 static char_u *
18785make_expanded_name(in_start, expr_start, expr_end, in_end)
18786 char_u *in_start;
18787 char_u *expr_start;
18788 char_u *expr_end;
18789 char_u *in_end;
18790{
18791 char_u c1;
18792 char_u *retval = NULL;
18793 char_u *temp_result;
18794 char_u *nextcmd = NULL;
18795
18796 if (expr_end == NULL || in_end == NULL)
18797 return NULL;
18798 *expr_start = NUL;
18799 *expr_end = NUL;
18800 c1 = *in_end;
18801 *in_end = NUL;
18802
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018803 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018804 if (temp_result != NULL && nextcmd == NULL)
18805 {
18806 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18807 + (in_end - expr_end) + 1));
18808 if (retval != NULL)
18809 {
18810 STRCPY(retval, in_start);
18811 STRCAT(retval, temp_result);
18812 STRCAT(retval, expr_end + 1);
18813 }
18814 }
18815 vim_free(temp_result);
18816
18817 *in_end = c1; /* put char back for error messages */
18818 *expr_start = '{';
18819 *expr_end = '}';
18820
18821 if (retval != NULL)
18822 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018823 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018824 if (expr_start != NULL)
18825 {
18826 /* Further expansion! */
18827 temp_result = make_expanded_name(retval, expr_start,
18828 expr_end, temp_result);
18829 vim_free(retval);
18830 retval = temp_result;
18831 }
18832 }
18833
18834 return retval;
18835}
18836
18837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018839 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 */
18841 static int
18842eval_isnamec(c)
18843 int c;
18844{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018845 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18846}
18847
18848/*
18849 * Return TRUE if character "c" can be used as the first character in a
18850 * variable or function name (excluding '{' and '}').
18851 */
18852 static int
18853eval_isnamec1(c)
18854 int c;
18855{
18856 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857}
18858
18859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 * Set number v: variable to "val".
18861 */
18862 void
18863set_vim_var_nr(idx, val)
18864 int idx;
18865 long val;
18866{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018867 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868}
18869
18870/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018871 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872 */
18873 long
18874get_vim_var_nr(idx)
18875 int idx;
18876{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018877 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878}
18879
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018880/*
18881 * Get string v: variable value. Uses a static buffer, can only be used once.
18882 */
18883 char_u *
18884get_vim_var_str(idx)
18885 int idx;
18886{
18887 return get_tv_string(&vimvars[idx].vv_tv);
18888}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018889
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018891 * Get List v: variable value. Caller must take care of reference count when
18892 * needed.
18893 */
18894 list_T *
18895get_vim_var_list(idx)
18896 int idx;
18897{
18898 return vimvars[idx].vv_list;
18899}
18900
18901/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018902 * Set v:char to character "c".
18903 */
18904 void
18905set_vim_var_char(c)
18906 int c;
18907{
18908#ifdef FEAT_MBYTE
18909 char_u buf[MB_MAXBYTES];
18910#else
18911 char_u buf[2];
18912#endif
18913
18914#ifdef FEAT_MBYTE
18915 if (has_mbyte)
18916 buf[(*mb_char2bytes)(c, buf)] = NUL;
18917 else
18918#endif
18919 {
18920 buf[0] = c;
18921 buf[1] = NUL;
18922 }
18923 set_vim_var_string(VV_CHAR, buf, -1);
18924}
18925
18926/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018927 * Set v:count to "count" and v:count1 to "count1".
18928 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 */
18930 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018931set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 long count;
18933 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018934 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018936 if (set_prevcount)
18937 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018938 vimvars[VV_COUNT].vv_nr = count;
18939 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940}
18941
18942/*
18943 * Set string v: variable to a copy of "val".
18944 */
18945 void
18946set_vim_var_string(idx, val, len)
18947 int idx;
18948 char_u *val;
18949 int len; /* length of "val" to use or -1 (whole string) */
18950{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018951 /* Need to do this (at least) once, since we can't initialize a union.
18952 * Will always be invoked when "v:progname" is set. */
18953 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18954
Bram Moolenaare9a41262005-01-15 22:18:47 +000018955 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018957 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018959 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018961 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962}
18963
18964/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018965 * Set List v: variable to "val".
18966 */
18967 void
18968set_vim_var_list(idx, val)
18969 int idx;
18970 list_T *val;
18971{
18972 list_unref(vimvars[idx].vv_list);
18973 vimvars[idx].vv_list = val;
18974 if (val != NULL)
18975 ++val->lv_refcount;
18976}
18977
18978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 * Set v:register if needed.
18980 */
18981 void
18982set_reg_var(c)
18983 int c;
18984{
18985 char_u regname;
18986
18987 if (c == 0 || c == ' ')
18988 regname = '"';
18989 else
18990 regname = c;
18991 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018992 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 set_vim_var_string(VV_REG, &regname, 1);
18994}
18995
18996/*
18997 * Get or set v:exception. If "oldval" == NULL, return the current value.
18998 * Otherwise, restore the value to "oldval" and return NULL.
18999 * Must always be called in pairs to save and restore v:exception! Does not
19000 * take care of memory allocations.
19001 */
19002 char_u *
19003v_exception(oldval)
19004 char_u *oldval;
19005{
19006 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019007 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008
Bram Moolenaare9a41262005-01-15 22:18:47 +000019009 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 return NULL;
19011}
19012
19013/*
19014 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19015 * Otherwise, restore the value to "oldval" and return NULL.
19016 * Must always be called in pairs to save and restore v:throwpoint! Does not
19017 * take care of memory allocations.
19018 */
19019 char_u *
19020v_throwpoint(oldval)
19021 char_u *oldval;
19022{
19023 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019024 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025
Bram Moolenaare9a41262005-01-15 22:18:47 +000019026 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 return NULL;
19028}
19029
19030#if defined(FEAT_AUTOCMD) || defined(PROTO)
19031/*
19032 * Set v:cmdarg.
19033 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19034 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19035 * Must always be called in pairs!
19036 */
19037 char_u *
19038set_cmdarg(eap, oldarg)
19039 exarg_T *eap;
19040 char_u *oldarg;
19041{
19042 char_u *oldval;
19043 char_u *newval;
19044 unsigned len;
19045
Bram Moolenaare9a41262005-01-15 22:18:47 +000019046 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019047 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019049 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019050 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019051 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 }
19053
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019054 if (eap->force_bin == FORCE_BIN)
19055 len = 6;
19056 else if (eap->force_bin == FORCE_NOBIN)
19057 len = 8;
19058 else
19059 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019060
19061 if (eap->read_edit)
19062 len += 7;
19063
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019064 if (eap->force_ff != 0)
19065 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19066# ifdef FEAT_MBYTE
19067 if (eap->force_enc != 0)
19068 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019069 if (eap->bad_char != 0)
19070 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019071# endif
19072
19073 newval = alloc(len + 1);
19074 if (newval == NULL)
19075 return NULL;
19076
19077 if (eap->force_bin == FORCE_BIN)
19078 sprintf((char *)newval, " ++bin");
19079 else if (eap->force_bin == FORCE_NOBIN)
19080 sprintf((char *)newval, " ++nobin");
19081 else
19082 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019083
19084 if (eap->read_edit)
19085 STRCAT(newval, " ++edit");
19086
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019087 if (eap->force_ff != 0)
19088 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19089 eap->cmd + eap->force_ff);
19090# ifdef FEAT_MBYTE
19091 if (eap->force_enc != 0)
19092 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19093 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019094 if (eap->bad_char == BAD_KEEP)
19095 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19096 else if (eap->bad_char == BAD_DROP)
19097 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19098 else if (eap->bad_char != 0)
19099 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019100# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019101 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019102 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103}
19104#endif
19105
19106/*
19107 * Get the value of internal variable "name".
19108 * Return OK or FAIL.
19109 */
19110 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019111get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 char_u *name;
19113 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019114 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019115 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116{
19117 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019118 typval_T *tv = NULL;
19119 typval_T atv;
19120 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122
19123 /* truncate the name, so that we can use strcmp() */
19124 cc = name[len];
19125 name[len] = NUL;
19126
19127 /*
19128 * Check for "b:changedtick".
19129 */
19130 if (STRCMP(name, "b:changedtick") == 0)
19131 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019132 atv.v_type = VAR_NUMBER;
19133 atv.vval.v_number = curbuf->b_changedtick;
19134 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 }
19136
19137 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 * Check for user-defined variables.
19139 */
19140 else
19141 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019142 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019144 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 }
19146
Bram Moolenaare9a41262005-01-15 22:18:47 +000019147 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019149 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019150 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 ret = FAIL;
19152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019153 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019154 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155
19156 name[len] = cc;
19157
19158 return ret;
19159}
19160
19161/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019162 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19163 * Also handle function call with Funcref variable: func(expr)
19164 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19165 */
19166 static int
19167handle_subscript(arg, rettv, evaluate, verbose)
19168 char_u **arg;
19169 typval_T *rettv;
19170 int evaluate; /* do more than finding the end */
19171 int verbose; /* give error messages */
19172{
19173 int ret = OK;
19174 dict_T *selfdict = NULL;
19175 char_u *s;
19176 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019177 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019178
19179 while (ret == OK
19180 && (**arg == '['
19181 || (**arg == '.' && rettv->v_type == VAR_DICT)
19182 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19183 && !vim_iswhite(*(*arg - 1)))
19184 {
19185 if (**arg == '(')
19186 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019187 /* need to copy the funcref so that we can clear rettv */
19188 functv = *rettv;
19189 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019190
19191 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019192 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019193 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019194 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19195 &len, evaluate, selfdict);
19196
19197 /* Clear the funcref afterwards, so that deleting it while
19198 * evaluating the arguments is possible (see test55). */
19199 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019200
19201 /* Stop the expression evaluation when immediately aborting on
19202 * error, or when an interrupt occurred or an exception was thrown
19203 * but not caught. */
19204 if (aborting())
19205 {
19206 if (ret == OK)
19207 clear_tv(rettv);
19208 ret = FAIL;
19209 }
19210 dict_unref(selfdict);
19211 selfdict = NULL;
19212 }
19213 else /* **arg == '[' || **arg == '.' */
19214 {
19215 dict_unref(selfdict);
19216 if (rettv->v_type == VAR_DICT)
19217 {
19218 selfdict = rettv->vval.v_dict;
19219 if (selfdict != NULL)
19220 ++selfdict->dv_refcount;
19221 }
19222 else
19223 selfdict = NULL;
19224 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19225 {
19226 clear_tv(rettv);
19227 ret = FAIL;
19228 }
19229 }
19230 }
19231 dict_unref(selfdict);
19232 return ret;
19233}
19234
19235/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019236 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019237 * value).
19238 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019239 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019240alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019241{
Bram Moolenaar33570922005-01-25 22:26:29 +000019242 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019243}
19244
19245/*
19246 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 * The string "s" must have been allocated, it is consumed.
19248 * Return NULL for out of memory, the variable otherwise.
19249 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019250 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 char_u *s;
19253{
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256 rettv = alloc_tv();
19257 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019259 rettv->v_type = VAR_STRING;
19260 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 }
19262 else
19263 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019264 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265}
19266
19267/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019268 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019270 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019271free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019272 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273{
19274 if (varp != NULL)
19275 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 switch (varp->v_type)
19277 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019278 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019279 func_unref(varp->vval.v_string);
19280 /*FALLTHROUGH*/
19281 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019282 vim_free(varp->vval.v_string);
19283 break;
19284 case VAR_LIST:
19285 list_unref(varp->vval.v_list);
19286 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019287 case VAR_DICT:
19288 dict_unref(varp->vval.v_dict);
19289 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019290 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019291#ifdef FEAT_FLOAT
19292 case VAR_FLOAT:
19293#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019294 case VAR_UNKNOWN:
19295 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019296 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019297 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019298 break;
19299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 vim_free(varp);
19301 }
19302}
19303
19304/*
19305 * Free the memory for a variable value and set the value to NULL or 0.
19306 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019307 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019308clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019309 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310{
19311 if (varp != NULL)
19312 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019313 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019315 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019316 func_unref(varp->vval.v_string);
19317 /*FALLTHROUGH*/
19318 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019319 vim_free(varp->vval.v_string);
19320 varp->vval.v_string = NULL;
19321 break;
19322 case VAR_LIST:
19323 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019324 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019325 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019326 case VAR_DICT:
19327 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019328 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019329 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019330 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019331 varp->vval.v_number = 0;
19332 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019333#ifdef FEAT_FLOAT
19334 case VAR_FLOAT:
19335 varp->vval.v_float = 0.0;
19336 break;
19337#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019338 case VAR_UNKNOWN:
19339 break;
19340 default:
19341 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019343 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 }
19345}
19346
19347/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019348 * Set the value of a variable to NULL without freeing items.
19349 */
19350 static void
19351init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019352 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019353{
19354 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019355 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019356}
19357
19358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 * Get the number value of a variable.
19360 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019361 * For incompatible types, return 0.
19362 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19363 * caller of incompatible types: it sets *denote to TRUE if "denote"
19364 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 */
19366 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019367get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019368 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019370 int error = FALSE;
19371
19372 return get_tv_number_chk(varp, &error); /* return 0L on error */
19373}
19374
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019375 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019376get_tv_number_chk(varp, denote)
19377 typval_T *varp;
19378 int *denote;
19379{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019380 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019382 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019384 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019385 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019386#ifdef FEAT_FLOAT
19387 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019388 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019389 break;
19390#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019391 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019392 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019393 break;
19394 case VAR_STRING:
19395 if (varp->vval.v_string != NULL)
19396 vim_str2nr(varp->vval.v_string, NULL, NULL,
19397 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019398 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019399 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019400 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019401 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019402 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019403 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019404 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019405 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019406 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019409 if (denote == NULL) /* useful for values that must be unsigned */
19410 n = -1;
19411 else
19412 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019413 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414}
19415
19416/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019417 * Get the lnum from the first argument.
19418 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019419 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 */
19421 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019422get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019423 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424{
Bram Moolenaar33570922005-01-25 22:26:29 +000019425 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 linenr_T lnum;
19427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019428 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 if (lnum == 0) /* no valid number, try using line() */
19430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019431 rettv.v_type = VAR_NUMBER;
19432 f_line(argvars, &rettv);
19433 lnum = rettv.vval.v_number;
19434 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 }
19436 return lnum;
19437}
19438
19439/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019440 * Get the lnum from the first argument.
19441 * Also accepts "$", then "buf" is used.
19442 * Returns 0 on error.
19443 */
19444 static linenr_T
19445get_tv_lnum_buf(argvars, buf)
19446 typval_T *argvars;
19447 buf_T *buf;
19448{
19449 if (argvars[0].v_type == VAR_STRING
19450 && argvars[0].vval.v_string != NULL
19451 && argvars[0].vval.v_string[0] == '$'
19452 && buf != NULL)
19453 return buf->b_ml.ml_line_count;
19454 return get_tv_number_chk(&argvars[0], NULL);
19455}
19456
19457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 * Get the string value of a variable.
19459 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019460 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19461 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 * If the String variable has never been set, return an empty string.
19463 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019464 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19465 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 */
19467 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019468get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019469 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470{
19471 static char_u mybuf[NUMBUFLEN];
19472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019473 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474}
19475
19476 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019478 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019479 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019481 char_u *res = get_tv_string_buf_chk(varp, buf);
19482
19483 return res != NULL ? res : (char_u *)"";
19484}
19485
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019486 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019487get_tv_string_chk(varp)
19488 typval_T *varp;
19489{
19490 static char_u mybuf[NUMBUFLEN];
19491
19492 return get_tv_string_buf_chk(varp, mybuf);
19493}
19494
19495 static char_u *
19496get_tv_string_buf_chk(varp, buf)
19497 typval_T *varp;
19498 char_u *buf;
19499{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019500 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019502 case VAR_NUMBER:
19503 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19504 return buf;
19505 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019506 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019507 break;
19508 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019509 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019510 break;
19511 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019512 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019513 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019514#ifdef FEAT_FLOAT
19515 case VAR_FLOAT:
19516 EMSG(_("E806: using Float as a String"));
19517 break;
19518#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019519 case VAR_STRING:
19520 if (varp->vval.v_string != NULL)
19521 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019522 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019523 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019524 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019527 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528}
19529
19530/*
19531 * Find variable "name" in the list of variables.
19532 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019533 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019534 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019535 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019537 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019538find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019540 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019543 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544
Bram Moolenaara7043832005-01-21 11:56:39 +000019545 ht = find_var_ht(name, &varname);
19546 if (htp != NULL)
19547 *htp = ht;
19548 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019550 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551}
19552
19553/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019554 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019555 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019557 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019558find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019559 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019560 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019561 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019562{
Bram Moolenaar33570922005-01-25 22:26:29 +000019563 hashitem_T *hi;
19564
19565 if (*varname == NUL)
19566 {
19567 /* Must be something like "s:", otherwise "ht" would be NULL. */
19568 switch (varname[-2])
19569 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019570 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019571 case 'g': return &globvars_var;
19572 case 'v': return &vimvars_var;
19573 case 'b': return &curbuf->b_bufvar;
19574 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019575#ifdef FEAT_WINDOWS
19576 case 't': return &curtab->tp_winvar;
19577#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019578 case 'l': return current_funccal == NULL
19579 ? NULL : &current_funccal->l_vars_var;
19580 case 'a': return current_funccal == NULL
19581 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019582 }
19583 return NULL;
19584 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019585
19586 hi = hash_find(ht, varname);
19587 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019588 {
19589 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019590 * worked find the variable again. Don't auto-load a script if it was
19591 * loaded already, otherwise it would be loaded every time when
19592 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019593 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019594 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019595 hi = hash_find(ht, varname);
19596 if (HASHITEM_EMPTY(hi))
19597 return NULL;
19598 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019600}
19601
19602/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019604 * Set "varname" to the start of name without ':'.
19605 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019606 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019607find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 char_u *name;
19609 char_u **varname;
19610{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019611 hashitem_T *hi;
19612
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 if (name[1] != ':')
19614 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019615 /* The name must not start with a colon or #. */
19616 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 return NULL;
19618 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019619
19620 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019621 hi = hash_find(&compat_hashtab, name);
19622 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019623 return &compat_hashtab;
19624
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 return &globvarht; /* global variable */
19627 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 }
19629 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019630 if (*name == 'g') /* global variable */
19631 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019632 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19633 */
19634 if (vim_strchr(name + 2, ':') != NULL
19635 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019636 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019640 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019641#ifdef FEAT_WINDOWS
19642 if (*name == 't') /* tab page variable */
19643 return &curtab->tp_vars.dv_hashtab;
19644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019645 if (*name == 'v') /* v: variable */
19646 return &vimvarht;
19647 if (*name == 'a' && current_funccal != NULL) /* function argument */
19648 return &current_funccal->l_avars.dv_hashtab;
19649 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19650 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 if (*name == 's' /* script variable */
19652 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19653 return &SCRIPT_VARS(current_SID);
19654 return NULL;
19655}
19656
19657/*
19658 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019659 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 * Returns NULL when it doesn't exist.
19661 */
19662 char_u *
19663get_var_value(name)
19664 char_u *name;
19665{
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667
Bram Moolenaara7043832005-01-21 11:56:39 +000019668 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 if (v == NULL)
19670 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672}
19673
19674/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 * sourcing this script and when executing functions defined in the script.
19677 */
19678 void
19679new_script_vars(id)
19680 scid_T id;
19681{
Bram Moolenaara7043832005-01-21 11:56:39 +000019682 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019683 hashtab_T *ht;
19684 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019685
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19687 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019688 /* Re-allocating ga_data means that an ht_array pointing to
19689 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019690 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019691 for (i = 1; i <= ga_scripts.ga_len; ++i)
19692 {
19693 ht = &SCRIPT_VARS(i);
19694 if (ht->ht_mask == HT_INIT_SIZE - 1)
19695 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019696 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019698 }
19699
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 while (ga_scripts.ga_len < id)
19701 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019702 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019703 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019704 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 }
19707 }
19708}
19709
19710/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019711 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19712 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 */
19714 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019715init_var_dict(dict, dict_var)
19716 dict_T *dict;
19717 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718{
Bram Moolenaar33570922005-01-25 22:26:29 +000019719 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019720 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019721 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019722 dict_var->di_tv.vval.v_dict = dict;
19723 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019724 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019725 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19726 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727}
19728
19729/*
19730 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 * Frees all allocated variables and the value they contain.
19732 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 */
19734 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019735vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019736 hashtab_T *ht;
19737{
19738 vars_clear_ext(ht, TRUE);
19739}
19740
19741/*
19742 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19743 */
19744 static void
19745vars_clear_ext(ht, free_val)
19746 hashtab_T *ht;
19747 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748{
Bram Moolenaara7043832005-01-21 11:56:39 +000019749 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019750 hashitem_T *hi;
19751 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019754 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019755 for (hi = ht->ht_array; todo > 0; ++hi)
19756 {
19757 if (!HASHITEM_EMPTY(hi))
19758 {
19759 --todo;
19760
Bram Moolenaar33570922005-01-25 22:26:29 +000019761 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019762 * ht_array might change then. hash_clear() takes care of it
19763 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 v = HI2DI(hi);
19765 if (free_val)
19766 clear_tv(&v->di_tv);
19767 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19768 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019769 }
19770 }
19771 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019772 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773}
19774
Bram Moolenaara7043832005-01-21 11:56:39 +000019775/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019776 * Delete a variable from hashtab "ht" at item "hi".
19777 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019780delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019781 hashtab_T *ht;
19782 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783{
Bram Moolenaar33570922005-01-25 22:26:29 +000019784 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019785
19786 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019787 clear_tv(&di->di_tv);
19788 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789}
19790
19791/*
19792 * List the value of one internal variable.
19793 */
19794 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019795list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019796 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019798 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019800 char_u *tofree;
19801 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019802 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019803
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019804 current_copyID += COPYID_INC;
19805 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019807 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809}
19810
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019812list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 char_u *prefix;
19814 char_u *name;
19815 int type;
19816 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019817 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818{
Bram Moolenaar31859182007-08-14 20:41:13 +000019819 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19820 msg_start();
19821 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 if (name != NULL) /* "a:" vars don't have a name stored */
19823 msg_puts(name);
19824 msg_putchar(' ');
19825 msg_advance(22);
19826 if (type == VAR_NUMBER)
19827 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019828 else if (type == VAR_FUNC)
19829 msg_putchar('*');
19830 else if (type == VAR_LIST)
19831 {
19832 msg_putchar('[');
19833 if (*string == '[')
19834 ++string;
19835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019836 else if (type == VAR_DICT)
19837 {
19838 msg_putchar('{');
19839 if (*string == '{')
19840 ++string;
19841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 else
19843 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019844
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019846
19847 if (type == VAR_FUNC)
19848 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019849 if (*first)
19850 {
19851 msg_clr_eos();
19852 *first = FALSE;
19853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854}
19855
19856/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 * If the variable already exists, the value is updated.
19859 * Otherwise the variable is created.
19860 */
19861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019862set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019865 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866{
Bram Moolenaar33570922005-01-25 22:26:29 +000019867 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019869 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019871 ht = find_var_ht(name, &varname);
19872 if (ht == NULL || *varname == NUL)
19873 {
19874 EMSG2(_(e_illvar), name);
19875 return;
19876 }
19877 v = find_var_in_ht(ht, varname, TRUE);
19878
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019879 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19880 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019881
Bram Moolenaar33570922005-01-25 22:26:29 +000019882 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019884 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019885 if (var_check_ro(v->di_flags, name)
19886 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019887 return;
19888 if (v->di_tv.v_type != tv->v_type
19889 && !((v->di_tv.v_type == VAR_STRING
19890 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019891 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019892 || tv->v_type == VAR_NUMBER))
19893#ifdef FEAT_FLOAT
19894 && !((v->di_tv.v_type == VAR_NUMBER
19895 || v->di_tv.v_type == VAR_FLOAT)
19896 && (tv->v_type == VAR_NUMBER
19897 || tv->v_type == VAR_FLOAT))
19898#endif
19899 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019900 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019901 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019902 return;
19903 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019904
19905 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019906 * Handle setting internal v: variables separately: we don't change
19907 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 */
19909 if (ht == &vimvarht)
19910 {
19911 if (v->di_tv.v_type == VAR_STRING)
19912 {
19913 vim_free(v->di_tv.vval.v_string);
19914 if (copy || tv->v_type != VAR_STRING)
19915 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19916 else
19917 {
19918 /* Take over the string to avoid an extra alloc/free. */
19919 v->di_tv.vval.v_string = tv->vval.v_string;
19920 tv->vval.v_string = NULL;
19921 }
19922 }
19923 else if (v->di_tv.v_type != VAR_NUMBER)
19924 EMSG2(_(e_intern2), "set_var()");
19925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019926 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019928 if (STRCMP(varname, "searchforward") == 0)
19929 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19930 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 return;
19932 }
19933
19934 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 }
19936 else /* add a new variable */
19937 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019938 /* Can't add "v:" variable. */
19939 if (ht == &vimvarht)
19940 {
19941 EMSG2(_(e_illvar), name);
19942 return;
19943 }
19944
Bram Moolenaar92124a32005-06-17 22:03:40 +000019945 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019946 if (!valid_varname(varname))
19947 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019948
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019949 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19950 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019951 if (v == NULL)
19952 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019953 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019954 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019956 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 return;
19958 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019959 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019961
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019962 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019963 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019964 else
19965 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019967 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970}
19971
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019972/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019973 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019974 * Also give an error message.
19975 */
19976 static int
19977var_check_ro(flags, name)
19978 int flags;
19979 char_u *name;
19980{
19981 if (flags & DI_FLAGS_RO)
19982 {
19983 EMSG2(_(e_readonlyvar), name);
19984 return TRUE;
19985 }
19986 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19987 {
19988 EMSG2(_(e_readonlysbx), name);
19989 return TRUE;
19990 }
19991 return FALSE;
19992}
19993
19994/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019995 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19996 * Also give an error message.
19997 */
19998 static int
19999var_check_fixed(flags, name)
20000 int flags;
20001 char_u *name;
20002{
20003 if (flags & DI_FLAGS_FIX)
20004 {
20005 EMSG2(_("E795: Cannot delete variable %s"), name);
20006 return TRUE;
20007 }
20008 return FALSE;
20009}
20010
20011/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020012 * Check if a funcref is assigned to a valid variable name.
20013 * Return TRUE and give an error if not.
20014 */
20015 static int
20016var_check_func_name(name, new_var)
20017 char_u *name; /* points to start of variable name */
20018 int new_var; /* TRUE when creating the variable */
20019{
20020 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20021 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20022 ? name[2] : name[0]))
20023 {
20024 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20025 name);
20026 return TRUE;
20027 }
20028 /* Don't allow hiding a function. When "v" is not NULL we might be
20029 * assigning another function to the same var, the type is checked
20030 * below. */
20031 if (new_var && function_exists(name))
20032 {
20033 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20034 name);
20035 return TRUE;
20036 }
20037 return FALSE;
20038}
20039
20040/*
20041 * Check if a variable name is valid.
20042 * Return FALSE and give an error if not.
20043 */
20044 static int
20045valid_varname(varname)
20046 char_u *varname;
20047{
20048 char_u *p;
20049
20050 for (p = varname; *p != NUL; ++p)
20051 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20052 && *p != AUTOLOAD_CHAR)
20053 {
20054 EMSG2(_(e_illvar), varname);
20055 return FALSE;
20056 }
20057 return TRUE;
20058}
20059
20060/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020061 * Return TRUE if typeval "tv" is set to be locked (immutable).
20062 * Also give an error message, using "name".
20063 */
20064 static int
20065tv_check_lock(lock, name)
20066 int lock;
20067 char_u *name;
20068{
20069 if (lock & VAR_LOCKED)
20070 {
20071 EMSG2(_("E741: Value is locked: %s"),
20072 name == NULL ? (char_u *)_("Unknown") : name);
20073 return TRUE;
20074 }
20075 if (lock & VAR_FIXED)
20076 {
20077 EMSG2(_("E742: Cannot change value of %s"),
20078 name == NULL ? (char_u *)_("Unknown") : name);
20079 return TRUE;
20080 }
20081 return FALSE;
20082}
20083
20084/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020085 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020086 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020087 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020088 * It is OK for "from" and "to" to point to the same item. This is used to
20089 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020090 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020091 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020093 typval_T *from;
20094 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020096 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020097 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020098 switch (from->v_type)
20099 {
20100 case VAR_NUMBER:
20101 to->vval.v_number = from->vval.v_number;
20102 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020103#ifdef FEAT_FLOAT
20104 case VAR_FLOAT:
20105 to->vval.v_float = from->vval.v_float;
20106 break;
20107#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020108 case VAR_STRING:
20109 case VAR_FUNC:
20110 if (from->vval.v_string == NULL)
20111 to->vval.v_string = NULL;
20112 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020113 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020114 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020115 if (from->v_type == VAR_FUNC)
20116 func_ref(to->vval.v_string);
20117 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020118 break;
20119 case VAR_LIST:
20120 if (from->vval.v_list == NULL)
20121 to->vval.v_list = NULL;
20122 else
20123 {
20124 to->vval.v_list = from->vval.v_list;
20125 ++to->vval.v_list->lv_refcount;
20126 }
20127 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020128 case VAR_DICT:
20129 if (from->vval.v_dict == NULL)
20130 to->vval.v_dict = NULL;
20131 else
20132 {
20133 to->vval.v_dict = from->vval.v_dict;
20134 ++to->vval.v_dict->dv_refcount;
20135 }
20136 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020137 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020139 break;
20140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141}
20142
20143/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020144 * Make a copy of an item.
20145 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020146 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20147 * reference to an already copied list/dict can be used.
20148 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020149 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020150 static int
20151item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 typval_T *from;
20153 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020154 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020155 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020156{
20157 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020158 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020159
Bram Moolenaar33570922005-01-25 22:26:29 +000020160 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020161 {
20162 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020163 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020164 }
20165 ++recurse;
20166
20167 switch (from->v_type)
20168 {
20169 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020170#ifdef FEAT_FLOAT
20171 case VAR_FLOAT:
20172#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020173 case VAR_STRING:
20174 case VAR_FUNC:
20175 copy_tv(from, to);
20176 break;
20177 case VAR_LIST:
20178 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020179 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020180 if (from->vval.v_list == NULL)
20181 to->vval.v_list = NULL;
20182 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20183 {
20184 /* use the copy made earlier */
20185 to->vval.v_list = from->vval.v_list->lv_copylist;
20186 ++to->vval.v_list->lv_refcount;
20187 }
20188 else
20189 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20190 if (to->vval.v_list == NULL)
20191 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020192 break;
20193 case VAR_DICT:
20194 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020195 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020196 if (from->vval.v_dict == NULL)
20197 to->vval.v_dict = NULL;
20198 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20199 {
20200 /* use the copy made earlier */
20201 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20202 ++to->vval.v_dict->dv_refcount;
20203 }
20204 else
20205 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20206 if (to->vval.v_dict == NULL)
20207 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020208 break;
20209 default:
20210 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020211 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020212 }
20213 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020214 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020215}
20216
20217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 * ":echo expr1 ..." print each argument separated with a space, add a
20219 * newline at the end.
20220 * ":echon expr1 ..." print each argument plain.
20221 */
20222 void
20223ex_echo(eap)
20224 exarg_T *eap;
20225{
20226 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020227 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020228 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 char_u *p;
20230 int needclr = TRUE;
20231 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020232 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233
20234 if (eap->skip)
20235 ++emsg_skip;
20236 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20237 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020238 /* If eval1() causes an error message the text from the command may
20239 * still need to be cleared. E.g., "echo 22,44". */
20240 need_clr_eos = needclr;
20241
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020243 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 {
20245 /*
20246 * Report the invalid expression unless the expression evaluation
20247 * has been cancelled due to an aborting error, an interrupt, or an
20248 * exception.
20249 */
20250 if (!aborting())
20251 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020252 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 break;
20254 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020255 need_clr_eos = FALSE;
20256
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 if (!eap->skip)
20258 {
20259 if (atstart)
20260 {
20261 atstart = FALSE;
20262 /* Call msg_start() after eval1(), evaluating the expression
20263 * may cause a message to appear. */
20264 if (eap->cmdidx == CMD_echo)
20265 msg_start();
20266 }
20267 else if (eap->cmdidx == CMD_echo)
20268 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020269 current_copyID += COPYID_INC;
20270 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020271 if (p != NULL)
20272 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020274 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020276 if (*p != TAB && needclr)
20277 {
20278 /* remove any text still there from the command */
20279 msg_clr_eos();
20280 needclr = FALSE;
20281 }
20282 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 }
20284 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020285 {
20286#ifdef FEAT_MBYTE
20287 if (has_mbyte)
20288 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020289 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020290
20291 (void)msg_outtrans_len_attr(p, i, echo_attr);
20292 p += i - 1;
20293 }
20294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020296 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020299 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020301 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 arg = skipwhite(arg);
20303 }
20304 eap->nextcmd = check_nextcmd(arg);
20305
20306 if (eap->skip)
20307 --emsg_skip;
20308 else
20309 {
20310 /* remove text that may still be there from the command */
20311 if (needclr)
20312 msg_clr_eos();
20313 if (eap->cmdidx == CMD_echo)
20314 msg_end();
20315 }
20316}
20317
20318/*
20319 * ":echohl {name}".
20320 */
20321 void
20322ex_echohl(eap)
20323 exarg_T *eap;
20324{
20325 int id;
20326
20327 id = syn_name2id(eap->arg);
20328 if (id == 0)
20329 echo_attr = 0;
20330 else
20331 echo_attr = syn_id2attr(id);
20332}
20333
20334/*
20335 * ":execute expr1 ..." execute the result of an expression.
20336 * ":echomsg expr1 ..." Print a message
20337 * ":echoerr expr1 ..." Print an error
20338 * Each gets spaces around each argument and a newline at the end for
20339 * echo commands
20340 */
20341 void
20342ex_execute(eap)
20343 exarg_T *eap;
20344{
20345 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 int ret = OK;
20348 char_u *p;
20349 garray_T ga;
20350 int len;
20351 int save_did_emsg;
20352
20353 ga_init2(&ga, 1, 80);
20354
20355 if (eap->skip)
20356 ++emsg_skip;
20357 while (*arg != NUL && *arg != '|' && *arg != '\n')
20358 {
20359 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020360 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 {
20362 /*
20363 * Report the invalid expression unless the expression evaluation
20364 * has been cancelled due to an aborting error, an interrupt, or an
20365 * exception.
20366 */
20367 if (!aborting())
20368 EMSG2(_(e_invexpr2), p);
20369 ret = FAIL;
20370 break;
20371 }
20372
20373 if (!eap->skip)
20374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020375 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 len = (int)STRLEN(p);
20377 if (ga_grow(&ga, len + 2) == FAIL)
20378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 ret = FAIL;
20381 break;
20382 }
20383 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 ga.ga_len += len;
20387 }
20388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020389 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 arg = skipwhite(arg);
20391 }
20392
20393 if (ret != FAIL && ga.ga_data != NULL)
20394 {
20395 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020398 out_flush();
20399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 else if (eap->cmdidx == CMD_echoerr)
20401 {
20402 /* We don't want to abort following commands, restore did_emsg. */
20403 save_did_emsg = did_emsg;
20404 EMSG((char_u *)ga.ga_data);
20405 if (!force_abort)
20406 did_emsg = save_did_emsg;
20407 }
20408 else if (eap->cmdidx == CMD_execute)
20409 do_cmdline((char_u *)ga.ga_data,
20410 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20411 }
20412
20413 ga_clear(&ga);
20414
20415 if (eap->skip)
20416 --emsg_skip;
20417
20418 eap->nextcmd = check_nextcmd(arg);
20419}
20420
20421/*
20422 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20423 * "arg" points to the "&" or '+' when called, to "option" when returning.
20424 * Returns NULL when no option name found. Otherwise pointer to the char
20425 * after the option name.
20426 */
20427 static char_u *
20428find_option_end(arg, opt_flags)
20429 char_u **arg;
20430 int *opt_flags;
20431{
20432 char_u *p = *arg;
20433
20434 ++p;
20435 if (*p == 'g' && p[1] == ':')
20436 {
20437 *opt_flags = OPT_GLOBAL;
20438 p += 2;
20439 }
20440 else if (*p == 'l' && p[1] == ':')
20441 {
20442 *opt_flags = OPT_LOCAL;
20443 p += 2;
20444 }
20445 else
20446 *opt_flags = 0;
20447
20448 if (!ASCII_ISALPHA(*p))
20449 return NULL;
20450 *arg = p;
20451
20452 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20453 p += 4; /* termcap option */
20454 else
20455 while (ASCII_ISALPHA(*p))
20456 ++p;
20457 return p;
20458}
20459
20460/*
20461 * ":function"
20462 */
20463 void
20464ex_function(eap)
20465 exarg_T *eap;
20466{
20467 char_u *theline;
20468 int j;
20469 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 char_u *name = NULL;
20472 char_u *p;
20473 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020474 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 garray_T newargs;
20476 garray_T newlines;
20477 int varargs = FALSE;
20478 int mustend = FALSE;
20479 int flags = 0;
20480 ufunc_T *fp;
20481 int indent;
20482 int nesting;
20483 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020484 dictitem_T *v;
20485 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020486 static int func_nr = 0; /* number for nameless function */
20487 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020488 hashtab_T *ht;
20489 int todo;
20490 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020491 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492
20493 /*
20494 * ":function" without argument: list functions.
20495 */
20496 if (ends_excmd(*eap->arg))
20497 {
20498 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020499 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020500 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020501 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020502 {
20503 if (!HASHITEM_EMPTY(hi))
20504 {
20505 --todo;
20506 fp = HI2UF(hi);
20507 if (!isdigit(*fp->uf_name))
20508 list_func_head(fp, FALSE);
20509 }
20510 }
20511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 eap->nextcmd = check_nextcmd(eap->arg);
20513 return;
20514 }
20515
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020516 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020517 * ":function /pat": list functions matching pattern.
20518 */
20519 if (*eap->arg == '/')
20520 {
20521 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20522 if (!eap->skip)
20523 {
20524 regmatch_T regmatch;
20525
20526 c = *p;
20527 *p = NUL;
20528 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20529 *p = c;
20530 if (regmatch.regprog != NULL)
20531 {
20532 regmatch.rm_ic = p_ic;
20533
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020534 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020535 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20536 {
20537 if (!HASHITEM_EMPTY(hi))
20538 {
20539 --todo;
20540 fp = HI2UF(hi);
20541 if (!isdigit(*fp->uf_name)
20542 && vim_regexec(&regmatch, fp->uf_name, 0))
20543 list_func_head(fp, FALSE);
20544 }
20545 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020546 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020547 }
20548 }
20549 if (*p == '/')
20550 ++p;
20551 eap->nextcmd = check_nextcmd(p);
20552 return;
20553 }
20554
20555 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020556 * Get the function name. There are these situations:
20557 * func normal function name
20558 * "name" == func, "fudi.fd_dict" == NULL
20559 * dict.func new dictionary entry
20560 * "name" == NULL, "fudi.fd_dict" set,
20561 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20562 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020563 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020564 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20565 * dict.func existing dict entry that's not a Funcref
20566 * "name" == NULL, "fudi.fd_dict" set,
20567 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020570 name = trans_function_name(&p, eap->skip, 0, &fudi);
20571 paren = (vim_strchr(p, '(') != NULL);
20572 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 {
20574 /*
20575 * Return on an invalid expression in braces, unless the expression
20576 * evaluation has been cancelled due to an aborting error, an
20577 * interrupt, or an exception.
20578 */
20579 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020581 if (!eap->skip && fudi.fd_newkey != NULL)
20582 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020583 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 else
20587 eap->skip = TRUE;
20588 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020589
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 /* An error in a function call during evaluation of an expression in magic
20591 * braces should not cause the function not to be defined. */
20592 saved_did_emsg = did_emsg;
20593 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594
20595 /*
20596 * ":function func" with only function name: list function.
20597 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020598 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 {
20600 if (!ends_excmd(*skipwhite(p)))
20601 {
20602 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020603 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 }
20605 eap->nextcmd = check_nextcmd(p);
20606 if (eap->nextcmd != NULL)
20607 *p = NUL;
20608 if (!eap->skip && !got_int)
20609 {
20610 fp = find_func(name);
20611 if (fp != NULL)
20612 {
20613 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020614 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020616 if (FUNCLINE(fp, j) == NULL)
20617 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 msg_putchar('\n');
20619 msg_outnum((long)(j + 1));
20620 if (j < 9)
20621 msg_putchar(' ');
20622 if (j < 99)
20623 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020624 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 out_flush(); /* show a line at a time */
20626 ui_breakcheck();
20627 }
20628 if (!got_int)
20629 {
20630 msg_putchar('\n');
20631 msg_puts((char_u *)" endfunction");
20632 }
20633 }
20634 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020635 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020637 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 }
20639
20640 /*
20641 * ":function name(arg1, arg2)" Define function.
20642 */
20643 p = skipwhite(p);
20644 if (*p != '(')
20645 {
20646 if (!eap->skip)
20647 {
20648 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020649 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 }
20651 /* attempt to continue by skipping some text */
20652 if (vim_strchr(p, '(') != NULL)
20653 p = vim_strchr(p, '(');
20654 }
20655 p = skipwhite(p + 1);
20656
20657 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20658 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20659
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020660 if (!eap->skip)
20661 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020662 /* Check the name of the function. Unless it's a dictionary function
20663 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020664 if (name != NULL)
20665 arg = name;
20666 else
20667 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020668 if (arg != NULL && (fudi.fd_di == NULL
20669 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020670 {
20671 if (*arg == K_SPECIAL)
20672 j = 3;
20673 else
20674 j = 0;
20675 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20676 : eval_isnamec(arg[j])))
20677 ++j;
20678 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020679 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020680 }
20681 }
20682
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 /*
20684 * Isolate the arguments: "arg1, arg2, ...)"
20685 */
20686 while (*p != ')')
20687 {
20688 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20689 {
20690 varargs = TRUE;
20691 p += 3;
20692 mustend = TRUE;
20693 }
20694 else
20695 {
20696 arg = p;
20697 while (ASCII_ISALNUM(*p) || *p == '_')
20698 ++p;
20699 if (arg == p || isdigit(*arg)
20700 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20701 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20702 {
20703 if (!eap->skip)
20704 EMSG2(_("E125: Illegal argument: %s"), arg);
20705 break;
20706 }
20707 if (ga_grow(&newargs, 1) == FAIL)
20708 goto erret;
20709 c = *p;
20710 *p = NUL;
20711 arg = vim_strsave(arg);
20712 if (arg == NULL)
20713 goto erret;
20714 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20715 *p = c;
20716 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 if (*p == ',')
20718 ++p;
20719 else
20720 mustend = TRUE;
20721 }
20722 p = skipwhite(p);
20723 if (mustend && *p != ')')
20724 {
20725 if (!eap->skip)
20726 EMSG2(_(e_invarg2), eap->arg);
20727 break;
20728 }
20729 }
20730 ++p; /* skip the ')' */
20731
Bram Moolenaare9a41262005-01-15 22:18:47 +000020732 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 for (;;)
20734 {
20735 p = skipwhite(p);
20736 if (STRNCMP(p, "range", 5) == 0)
20737 {
20738 flags |= FC_RANGE;
20739 p += 5;
20740 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020741 else if (STRNCMP(p, "dict", 4) == 0)
20742 {
20743 flags |= FC_DICT;
20744 p += 4;
20745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 else if (STRNCMP(p, "abort", 5) == 0)
20747 {
20748 flags |= FC_ABORT;
20749 p += 5;
20750 }
20751 else
20752 break;
20753 }
20754
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020755 /* When there is a line break use what follows for the function body.
20756 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20757 if (*p == '\n')
20758 line_arg = p + 1;
20759 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 EMSG(_(e_trailing));
20761
20762 /*
20763 * Read the body of the function, until ":endfunction" is found.
20764 */
20765 if (KeyTyped)
20766 {
20767 /* Check if the function already exists, don't let the user type the
20768 * whole function before telling him it doesn't work! For a script we
20769 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020770 if (!eap->skip && !eap->forceit)
20771 {
20772 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20773 EMSG(_(e_funcdict));
20774 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020775 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020778 if (!eap->skip && did_emsg)
20779 goto erret;
20780
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 msg_putchar('\n'); /* don't overwrite the function name */
20782 cmdline_row = msg_row;
20783 }
20784
20785 indent = 2;
20786 nesting = 0;
20787 for (;;)
20788 {
20789 msg_scroll = TRUE;
20790 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020791 sourcing_lnum_off = sourcing_lnum;
20792
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020793 if (line_arg != NULL)
20794 {
20795 /* Use eap->arg, split up in parts by line breaks. */
20796 theline = line_arg;
20797 p = vim_strchr(theline, '\n');
20798 if (p == NULL)
20799 line_arg += STRLEN(line_arg);
20800 else
20801 {
20802 *p = NUL;
20803 line_arg = p + 1;
20804 }
20805 }
20806 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 theline = getcmdline(':', 0L, indent);
20808 else
20809 theline = eap->getline(':', eap->cookie, indent);
20810 if (KeyTyped)
20811 lines_left = Rows - 1;
20812 if (theline == NULL)
20813 {
20814 EMSG(_("E126: Missing :endfunction"));
20815 goto erret;
20816 }
20817
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020818 /* Detect line continuation: sourcing_lnum increased more than one. */
20819 if (sourcing_lnum > sourcing_lnum_off + 1)
20820 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20821 else
20822 sourcing_lnum_off = 0;
20823
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 if (skip_until != NULL)
20825 {
20826 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20827 * don't check for ":endfunc". */
20828 if (STRCMP(theline, skip_until) == 0)
20829 {
20830 vim_free(skip_until);
20831 skip_until = NULL;
20832 }
20833 }
20834 else
20835 {
20836 /* skip ':' and blanks*/
20837 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20838 ;
20839
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020840 /* Check for "endfunction". */
20841 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020843 if (line_arg == NULL)
20844 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 break;
20846 }
20847
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020848 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 * at "end". */
20850 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20851 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020852 else if (STRNCMP(p, "if", 2) == 0
20853 || STRNCMP(p, "wh", 2) == 0
20854 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 || STRNCMP(p, "try", 3) == 0)
20856 indent += 2;
20857
20858 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020859 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020861 if (*p == '!')
20862 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 p += eval_fname_script(p);
20864 if (ASCII_ISALPHA(*p))
20865 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020866 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 if (*skipwhite(p) == '(')
20868 {
20869 ++nesting;
20870 indent += 2;
20871 }
20872 }
20873 }
20874
20875 /* Check for ":append" or ":insert". */
20876 p = skip_range(p, NULL);
20877 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20878 || (p[0] == 'i'
20879 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20880 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20881 skip_until = vim_strsave((char_u *)".");
20882
20883 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20884 arg = skipwhite(skiptowhite(p));
20885 if (arg[0] == '<' && arg[1] =='<'
20886 && ((p[0] == 'p' && p[1] == 'y'
20887 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20888 || (p[0] == 'p' && p[1] == 'e'
20889 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20890 || (p[0] == 't' && p[1] == 'c'
20891 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20892 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20893 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020894 || (p[0] == 'm' && p[1] == 'z'
20895 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 ))
20897 {
20898 /* ":python <<" continues until a dot, like ":append" */
20899 p = skipwhite(arg + 2);
20900 if (*p == NUL)
20901 skip_until = vim_strsave((char_u *)".");
20902 else
20903 skip_until = vim_strsave(p);
20904 }
20905 }
20906
20907 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020908 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020909 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020910 if (line_arg == NULL)
20911 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020913 }
20914
20915 /* Copy the line to newly allocated memory. get_one_sourceline()
20916 * allocates 250 bytes per line, this saves 80% on average. The cost
20917 * is an extra alloc/free. */
20918 p = vim_strsave(theline);
20919 if (p != NULL)
20920 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020921 if (line_arg == NULL)
20922 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020923 theline = p;
20924 }
20925
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020926 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20927
20928 /* Add NULL lines for continuation lines, so that the line count is
20929 * equal to the index in the growarray. */
20930 while (sourcing_lnum_off-- > 0)
20931 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020932
20933 /* Check for end of eap->arg. */
20934 if (line_arg != NULL && *line_arg == NUL)
20935 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 }
20937
20938 /* Don't define the function when skipping commands or when an error was
20939 * detected. */
20940 if (eap->skip || did_emsg)
20941 goto erret;
20942
20943 /*
20944 * If there are no errors, add the function
20945 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020946 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020947 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020948 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020949 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020950 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020951 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020952 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020953 goto erret;
20954 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020955
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020956 fp = find_func(name);
20957 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020959 if (!eap->forceit)
20960 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020961 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020962 goto erret;
20963 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020965 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020966 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020967 name);
20968 goto erret;
20969 }
20970 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020971 ga_clear_strings(&(fp->uf_args));
20972 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020973 vim_free(name);
20974 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 }
20977 else
20978 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020979 char numbuf[20];
20980
20981 fp = NULL;
20982 if (fudi.fd_newkey == NULL && !eap->forceit)
20983 {
20984 EMSG(_(e_funcdict));
20985 goto erret;
20986 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020987 if (fudi.fd_di == NULL)
20988 {
20989 /* Can't add a function to a locked dictionary */
20990 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20991 goto erret;
20992 }
20993 /* Can't change an existing function if it is locked */
20994 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20995 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020996
20997 /* Give the function a sequential number. Can only be used with a
20998 * Funcref! */
20999 vim_free(name);
21000 sprintf(numbuf, "%d", ++func_nr);
21001 name = vim_strsave((char_u *)numbuf);
21002 if (name == NULL)
21003 goto erret;
21004 }
21005
21006 if (fp == NULL)
21007 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021008 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021009 {
21010 int slen, plen;
21011 char_u *scriptname;
21012
21013 /* Check that the autoload name matches the script name. */
21014 j = FAIL;
21015 if (sourcing_name != NULL)
21016 {
21017 scriptname = autoload_name(name);
21018 if (scriptname != NULL)
21019 {
21020 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021021 plen = (int)STRLEN(p);
21022 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021023 if (slen > plen && fnamecmp(p,
21024 sourcing_name + slen - plen) == 0)
21025 j = OK;
21026 vim_free(scriptname);
21027 }
21028 }
21029 if (j == FAIL)
21030 {
21031 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21032 goto erret;
21033 }
21034 }
21035
21036 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 if (fp == NULL)
21038 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021039
21040 if (fudi.fd_dict != NULL)
21041 {
21042 if (fudi.fd_di == NULL)
21043 {
21044 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021045 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021046 if (fudi.fd_di == NULL)
21047 {
21048 vim_free(fp);
21049 goto erret;
21050 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021051 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21052 {
21053 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021054 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021055 goto erret;
21056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021057 }
21058 else
21059 /* overwrite existing dict entry */
21060 clear_tv(&fudi.fd_di->di_tv);
21061 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021062 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021063 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021064 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021065
21066 /* behave like "dict" was used */
21067 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021068 }
21069
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021071 STRCPY(fp->uf_name, name);
21072 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021074 fp->uf_args = newargs;
21075 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021076#ifdef FEAT_PROFILE
21077 fp->uf_tml_count = NULL;
21078 fp->uf_tml_total = NULL;
21079 fp->uf_tml_self = NULL;
21080 fp->uf_profiling = FALSE;
21081 if (prof_def_func())
21082 func_do_profile(fp);
21083#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021084 fp->uf_varargs = varargs;
21085 fp->uf_flags = flags;
21086 fp->uf_calls = 0;
21087 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021088 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089
21090erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 ga_clear_strings(&newargs);
21092 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021093ret_free:
21094 vim_free(skip_until);
21095 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098}
21099
21100/*
21101 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021102 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021104 * flags:
21105 * TFN_INT: internal function name OK
21106 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 * Advances "pp" to just after the function name (if no error).
21108 */
21109 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 char_u **pp;
21112 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021113 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021114 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021116 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 char_u *start;
21118 char_u *end;
21119 int lead;
21120 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021123
21124 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021125 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021127
21128 /* Check for hard coded <SNR>: already translated function ID (from a user
21129 * command). */
21130 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21131 && (*pp)[2] == (int)KE_SNR)
21132 {
21133 *pp += 3;
21134 len = get_id_len(pp) + 3;
21135 return vim_strnsave(start, len);
21136 }
21137
21138 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21139 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021141 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021143
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021144 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21145 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021146 if (end == start)
21147 {
21148 if (!skip)
21149 EMSG(_("E129: Function name required"));
21150 goto theend;
21151 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021152 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021153 {
21154 /*
21155 * Report an invalid expression in braces, unless the expression
21156 * evaluation has been cancelled due to an aborting error, an
21157 * interrupt, or an exception.
21158 */
21159 if (!aborting())
21160 {
21161 if (end != NULL)
21162 EMSG2(_(e_invarg2), start);
21163 }
21164 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021165 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021166 goto theend;
21167 }
21168
21169 if (lv.ll_tv != NULL)
21170 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021171 if (fdp != NULL)
21172 {
21173 fdp->fd_dict = lv.ll_dict;
21174 fdp->fd_newkey = lv.ll_newkey;
21175 lv.ll_newkey = NULL;
21176 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021177 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021178 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21179 {
21180 name = vim_strsave(lv.ll_tv->vval.v_string);
21181 *pp = end;
21182 }
21183 else
21184 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021185 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21186 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021187 EMSG(_(e_funcref));
21188 else
21189 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021190 name = NULL;
21191 }
21192 goto theend;
21193 }
21194
21195 if (lv.ll_name == NULL)
21196 {
21197 /* Error found, but continue after the function name. */
21198 *pp = end;
21199 goto theend;
21200 }
21201
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021202 /* Check if the name is a Funcref. If so, use the value. */
21203 if (lv.ll_exp_name != NULL)
21204 {
21205 len = (int)STRLEN(lv.ll_exp_name);
21206 name = deref_func_name(lv.ll_exp_name, &len);
21207 if (name == lv.ll_exp_name)
21208 name = NULL;
21209 }
21210 else
21211 {
21212 len = (int)(end - *pp);
21213 name = deref_func_name(*pp, &len);
21214 if (name == *pp)
21215 name = NULL;
21216 }
21217 if (name != NULL)
21218 {
21219 name = vim_strsave(name);
21220 *pp = end;
21221 goto theend;
21222 }
21223
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021224 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021225 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021226 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021227 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21228 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21229 {
21230 /* When there was "s:" already or the name expanded to get a
21231 * leading "s:" then remove it. */
21232 lv.ll_name += 2;
21233 len -= 2;
21234 lead = 2;
21235 }
21236 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021237 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021238 {
21239 if (lead == 2) /* skip over "s:" */
21240 lv.ll_name += 2;
21241 len = (int)(end - lv.ll_name);
21242 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021243
21244 /*
21245 * Copy the function name to allocated memory.
21246 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21247 * Accept <SNR>123_name() outside a script.
21248 */
21249 if (skip)
21250 lead = 0; /* do nothing */
21251 else if (lead > 0)
21252 {
21253 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021254 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21255 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021256 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021257 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021258 if (current_SID <= 0)
21259 {
21260 EMSG(_(e_usingsid));
21261 goto theend;
21262 }
21263 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21264 lead += (int)STRLEN(sid_buf);
21265 }
21266 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021267 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021268 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021269 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021270 goto theend;
21271 }
21272 name = alloc((unsigned)(len + lead + 1));
21273 if (name != NULL)
21274 {
21275 if (lead > 0)
21276 {
21277 name[0] = K_SPECIAL;
21278 name[1] = KS_EXTRA;
21279 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021280 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021281 STRCPY(name + 3, sid_buf);
21282 }
21283 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21284 name[len + lead] = NUL;
21285 }
21286 *pp = end;
21287
21288theend:
21289 clear_lval(&lv);
21290 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291}
21292
21293/*
21294 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21295 * Return 2 if "p" starts with "s:".
21296 * Return 0 otherwise.
21297 */
21298 static int
21299eval_fname_script(p)
21300 char_u *p;
21301{
21302 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21303 || STRNICMP(p + 1, "SNR>", 4) == 0))
21304 return 5;
21305 if (p[0] == 's' && p[1] == ':')
21306 return 2;
21307 return 0;
21308}
21309
21310/*
21311 * Return TRUE if "p" starts with "<SID>" or "s:".
21312 * Only works if eval_fname_script() returned non-zero for "p"!
21313 */
21314 static int
21315eval_fname_sid(p)
21316 char_u *p;
21317{
21318 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21319}
21320
21321/*
21322 * List the head of the function: "name(arg1, arg2)".
21323 */
21324 static void
21325list_func_head(fp, indent)
21326 ufunc_T *fp;
21327 int indent;
21328{
21329 int j;
21330
21331 msg_start();
21332 if (indent)
21333 MSG_PUTS(" ");
21334 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021335 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 {
21337 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021338 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 }
21340 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021341 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021343 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 {
21345 if (j)
21346 MSG_PUTS(", ");
21347 msg_puts(FUNCARG(fp, j));
21348 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021349 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 {
21351 if (j)
21352 MSG_PUTS(", ");
21353 MSG_PUTS("...");
21354 }
21355 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021356 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021357 if (p_verbose > 0)
21358 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359}
21360
21361/*
21362 * Find a function by name, return pointer to it in ufuncs.
21363 * Return NULL for unknown function.
21364 */
21365 static ufunc_T *
21366find_func(name)
21367 char_u *name;
21368{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021369 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021371 hi = hash_find(&func_hashtab, name);
21372 if (!HASHITEM_EMPTY(hi))
21373 return HI2UF(hi);
21374 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375}
21376
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021377#if defined(EXITFREE) || defined(PROTO)
21378 void
21379free_all_functions()
21380{
21381 hashitem_T *hi;
21382
21383 /* Need to start all over every time, because func_free() may change the
21384 * hash table. */
21385 while (func_hashtab.ht_used > 0)
21386 for (hi = func_hashtab.ht_array; ; ++hi)
21387 if (!HASHITEM_EMPTY(hi))
21388 {
21389 func_free(HI2UF(hi));
21390 break;
21391 }
21392}
21393#endif
21394
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021395/*
21396 * Return TRUE if a function "name" exists.
21397 */
21398 static int
21399function_exists(name)
21400 char_u *name;
21401{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021402 char_u *nm = name;
21403 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021404 int n = FALSE;
21405
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021406 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021407 nm = skipwhite(nm);
21408
21409 /* Only accept "funcname", "funcname ", "funcname (..." and
21410 * "funcname(...", not "funcname!...". */
21411 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021412 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021413 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021414 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021415 else
21416 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021417 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021418 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021419 return n;
21420}
21421
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021422/*
21423 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021424 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021425 */
21426 static int
21427builtin_function(name)
21428 char_u *name;
21429{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021430 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21431 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021432}
21433
Bram Moolenaar05159a02005-02-26 23:04:13 +000021434#if defined(FEAT_PROFILE) || defined(PROTO)
21435/*
21436 * Start profiling function "fp".
21437 */
21438 static void
21439func_do_profile(fp)
21440 ufunc_T *fp;
21441{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021442 int len = fp->uf_lines.ga_len;
21443
21444 if (len == 0)
21445 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021446 fp->uf_tm_count = 0;
21447 profile_zero(&fp->uf_tm_self);
21448 profile_zero(&fp->uf_tm_total);
21449 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021450 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021451 if (fp->uf_tml_total == NULL)
21452 fp->uf_tml_total = (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 if (fp->uf_tml_self == NULL)
21455 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021456 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021457 fp->uf_tml_idx = -1;
21458 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21459 || fp->uf_tml_self == NULL)
21460 return; /* out of memory */
21461
21462 fp->uf_profiling = TRUE;
21463}
21464
21465/*
21466 * Dump the profiling results for all functions in file "fd".
21467 */
21468 void
21469func_dump_profile(fd)
21470 FILE *fd;
21471{
21472 hashitem_T *hi;
21473 int todo;
21474 ufunc_T *fp;
21475 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021476 ufunc_T **sorttab;
21477 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021478
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021479 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021480 if (todo == 0)
21481 return; /* nothing to dump */
21482
Bram Moolenaar73830342005-02-28 22:48:19 +000021483 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21484
Bram Moolenaar05159a02005-02-26 23:04:13 +000021485 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21486 {
21487 if (!HASHITEM_EMPTY(hi))
21488 {
21489 --todo;
21490 fp = HI2UF(hi);
21491 if (fp->uf_profiling)
21492 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021493 if (sorttab != NULL)
21494 sorttab[st_len++] = fp;
21495
Bram Moolenaar05159a02005-02-26 23:04:13 +000021496 if (fp->uf_name[0] == K_SPECIAL)
21497 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21498 else
21499 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21500 if (fp->uf_tm_count == 1)
21501 fprintf(fd, "Called 1 time\n");
21502 else
21503 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21504 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21505 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21506 fprintf(fd, "\n");
21507 fprintf(fd, "count total (s) self (s)\n");
21508
21509 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21510 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021511 if (FUNCLINE(fp, i) == NULL)
21512 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021513 prof_func_line(fd, fp->uf_tml_count[i],
21514 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021515 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21516 }
21517 fprintf(fd, "\n");
21518 }
21519 }
21520 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021521
21522 if (sorttab != NULL && st_len > 0)
21523 {
21524 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21525 prof_total_cmp);
21526 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21527 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21528 prof_self_cmp);
21529 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21530 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021531
21532 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021533}
Bram Moolenaar73830342005-02-28 22:48:19 +000021534
21535 static void
21536prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21537 FILE *fd;
21538 ufunc_T **sorttab;
21539 int st_len;
21540 char *title;
21541 int prefer_self; /* when equal print only self time */
21542{
21543 int i;
21544 ufunc_T *fp;
21545
21546 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21547 fprintf(fd, "count total (s) self (s) function\n");
21548 for (i = 0; i < 20 && i < st_len; ++i)
21549 {
21550 fp = sorttab[i];
21551 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21552 prefer_self);
21553 if (fp->uf_name[0] == K_SPECIAL)
21554 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21555 else
21556 fprintf(fd, " %s()\n", fp->uf_name);
21557 }
21558 fprintf(fd, "\n");
21559}
21560
21561/*
21562 * Print the count and times for one function or function line.
21563 */
21564 static void
21565prof_func_line(fd, count, total, self, prefer_self)
21566 FILE *fd;
21567 int count;
21568 proftime_T *total;
21569 proftime_T *self;
21570 int prefer_self; /* when equal print only self time */
21571{
21572 if (count > 0)
21573 {
21574 fprintf(fd, "%5d ", count);
21575 if (prefer_self && profile_equal(total, self))
21576 fprintf(fd, " ");
21577 else
21578 fprintf(fd, "%s ", profile_msg(total));
21579 if (!prefer_self && profile_equal(total, self))
21580 fprintf(fd, " ");
21581 else
21582 fprintf(fd, "%s ", profile_msg(self));
21583 }
21584 else
21585 fprintf(fd, " ");
21586}
21587
21588/*
21589 * Compare function for total time sorting.
21590 */
21591 static int
21592#ifdef __BORLANDC__
21593_RTLENTRYF
21594#endif
21595prof_total_cmp(s1, s2)
21596 const void *s1;
21597 const void *s2;
21598{
21599 ufunc_T *p1, *p2;
21600
21601 p1 = *(ufunc_T **)s1;
21602 p2 = *(ufunc_T **)s2;
21603 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21604}
21605
21606/*
21607 * Compare function for self time sorting.
21608 */
21609 static int
21610#ifdef __BORLANDC__
21611_RTLENTRYF
21612#endif
21613prof_self_cmp(s1, s2)
21614 const void *s1;
21615 const void *s2;
21616{
21617 ufunc_T *p1, *p2;
21618
21619 p1 = *(ufunc_T **)s1;
21620 p2 = *(ufunc_T **)s2;
21621 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21622}
21623
Bram Moolenaar05159a02005-02-26 23:04:13 +000021624#endif
21625
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021626/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021627 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021628 * Return TRUE if a package was loaded.
21629 */
21630 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021631script_autoload(name, reload)
21632 char_u *name;
21633 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021634{
21635 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021636 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021637 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021638 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021639
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021640 /* Return quickly when autoload disabled. */
21641 if (no_autoload)
21642 return FALSE;
21643
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021644 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021645 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021646 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021647 return FALSE;
21648
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021649 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021650
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021651 /* Find the name in the list of previously loaded package names. Skip
21652 * "autoload/", it's always the same. */
21653 for (i = 0; i < ga_loaded.ga_len; ++i)
21654 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21655 break;
21656 if (!reload && i < ga_loaded.ga_len)
21657 ret = FALSE; /* was loaded already */
21658 else
21659 {
21660 /* Remember the name if it wasn't loaded already. */
21661 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21662 {
21663 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21664 tofree = NULL;
21665 }
21666
21667 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021668 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021669 ret = TRUE;
21670 }
21671
21672 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021673 return ret;
21674}
21675
21676/*
21677 * Return the autoload script name for a function or variable name.
21678 * Returns NULL when out of memory.
21679 */
21680 static char_u *
21681autoload_name(name)
21682 char_u *name;
21683{
21684 char_u *p;
21685 char_u *scriptname;
21686
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021687 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021688 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21689 if (scriptname == NULL)
21690 return FALSE;
21691 STRCPY(scriptname, "autoload/");
21692 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021693 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021694 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021695 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021696 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021697 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021698}
21699
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21701
21702/*
21703 * Function given to ExpandGeneric() to obtain the list of user defined
21704 * function names.
21705 */
21706 char_u *
21707get_user_func_name(xp, idx)
21708 expand_T *xp;
21709 int idx;
21710{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021711 static long_u done;
21712 static hashitem_T *hi;
21713 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714
21715 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021717 done = 0;
21718 hi = func_hashtab.ht_array;
21719 }
21720 if (done < func_hashtab.ht_used)
21721 {
21722 if (done++ > 0)
21723 ++hi;
21724 while (HASHITEM_EMPTY(hi))
21725 ++hi;
21726 fp = HI2UF(hi);
21727
21728 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21729 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730
21731 cat_func_name(IObuff, fp);
21732 if (xp->xp_context != EXPAND_USER_FUNC)
21733 {
21734 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021735 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 STRCAT(IObuff, ")");
21737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 return IObuff;
21739 }
21740 return NULL;
21741}
21742
21743#endif /* FEAT_CMDL_COMPL */
21744
21745/*
21746 * Copy the function name of "fp" to buffer "buf".
21747 * "buf" must be able to hold the function name plus three bytes.
21748 * Takes care of script-local function names.
21749 */
21750 static void
21751cat_func_name(buf, fp)
21752 char_u *buf;
21753 ufunc_T *fp;
21754{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021755 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 {
21757 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021758 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 }
21760 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021761 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762}
21763
21764/*
21765 * ":delfunction {name}"
21766 */
21767 void
21768ex_delfunction(eap)
21769 exarg_T *eap;
21770{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021771 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 char_u *p;
21773 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775
21776 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021777 name = trans_function_name(&p, eap->skip, 0, &fudi);
21778 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021780 {
21781 if (fudi.fd_dict != NULL && !eap->skip)
21782 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 if (!ends_excmd(*skipwhite(p)))
21786 {
21787 vim_free(name);
21788 EMSG(_(e_trailing));
21789 return;
21790 }
21791 eap->nextcmd = check_nextcmd(p);
21792 if (eap->nextcmd != NULL)
21793 *p = NUL;
21794
21795 if (!eap->skip)
21796 fp = find_func(name);
21797 vim_free(name);
21798
21799 if (!eap->skip)
21800 {
21801 if (fp == NULL)
21802 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021803 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 return;
21805 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021806 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807 {
21808 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21809 return;
21810 }
21811
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021812 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021814 /* Delete the dict item that refers to the function, it will
21815 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021816 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021818 else
21819 func_free(fp);
21820 }
21821}
21822
21823/*
21824 * Free a function and remove it from the list of functions.
21825 */
21826 static void
21827func_free(fp)
21828 ufunc_T *fp;
21829{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021830 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021831
21832 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021833 ga_clear_strings(&(fp->uf_args));
21834 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021835#ifdef FEAT_PROFILE
21836 vim_free(fp->uf_tml_count);
21837 vim_free(fp->uf_tml_total);
21838 vim_free(fp->uf_tml_self);
21839#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021840
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021841 /* remove the function from the function hashtable */
21842 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21843 if (HASHITEM_EMPTY(hi))
21844 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021845 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021846 hash_remove(&func_hashtab, hi);
21847
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021848 vim_free(fp);
21849}
21850
21851/*
21852 * Unreference a Function: decrement the reference count and free it when it
21853 * becomes zero. Only for numbered functions.
21854 */
21855 static void
21856func_unref(name)
21857 char_u *name;
21858{
21859 ufunc_T *fp;
21860
21861 if (name != NULL && isdigit(*name))
21862 {
21863 fp = find_func(name);
21864 if (fp == NULL)
21865 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021866 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021867 {
21868 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021869 * when "uf_calls" becomes zero. */
21870 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021871 func_free(fp);
21872 }
21873 }
21874}
21875
21876/*
21877 * Count a reference to a Function.
21878 */
21879 static void
21880func_ref(name)
21881 char_u *name;
21882{
21883 ufunc_T *fp;
21884
21885 if (name != NULL && isdigit(*name))
21886 {
21887 fp = find_func(name);
21888 if (fp == NULL)
21889 EMSG2(_(e_intern2), "func_ref()");
21890 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021891 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 }
21893}
21894
21895/*
21896 * Call a user function.
21897 */
21898 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021899call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 ufunc_T *fp; /* pointer to function */
21901 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021902 typval_T *argvars; /* arguments */
21903 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904 linenr_T firstline; /* first line of range */
21905 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907{
Bram Moolenaar33570922005-01-25 22:26:29 +000021908 char_u *save_sourcing_name;
21909 linenr_T save_sourcing_lnum;
21910 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021911 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 int save_did_emsg;
21913 static int depth = 0;
21914 dictitem_T *v;
21915 int fixvar_idx = 0; /* index in fixvar[] */
21916 int i;
21917 int ai;
21918 char_u numbuf[NUMBUFLEN];
21919 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021920#ifdef FEAT_PROFILE
21921 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021922 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924
21925 /* If depth of calling is getting too high, don't execute the function */
21926 if (depth >= p_mfd)
21927 {
21928 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021929 rettv->v_type = VAR_NUMBER;
21930 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 return;
21932 }
21933 ++depth;
21934
21935 line_breakcheck(); /* check for CTRL-C hit */
21936
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021937 fc = (funccall_T *)alloc(sizeof(funccall_T));
21938 fc->caller = current_funccal;
21939 current_funccal = fc;
21940 fc->func = fp;
21941 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021942 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021943 fc->linenr = 0;
21944 fc->returned = FALSE;
21945 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021947 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21948 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949
Bram Moolenaar33570922005-01-25 22:26:29 +000021950 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021951 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021952 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21953 * each argument variable and saves a lot of time.
21954 */
21955 /*
21956 * Init l: variables.
21957 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021958 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021959 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021960 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021961 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21962 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021963 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021964 name = v->di_key;
21965 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021967 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021968 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021969 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 v->di_tv.vval.v_dict = selfdict;
21971 ++selfdict->dv_refcount;
21972 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021973
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 /*
21975 * Init a: variables.
21976 * Set a:0 to "argcount".
21977 * Set a:000 to a list with room for the "..." arguments.
21978 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021979 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21980 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021981 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021982 /* Use "name" to avoid a warning from some compiler that checks the
21983 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021984 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021985 name = v->di_key;
21986 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021988 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021989 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021990 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021991 v->di_tv.vval.v_list = &fc->l_varlist;
21992 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21993 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21994 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021995
21996 /*
21997 * Set a:firstline to "firstline" and a:lastline to "lastline".
21998 * Set a:name to named arguments.
21999 * Set a:N to the "..." arguments.
22000 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022001 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022002 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022003 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022004 (varnumber_T)lastline);
22005 for (i = 0; i < argcount; ++i)
22006 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022007 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022008 if (ai < 0)
22009 /* named argument a:name */
22010 name = FUNCARG(fp, i);
22011 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022012 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022013 /* "..." argument a:1, a:2, etc. */
22014 sprintf((char *)numbuf, "%d", ai + 1);
22015 name = numbuf;
22016 }
22017 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22018 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022019 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022020 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22021 }
22022 else
22023 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022024 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22025 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022026 if (v == NULL)
22027 break;
22028 v->di_flags = DI_FLAGS_RO;
22029 }
22030 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022031 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022032
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022033 /* Note: the values are copied directly to avoid alloc/free.
22034 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022035 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022036 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022037
22038 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22039 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022040 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22041 fc->l_listitems[ai].li_tv = argvars[i];
22042 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022043 }
22044 }
22045
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 /* Don't redraw while executing the function. */
22047 ++RedrawingDisabled;
22048 save_sourcing_name = sourcing_name;
22049 save_sourcing_lnum = sourcing_lnum;
22050 sourcing_lnum = 1;
22051 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022052 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 if (sourcing_name != NULL)
22054 {
22055 if (save_sourcing_name != NULL
22056 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22057 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22058 else
22059 STRCPY(sourcing_name, "function ");
22060 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22061
22062 if (p_verbose >= 12)
22063 {
22064 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022065 verbose_enter_scroll();
22066
Bram Moolenaar555b2802005-05-19 21:08:39 +000022067 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 if (p_verbose >= 14)
22069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022071 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022072 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022073 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074
22075 msg_puts((char_u *)"(");
22076 for (i = 0; i < argcount; ++i)
22077 {
22078 if (i > 0)
22079 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022080 if (argvars[i].v_type == VAR_NUMBER)
22081 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 else
22083 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022084 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22085 if (s != NULL)
22086 {
22087 trunc_string(s, buf, MSG_BUF_CLEN);
22088 msg_puts(buf);
22089 vim_free(tofree);
22090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 }
22092 }
22093 msg_puts((char_u *)")");
22094 }
22095 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022096
22097 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 --no_wait_return;
22099 }
22100 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022101#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022102 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022103 {
22104 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22105 func_do_profile(fp);
22106 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022107 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022108 {
22109 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022110 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022111 profile_zero(&fp->uf_tm_children);
22112 }
22113 script_prof_save(&wait_start);
22114 }
22115#endif
22116
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022118 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 save_did_emsg = did_emsg;
22120 did_emsg = FALSE;
22121
22122 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022123 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22125
22126 --RedrawingDisabled;
22127
22128 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022129 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022131 clear_tv(rettv);
22132 rettv->v_type = VAR_NUMBER;
22133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134 }
22135
Bram Moolenaar05159a02005-02-26 23:04:13 +000022136#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022137 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022138 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022139 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022140 profile_end(&call_start);
22141 profile_sub_wait(&wait_start, &call_start);
22142 profile_add(&fp->uf_tm_total, &call_start);
22143 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022144 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022145 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022146 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22147 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022148 }
22149 }
22150#endif
22151
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 /* when being verbose, mention the return value */
22153 if (p_verbose >= 12)
22154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022156 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022159 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022160 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022161 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022162 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022165 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022166 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022167 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022168 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022169
Bram Moolenaar555b2802005-05-19 21:08:39 +000022170 /* The value may be very long. Skip the middle part, so that we
22171 * have some idea how it starts and ends. smsg() would always
22172 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022173 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022174 if (s != NULL)
22175 {
22176 trunc_string(s, buf, MSG_BUF_CLEN);
22177 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22178 vim_free(tofree);
22179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 }
22181 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022182
22183 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 --no_wait_return;
22185 }
22186
22187 vim_free(sourcing_name);
22188 sourcing_name = save_sourcing_name;
22189 sourcing_lnum = save_sourcing_lnum;
22190 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022191#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022192 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022193 script_prof_restore(&wait_start);
22194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195
22196 if (p_verbose >= 12 && sourcing_name != NULL)
22197 {
22198 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022199 verbose_enter_scroll();
22200
Bram Moolenaar555b2802005-05-19 21:08:39 +000022201 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022203
22204 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 --no_wait_return;
22206 }
22207
22208 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022209 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022211
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022212 /* If the a:000 list and the l: and a: dicts are not referenced we can
22213 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022214 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22215 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22216 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22217 {
22218 free_funccal(fc, FALSE);
22219 }
22220 else
22221 {
22222 hashitem_T *hi;
22223 listitem_T *li;
22224 int todo;
22225
22226 /* "fc" is still in use. This can happen when returning "a:000" or
22227 * assigning "l:" to a global variable.
22228 * Link "fc" in the list for garbage collection later. */
22229 fc->caller = previous_funccal;
22230 previous_funccal = fc;
22231
22232 /* Make a copy of the a: variables, since we didn't do that above. */
22233 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22234 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22235 {
22236 if (!HASHITEM_EMPTY(hi))
22237 {
22238 --todo;
22239 v = HI2DI(hi);
22240 copy_tv(&v->di_tv, &v->di_tv);
22241 }
22242 }
22243
22244 /* Make a copy of the a:000 items, since we didn't do that above. */
22245 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22246 copy_tv(&li->li_tv, &li->li_tv);
22247 }
22248}
22249
22250/*
22251 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022252 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022253 */
22254 static int
22255can_free_funccal(fc, copyID)
22256 funccall_T *fc;
22257 int copyID;
22258{
22259 return (fc->l_varlist.lv_copyID != copyID
22260 && fc->l_vars.dv_copyID != copyID
22261 && fc->l_avars.dv_copyID != copyID);
22262}
22263
22264/*
22265 * Free "fc" and what it contains.
22266 */
22267 static void
22268free_funccal(fc, free_val)
22269 funccall_T *fc;
22270 int free_val; /* a: vars were allocated */
22271{
22272 listitem_T *li;
22273
22274 /* The a: variables typevals may not have been allocated, only free the
22275 * allocated variables. */
22276 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22277
22278 /* free all l: variables */
22279 vars_clear(&fc->l_vars.dv_hashtab);
22280
22281 /* Free the a:000 variables if they were allocated. */
22282 if (free_val)
22283 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22284 clear_tv(&li->li_tv);
22285
22286 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287}
22288
22289/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022290 * Add a number variable "name" to dict "dp" with value "nr".
22291 */
22292 static void
22293add_nr_var(dp, v, name, nr)
22294 dict_T *dp;
22295 dictitem_T *v;
22296 char *name;
22297 varnumber_T nr;
22298{
22299 STRCPY(v->di_key, name);
22300 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22301 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22302 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022303 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022304 v->di_tv.vval.v_number = nr;
22305}
22306
22307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 * ":return [expr]"
22309 */
22310 void
22311ex_return(eap)
22312 exarg_T *eap;
22313{
22314 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022315 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 int returning = FALSE;
22317
22318 if (current_funccal == NULL)
22319 {
22320 EMSG(_("E133: :return not inside a function"));
22321 return;
22322 }
22323
22324 if (eap->skip)
22325 ++emsg_skip;
22326
22327 eap->nextcmd = NULL;
22328 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022329 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 {
22331 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022332 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022334 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 }
22336 /* It's safer to return also on error. */
22337 else if (!eap->skip)
22338 {
22339 /*
22340 * Return unless the expression evaluation has been cancelled due to an
22341 * aborting error, an interrupt, or an exception.
22342 */
22343 if (!aborting())
22344 returning = do_return(eap, FALSE, TRUE, NULL);
22345 }
22346
22347 /* When skipping or the return gets pending, advance to the next command
22348 * in this line (!returning). Otherwise, ignore the rest of the line.
22349 * Following lines will be ignored by get_func_line(). */
22350 if (returning)
22351 eap->nextcmd = NULL;
22352 else if (eap->nextcmd == NULL) /* no argument */
22353 eap->nextcmd = check_nextcmd(arg);
22354
22355 if (eap->skip)
22356 --emsg_skip;
22357}
22358
22359/*
22360 * Return from a function. Possibly makes the return pending. Also called
22361 * for a pending return at the ":endtry" or after returning from an extra
22362 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022363 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022364 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 * FALSE when the return gets pending.
22366 */
22367 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022368do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 exarg_T *eap;
22370 int reanimate;
22371 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022372 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373{
22374 int idx;
22375 struct condstack *cstack = eap->cstack;
22376
22377 if (reanimate)
22378 /* Undo the return. */
22379 current_funccal->returned = FALSE;
22380
22381 /*
22382 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22383 * not in its finally clause (which then is to be executed next) is found.
22384 * In this case, make the ":return" pending for execution at the ":endtry".
22385 * Otherwise, return normally.
22386 */
22387 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22388 if (idx >= 0)
22389 {
22390 cstack->cs_pending[idx] = CSTP_RETURN;
22391
22392 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022393 /* A pending return again gets pending. "rettv" points to an
22394 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022396 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 else
22398 {
22399 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022400 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022402 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022404 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 {
22406 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022407 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022408 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 else
22410 EMSG(_(e_outofmem));
22411 }
22412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022413 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414
22415 if (reanimate)
22416 {
22417 /* The pending return value could be overwritten by a ":return"
22418 * without argument in a finally clause; reset the default
22419 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022420 current_funccal->rettv->v_type = VAR_NUMBER;
22421 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 }
22423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022424 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 }
22426 else
22427 {
22428 current_funccal->returned = TRUE;
22429
22430 /* If the return is carried out now, store the return value. For
22431 * a return immediately after reanimation, the value is already
22432 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022433 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022435 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022436 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022438 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 }
22440 }
22441
22442 return idx < 0;
22443}
22444
22445/*
22446 * Free the variable with a pending return value.
22447 */
22448 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022449discard_pending_return(rettv)
22450 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451{
Bram Moolenaar33570922005-01-25 22:26:29 +000022452 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453}
22454
22455/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022456 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 * is an allocated string. Used by report_pending() for verbose messages.
22458 */
22459 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022460get_return_cmd(rettv)
22461 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022463 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022464 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022465 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022467 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022468 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022469 if (s == NULL)
22470 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022471
22472 STRCPY(IObuff, ":return ");
22473 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22474 if (STRLEN(s) + 8 >= IOSIZE)
22475 STRCPY(IObuff + IOSIZE - 4, "...");
22476 vim_free(tofree);
22477 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478}
22479
22480/*
22481 * Get next function line.
22482 * Called by do_cmdline() to get the next line.
22483 * Returns allocated string, or NULL for end of function.
22484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 char_u *
22486get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022487 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022489 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490{
Bram Moolenaar33570922005-01-25 22:26:29 +000022491 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022492 ufunc_T *fp = fcp->func;
22493 char_u *retval;
22494 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495
22496 /* If breakpoints have been added/deleted need to check for it. */
22497 if (fcp->dbg_tick != debug_tick)
22498 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022499 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 sourcing_lnum);
22501 fcp->dbg_tick = debug_tick;
22502 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022503#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022504 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022505 func_line_end(cookie);
22506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507
Bram Moolenaar05159a02005-02-26 23:04:13 +000022508 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022509 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22510 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 retval = NULL;
22512 else
22513 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022514 /* Skip NULL lines (continuation lines). */
22515 while (fcp->linenr < gap->ga_len
22516 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22517 ++fcp->linenr;
22518 if (fcp->linenr >= gap->ga_len)
22519 retval = NULL;
22520 else
22521 {
22522 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22523 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022524#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022525 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022526 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022527#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 }
22530
22531 /* Did we encounter a breakpoint? */
22532 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22533 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022534 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022536 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 sourcing_lnum);
22538 fcp->dbg_tick = debug_tick;
22539 }
22540
22541 return retval;
22542}
22543
Bram Moolenaar05159a02005-02-26 23:04:13 +000022544#if defined(FEAT_PROFILE) || defined(PROTO)
22545/*
22546 * Called when starting to read a function line.
22547 * "sourcing_lnum" must be correct!
22548 * When skipping lines it may not actually be executed, but we won't find out
22549 * until later and we need to store the time now.
22550 */
22551 void
22552func_line_start(cookie)
22553 void *cookie;
22554{
22555 funccall_T *fcp = (funccall_T *)cookie;
22556 ufunc_T *fp = fcp->func;
22557
22558 if (fp->uf_profiling && sourcing_lnum >= 1
22559 && sourcing_lnum <= fp->uf_lines.ga_len)
22560 {
22561 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022562 /* Skip continuation lines. */
22563 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22564 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022565 fp->uf_tml_execed = FALSE;
22566 profile_start(&fp->uf_tml_start);
22567 profile_zero(&fp->uf_tml_children);
22568 profile_get_wait(&fp->uf_tml_wait);
22569 }
22570}
22571
22572/*
22573 * Called when actually executing a function line.
22574 */
22575 void
22576func_line_exec(cookie)
22577 void *cookie;
22578{
22579 funccall_T *fcp = (funccall_T *)cookie;
22580 ufunc_T *fp = fcp->func;
22581
22582 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22583 fp->uf_tml_execed = TRUE;
22584}
22585
22586/*
22587 * Called when done with a function line.
22588 */
22589 void
22590func_line_end(cookie)
22591 void *cookie;
22592{
22593 funccall_T *fcp = (funccall_T *)cookie;
22594 ufunc_T *fp = fcp->func;
22595
22596 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22597 {
22598 if (fp->uf_tml_execed)
22599 {
22600 ++fp->uf_tml_count[fp->uf_tml_idx];
22601 profile_end(&fp->uf_tml_start);
22602 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022603 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022604 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22605 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022606 }
22607 fp->uf_tml_idx = -1;
22608 }
22609}
22610#endif
22611
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612/*
22613 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022614 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 */
22616 int
22617func_has_ended(cookie)
22618 void *cookie;
22619{
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621
22622 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22623 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022624 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625 || fcp->returned);
22626}
22627
22628/*
22629 * return TRUE if cookie indicates a function which "abort"s on errors.
22630 */
22631 int
22632func_has_abort(cookie)
22633 void *cookie;
22634{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022635 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636}
22637
22638#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22639typedef enum
22640{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022641 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22642 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22643 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644} var_flavour_T;
22645
22646static var_flavour_T var_flavour __ARGS((char_u *varname));
22647
22648 static var_flavour_T
22649var_flavour(varname)
22650 char_u *varname;
22651{
22652 char_u *p = varname;
22653
22654 if (ASCII_ISUPPER(*p))
22655 {
22656 while (*(++p))
22657 if (ASCII_ISLOWER(*p))
22658 return VAR_FLAVOUR_SESSION;
22659 return VAR_FLAVOUR_VIMINFO;
22660 }
22661 else
22662 return VAR_FLAVOUR_DEFAULT;
22663}
22664#endif
22665
22666#if defined(FEAT_VIMINFO) || defined(PROTO)
22667/*
22668 * Restore global vars that start with a capital from the viminfo file
22669 */
22670 int
22671read_viminfo_varlist(virp, writing)
22672 vir_T *virp;
22673 int writing;
22674{
22675 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022676 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022677 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678
22679 if (!writing && (find_viminfo_parameter('!') != NULL))
22680 {
22681 tab = vim_strchr(virp->vir_line + 1, '\t');
22682 if (tab != NULL)
22683 {
22684 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022685 switch (*tab)
22686 {
22687 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022688#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022689 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022690#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022691 case 'D': type = VAR_DICT; break;
22692 case 'L': type = VAR_LIST; break;
22693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694
22695 tab = vim_strchr(tab, '\t');
22696 if (tab != NULL)
22697 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022698 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022699 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022700 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022702#ifdef FEAT_FLOAT
22703 else if (type == VAR_FLOAT)
22704 (void)string2float(tab + 1, &tv.vval.v_float);
22705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022707 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022708 if (type == VAR_DICT || type == VAR_LIST)
22709 {
22710 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22711
22712 if (etv == NULL)
22713 /* Failed to parse back the dict or list, use it as a
22714 * string. */
22715 tv.v_type = VAR_STRING;
22716 else
22717 {
22718 vim_free(tv.vval.v_string);
22719 tv = *etv;
22720 }
22721 }
22722
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022723 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022724
22725 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022726 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022727 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22728 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 }
22730 }
22731 }
22732
22733 return viminfo_readline(virp);
22734}
22735
22736/*
22737 * Write global vars that start with a capital to the viminfo file
22738 */
22739 void
22740write_viminfo_varlist(fp)
22741 FILE *fp;
22742{
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 hashitem_T *hi;
22744 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022745 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022746 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022747 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022748 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022749 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750
22751 if (find_viminfo_parameter('!') == NULL)
22752 return;
22753
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022754 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022755
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022756 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022759 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022761 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022762 this_var = HI2DI(hi);
22763 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022764 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022765 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022766 {
22767 case VAR_STRING: s = "STR"; break;
22768 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022769#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022770 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022771#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022772 case VAR_DICT: s = "DIC"; break;
22773 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022774 default: continue;
22775 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022776 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022777 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022778 if (p != NULL)
22779 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022780 vim_free(tofree);
22781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 }
22783 }
22784}
22785#endif
22786
22787#if defined(FEAT_SESSION) || defined(PROTO)
22788 int
22789store_session_globals(fd)
22790 FILE *fd;
22791{
Bram Moolenaar33570922005-01-25 22:26:29 +000022792 hashitem_T *hi;
22793 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022794 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 char_u *p, *t;
22796
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022797 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022798 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022800 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022802 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022803 this_var = HI2DI(hi);
22804 if ((this_var->di_tv.v_type == VAR_NUMBER
22805 || this_var->di_tv.v_type == VAR_STRING)
22806 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022807 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022808 /* Escape special characters with a backslash. Turn a LF and
22809 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022810 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022811 (char_u *)"\\\"\n\r");
22812 if (p == NULL) /* out of memory */
22813 break;
22814 for (t = p; *t != NUL; ++t)
22815 if (*t == '\n')
22816 *t = 'n';
22817 else if (*t == '\r')
22818 *t = 'r';
22819 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022820 this_var->di_key,
22821 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22822 : ' ',
22823 p,
22824 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22825 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022826 || put_eol(fd) == FAIL)
22827 {
22828 vim_free(p);
22829 return FAIL;
22830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 vim_free(p);
22832 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022833#ifdef FEAT_FLOAT
22834 else if (this_var->di_tv.v_type == VAR_FLOAT
22835 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22836 {
22837 float_T f = this_var->di_tv.vval.v_float;
22838 int sign = ' ';
22839
22840 if (f < 0)
22841 {
22842 f = -f;
22843 sign = '-';
22844 }
22845 if ((fprintf(fd, "let %s = %c&%f",
22846 this_var->di_key, sign, f) < 0)
22847 || put_eol(fd) == FAIL)
22848 return FAIL;
22849 }
22850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 }
22852 }
22853 return OK;
22854}
22855#endif
22856
Bram Moolenaar661b1822005-07-28 22:36:45 +000022857/*
22858 * Display script name where an item was last set.
22859 * Should only be invoked when 'verbose' is non-zero.
22860 */
22861 void
22862last_set_msg(scriptID)
22863 scid_T scriptID;
22864{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022865 char_u *p;
22866
Bram Moolenaar661b1822005-07-28 22:36:45 +000022867 if (scriptID != 0)
22868 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022869 p = home_replace_save(NULL, get_scriptname(scriptID));
22870 if (p != NULL)
22871 {
22872 verbose_enter();
22873 MSG_PUTS(_("\n\tLast set from "));
22874 MSG_PUTS(p);
22875 vim_free(p);
22876 verbose_leave();
22877 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022878 }
22879}
22880
Bram Moolenaard812df62008-11-09 12:46:09 +000022881/*
22882 * List v:oldfiles in a nice way.
22883 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022884 void
22885ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022886 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022887{
22888 list_T *l = vimvars[VV_OLDFILES].vv_list;
22889 listitem_T *li;
22890 int nr = 0;
22891
22892 if (l == NULL)
22893 msg((char_u *)_("No old files"));
22894 else
22895 {
22896 msg_start();
22897 msg_scroll = TRUE;
22898 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22899 {
22900 msg_outnum((long)++nr);
22901 MSG_PUTS(": ");
22902 msg_outtrans(get_tv_string(&li->li_tv));
22903 msg_putchar('\n');
22904 out_flush(); /* output one line at a time */
22905 ui_breakcheck();
22906 }
22907 /* Assume "got_int" was set to truncate the listing. */
22908 got_int = FALSE;
22909
22910#ifdef FEAT_BROWSE_CMD
22911 if (cmdmod.browse)
22912 {
22913 quit_more = FALSE;
22914 nr = prompt_for_number(FALSE);
22915 msg_starthere();
22916 if (nr > 0)
22917 {
22918 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22919 (long)nr);
22920
22921 if (p != NULL)
22922 {
22923 p = expand_env_save(p);
22924 eap->arg = p;
22925 eap->cmdidx = CMD_edit;
22926 cmdmod.browse = FALSE;
22927 do_exedit(eap, NULL);
22928 vim_free(p);
22929 }
22930 }
22931 }
22932#endif
22933 }
22934}
22935
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936#endif /* FEAT_EVAL */
22937
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022939#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940
22941#ifdef WIN3264
22942/*
22943 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22944 */
22945static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22946static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22947static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22948
22949/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022950 * Get the short path (8.3) for the filename in "fnamep".
22951 * Only works for a valid file name.
22952 * When the path gets longer "fnamep" is changed and the allocated buffer
22953 * is put in "bufp".
22954 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22955 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956 */
22957 static int
22958get_short_pathname(fnamep, bufp, fnamelen)
22959 char_u **fnamep;
22960 char_u **bufp;
22961 int *fnamelen;
22962{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022963 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 char_u *newbuf;
22965
22966 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 l = GetShortPathName(*fnamep, *fnamep, len);
22968 if (l > len - 1)
22969 {
22970 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022971 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 newbuf = vim_strnsave(*fnamep, l);
22973 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022974 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975
22976 vim_free(*bufp);
22977 *fnamep = *bufp = newbuf;
22978
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022979 /* Really should always succeed, as the buffer is big enough. */
22980 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 }
22982
22983 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022984 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985}
22986
22987/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022988 * Get the short path (8.3) for the filename in "fname". The converted
22989 * path is returned in "bufp".
22990 *
22991 * Some of the directories specified in "fname" may not exist. This function
22992 * will shorten the existing directories at the beginning of the path and then
22993 * append the remaining non-existing path.
22994 *
22995 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022996 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022997 * bufp - Pointer to an allocated buffer for the filename.
22998 * fnamelen - Length of the filename pointed to by fname
22999 *
23000 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 */
23002 static int
23003shortpath_for_invalid_fname(fname, bufp, fnamelen)
23004 char_u **fname;
23005 char_u **bufp;
23006 int *fnamelen;
23007{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023008 char_u *short_fname, *save_fname, *pbuf_unused;
23009 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023011 int old_len, len;
23012 int new_len, sfx_len;
23013 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014
23015 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023016 old_len = *fnamelen;
23017 save_fname = vim_strnsave(*fname, old_len);
23018 pbuf_unused = NULL;
23019 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023021 endp = save_fname + old_len - 1; /* Find the end of the copy */
23022 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023024 /*
23025 * Try shortening the supplied path till it succeeds by removing one
23026 * directory at a time from the tail of the path.
23027 */
23028 len = 0;
23029 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023031 /* go back one path-separator */
23032 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23033 --endp;
23034 if (endp <= save_fname)
23035 break; /* processed the complete path */
23036
23037 /*
23038 * Replace the path separator with a NUL and try to shorten the
23039 * resulting path.
23040 */
23041 ch = *endp;
23042 *endp = 0;
23043 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023044 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023045 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23046 {
23047 retval = FAIL;
23048 goto theend;
23049 }
23050 *endp = ch; /* preserve the string */
23051
23052 if (len > 0)
23053 break; /* successfully shortened the path */
23054
23055 /* failed to shorten the path. Skip the path separator */
23056 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 }
23058
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023059 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023061 /*
23062 * Succeeded in shortening the path. Now concatenate the shortened
23063 * path with the remaining path at the tail.
23064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023066 /* Compute the length of the new path. */
23067 sfx_len = (int)(save_endp - endp) + 1;
23068 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023070 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023072 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023074 /* There is not enough space in the currently allocated string,
23075 * copy it to a buffer big enough. */
23076 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023078 {
23079 retval = FAIL;
23080 goto theend;
23081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 }
23083 else
23084 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023085 /* Transfer short_fname to the main buffer (it's big enough),
23086 * unless get_short_pathname() did its work in-place. */
23087 *fname = *bufp = save_fname;
23088 if (short_fname != save_fname)
23089 vim_strncpy(save_fname, short_fname, len);
23090 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023092
23093 /* concat the not-shortened part of the path */
23094 vim_strncpy(*fname + len, endp, sfx_len);
23095 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023097
23098theend:
23099 vim_free(pbuf_unused);
23100 vim_free(save_fname);
23101
23102 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103}
23104
23105/*
23106 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023107 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 */
23109 static int
23110shortpath_for_partial(fnamep, bufp, fnamelen)
23111 char_u **fnamep;
23112 char_u **bufp;
23113 int *fnamelen;
23114{
23115 int sepcount, len, tflen;
23116 char_u *p;
23117 char_u *pbuf, *tfname;
23118 int hasTilde;
23119
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023120 /* Count up the path separators from the RHS.. so we know which part
23121 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023123 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 if (vim_ispathsep(*p))
23125 ++sepcount;
23126
23127 /* Need full path first (use expand_env() to remove a "~/") */
23128 hasTilde = (**fnamep == '~');
23129 if (hasTilde)
23130 pbuf = tfname = expand_env_save(*fnamep);
23131 else
23132 pbuf = tfname = FullName_save(*fnamep, FALSE);
23133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023134 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023136 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23137 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138
23139 if (len == 0)
23140 {
23141 /* Don't have a valid filename, so shorten the rest of the
23142 * path if we can. This CAN give us invalid 8.3 filenames, but
23143 * there's not a lot of point in guessing what it might be.
23144 */
23145 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023146 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 }
23149
23150 /* Count the paths backward to find the beginning of the desired string. */
23151 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023152 {
23153#ifdef FEAT_MBYTE
23154 if (has_mbyte)
23155 p -= mb_head_off(tfname, p);
23156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 if (vim_ispathsep(*p))
23158 {
23159 if (sepcount == 0 || (hasTilde && sepcount == 1))
23160 break;
23161 else
23162 sepcount --;
23163 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 if (hasTilde)
23166 {
23167 --p;
23168 if (p >= tfname)
23169 *p = '~';
23170 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 }
23173 else
23174 ++p;
23175
23176 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23177 vim_free(*bufp);
23178 *fnamelen = (int)STRLEN(p);
23179 *bufp = pbuf;
23180 *fnamep = p;
23181
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023182 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183}
23184#endif /* WIN3264 */
23185
23186/*
23187 * Adjust a filename, according to a string of modifiers.
23188 * *fnamep must be NUL terminated when called. When returning, the length is
23189 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023190 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 * When there is an error, *fnamep is set to NULL.
23192 */
23193 int
23194modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23195 char_u *src; /* string with modifiers */
23196 int *usedlen; /* characters after src that are used */
23197 char_u **fnamep; /* file name so far */
23198 char_u **bufp; /* buffer for allocated file name or NULL */
23199 int *fnamelen; /* length of fnamep */
23200{
23201 int valid = 0;
23202 char_u *tail;
23203 char_u *s, *p, *pbuf;
23204 char_u dirname[MAXPATHL];
23205 int c;
23206 int has_fullname = 0;
23207#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023208 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 int has_shortname = 0;
23210#endif
23211
23212repeat:
23213 /* ":p" - full path/file_name */
23214 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23215 {
23216 has_fullname = 1;
23217
23218 valid |= VALID_PATH;
23219 *usedlen += 2;
23220
23221 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23222 if ((*fnamep)[0] == '~'
23223#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23224 && ((*fnamep)[1] == '/'
23225# ifdef BACKSLASH_IN_FILENAME
23226 || (*fnamep)[1] == '\\'
23227# endif
23228 || (*fnamep)[1] == NUL)
23229
23230#endif
23231 )
23232 {
23233 *fnamep = expand_env_save(*fnamep);
23234 vim_free(*bufp); /* free any allocated file name */
23235 *bufp = *fnamep;
23236 if (*fnamep == NULL)
23237 return -1;
23238 }
23239
23240 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023241 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 {
23243 if (vim_ispathsep(*p)
23244 && p[1] == '.'
23245 && (p[2] == NUL
23246 || vim_ispathsep(p[2])
23247 || (p[2] == '.'
23248 && (p[3] == NUL || vim_ispathsep(p[3])))))
23249 break;
23250 }
23251
23252 /* FullName_save() is slow, don't use it when not needed. */
23253 if (*p != NUL || !vim_isAbsName(*fnamep))
23254 {
23255 *fnamep = FullName_save(*fnamep, *p != NUL);
23256 vim_free(*bufp); /* free any allocated file name */
23257 *bufp = *fnamep;
23258 if (*fnamep == NULL)
23259 return -1;
23260 }
23261
23262 /* Append a path separator to a directory. */
23263 if (mch_isdir(*fnamep))
23264 {
23265 /* Make room for one or two extra characters. */
23266 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23267 vim_free(*bufp); /* free any allocated file name */
23268 *bufp = *fnamep;
23269 if (*fnamep == NULL)
23270 return -1;
23271 add_pathsep(*fnamep);
23272 }
23273 }
23274
23275 /* ":." - path relative to the current directory */
23276 /* ":~" - path relative to the home directory */
23277 /* ":8" - shortname path - postponed till after */
23278 while (src[*usedlen] == ':'
23279 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23280 {
23281 *usedlen += 2;
23282 if (c == '8')
23283 {
23284#ifdef WIN3264
23285 has_shortname = 1; /* Postpone this. */
23286#endif
23287 continue;
23288 }
23289 pbuf = NULL;
23290 /* Need full path first (use expand_env() to remove a "~/") */
23291 if (!has_fullname)
23292 {
23293 if (c == '.' && **fnamep == '~')
23294 p = pbuf = expand_env_save(*fnamep);
23295 else
23296 p = pbuf = FullName_save(*fnamep, FALSE);
23297 }
23298 else
23299 p = *fnamep;
23300
23301 has_fullname = 0;
23302
23303 if (p != NULL)
23304 {
23305 if (c == '.')
23306 {
23307 mch_dirname(dirname, MAXPATHL);
23308 s = shorten_fname(p, dirname);
23309 if (s != NULL)
23310 {
23311 *fnamep = s;
23312 if (pbuf != NULL)
23313 {
23314 vim_free(*bufp); /* free any allocated file name */
23315 *bufp = pbuf;
23316 pbuf = NULL;
23317 }
23318 }
23319 }
23320 else
23321 {
23322 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23323 /* Only replace it when it starts with '~' */
23324 if (*dirname == '~')
23325 {
23326 s = vim_strsave(dirname);
23327 if (s != NULL)
23328 {
23329 *fnamep = s;
23330 vim_free(*bufp);
23331 *bufp = s;
23332 }
23333 }
23334 }
23335 vim_free(pbuf);
23336 }
23337 }
23338
23339 tail = gettail(*fnamep);
23340 *fnamelen = (int)STRLEN(*fnamep);
23341
23342 /* ":h" - head, remove "/file_name", can be repeated */
23343 /* Don't remove the first "/" or "c:\" */
23344 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23345 {
23346 valid |= VALID_HEAD;
23347 *usedlen += 2;
23348 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023349 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023350 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 *fnamelen = (int)(tail - *fnamep);
23352#ifdef VMS
23353 if (*fnamelen > 0)
23354 *fnamelen += 1; /* the path separator is part of the path */
23355#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023356 if (*fnamelen == 0)
23357 {
23358 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23359 p = vim_strsave((char_u *)".");
23360 if (p == NULL)
23361 return -1;
23362 vim_free(*bufp);
23363 *bufp = *fnamep = tail = p;
23364 *fnamelen = 1;
23365 }
23366 else
23367 {
23368 while (tail > s && !after_pathsep(s, tail))
23369 mb_ptr_back(*fnamep, tail);
23370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 }
23372
23373 /* ":8" - shortname */
23374 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23375 {
23376 *usedlen += 2;
23377#ifdef WIN3264
23378 has_shortname = 1;
23379#endif
23380 }
23381
23382#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023383 /*
23384 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385 */
23386 if (has_shortname)
23387 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023388 /* Copy the string if it is shortened by :h and when it wasn't copied
23389 * yet, because we are going to change it in place. Avoids changing
23390 * the buffer name for "%:8". */
23391 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 {
23393 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023394 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 return -1;
23396 vim_free(*bufp);
23397 *bufp = *fnamep = p;
23398 }
23399
23400 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023401 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 if (!has_fullname && !vim_isAbsName(*fnamep))
23403 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023404 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 return -1;
23406 }
23407 else
23408 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023409 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410
Bram Moolenaardc935552011-08-17 15:23:23 +020023411 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023413 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 return -1;
23415
23416 if (l == 0)
23417 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023418 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023420 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 return -1;
23422 }
23423 *fnamelen = l;
23424 }
23425 }
23426#endif /* WIN3264 */
23427
23428 /* ":t" - tail, just the basename */
23429 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23430 {
23431 *usedlen += 2;
23432 *fnamelen -= (int)(tail - *fnamep);
23433 *fnamep = tail;
23434 }
23435
23436 /* ":e" - extension, can be repeated */
23437 /* ":r" - root, without extension, can be repeated */
23438 while (src[*usedlen] == ':'
23439 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23440 {
23441 /* find a '.' in the tail:
23442 * - for second :e: before the current fname
23443 * - otherwise: The last '.'
23444 */
23445 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23446 s = *fnamep - 2;
23447 else
23448 s = *fnamep + *fnamelen - 1;
23449 for ( ; s > tail; --s)
23450 if (s[0] == '.')
23451 break;
23452 if (src[*usedlen + 1] == 'e') /* :e */
23453 {
23454 if (s > tail)
23455 {
23456 *fnamelen += (int)(*fnamep - (s + 1));
23457 *fnamep = s + 1;
23458#ifdef VMS
23459 /* cut version from the extension */
23460 s = *fnamep + *fnamelen - 1;
23461 for ( ; s > *fnamep; --s)
23462 if (s[0] == ';')
23463 break;
23464 if (s > *fnamep)
23465 *fnamelen = s - *fnamep;
23466#endif
23467 }
23468 else if (*fnamep <= tail)
23469 *fnamelen = 0;
23470 }
23471 else /* :r */
23472 {
23473 if (s > tail) /* remove one extension */
23474 *fnamelen = (int)(s - *fnamep);
23475 }
23476 *usedlen += 2;
23477 }
23478
23479 /* ":s?pat?foo?" - substitute */
23480 /* ":gs?pat?foo?" - global substitute */
23481 if (src[*usedlen] == ':'
23482 && (src[*usedlen + 1] == 's'
23483 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23484 {
23485 char_u *str;
23486 char_u *pat;
23487 char_u *sub;
23488 int sep;
23489 char_u *flags;
23490 int didit = FALSE;
23491
23492 flags = (char_u *)"";
23493 s = src + *usedlen + 2;
23494 if (src[*usedlen + 1] == 'g')
23495 {
23496 flags = (char_u *)"g";
23497 ++s;
23498 }
23499
23500 sep = *s++;
23501 if (sep)
23502 {
23503 /* find end of pattern */
23504 p = vim_strchr(s, sep);
23505 if (p != NULL)
23506 {
23507 pat = vim_strnsave(s, (int)(p - s));
23508 if (pat != NULL)
23509 {
23510 s = p + 1;
23511 /* find end of substitution */
23512 p = vim_strchr(s, sep);
23513 if (p != NULL)
23514 {
23515 sub = vim_strnsave(s, (int)(p - s));
23516 str = vim_strnsave(*fnamep, *fnamelen);
23517 if (sub != NULL && str != NULL)
23518 {
23519 *usedlen = (int)(p + 1 - src);
23520 s = do_string_sub(str, pat, sub, flags);
23521 if (s != NULL)
23522 {
23523 *fnamep = s;
23524 *fnamelen = (int)STRLEN(s);
23525 vim_free(*bufp);
23526 *bufp = s;
23527 didit = TRUE;
23528 }
23529 }
23530 vim_free(sub);
23531 vim_free(str);
23532 }
23533 vim_free(pat);
23534 }
23535 }
23536 /* after using ":s", repeat all the modifiers */
23537 if (didit)
23538 goto repeat;
23539 }
23540 }
23541
23542 return valid;
23543}
23544
23545/*
23546 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23547 * "flags" can be "g" to do a global substitute.
23548 * Returns an allocated string, NULL for error.
23549 */
23550 char_u *
23551do_string_sub(str, pat, sub, flags)
23552 char_u *str;
23553 char_u *pat;
23554 char_u *sub;
23555 char_u *flags;
23556{
23557 int sublen;
23558 regmatch_T regmatch;
23559 int i;
23560 int do_all;
23561 char_u *tail;
23562 garray_T ga;
23563 char_u *ret;
23564 char_u *save_cpo;
23565
23566 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23567 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023568 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569
23570 ga_init2(&ga, 1, 200);
23571
23572 do_all = (flags[0] == 'g');
23573
23574 regmatch.rm_ic = p_ic;
23575 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23576 if (regmatch.regprog != NULL)
23577 {
23578 tail = str;
23579 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23580 {
23581 /*
23582 * Get some space for a temporary buffer to do the substitution
23583 * into. It will contain:
23584 * - The text up to where the match is.
23585 * - The substituted text.
23586 * - The text after the match.
23587 */
23588 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23589 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23590 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23591 {
23592 ga_clear(&ga);
23593 break;
23594 }
23595
23596 /* copy the text up to where the match is */
23597 i = (int)(regmatch.startp[0] - tail);
23598 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23599 /* add the substituted text */
23600 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23601 + ga.ga_len + i, TRUE, TRUE, FALSE);
23602 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 /* avoid getting stuck on a match with an empty string */
23604 if (tail == regmatch.endp[0])
23605 {
23606 if (*tail == NUL)
23607 break;
23608 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23609 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 }
23611 else
23612 {
23613 tail = regmatch.endp[0];
23614 if (*tail == NUL)
23615 break;
23616 }
23617 if (!do_all)
23618 break;
23619 }
23620
23621 if (ga.ga_data != NULL)
23622 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23623
23624 vim_free(regmatch.regprog);
23625 }
23626
23627 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23628 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023629 if (p_cpo == empty_option)
23630 p_cpo = save_cpo;
23631 else
23632 /* Darn, evaluating {sub} expression changed the value. */
23633 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634
23635 return ret;
23636}
23637
23638#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */