blob: 2432ad39502e99cd35ba85eab11b2e6a8ee65266 [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 Moolenaar29a1c1d2005-06-24 23:11:15 +0000914
915 /* global variables */
916 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000917
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000918 /* autoloaded script names */
919 ga_clear_strings(&ga_loaded);
920
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200921 /* script-local variables */
922 for (i = 1; i <= ga_scripts.ga_len; ++i)
923 {
924 vars_clear(&SCRIPT_VARS(i));
925 vim_free(SCRIPT_SV(i));
926 }
927 ga_clear(&ga_scripts);
928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000929 /* unreferenced lists and dicts */
930 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000931
932 /* functions */
933 free_all_functions();
934 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935}
936#endif
937
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * Return the name of the executed function.
940 */
941 char_u *
942func_name(cookie)
943 void *cookie;
944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948/*
949 * Return the address holding the next breakpoint line for a funccall cookie.
950 */
951 linenr_T *
952func_breakpoint(cookie)
953 void *cookie;
954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the debug tick for a funccall cookie.
960 */
961 int *
962func_dbg_tick(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the nesting level for a funccall cookie.
970 */
971 int
972func_level(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000979funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000981/* pointer to list of previously used funccal, still around because some
982 * item in it is still being used. */
983funccall_T *previous_funccal = NULL;
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
986 * Return TRUE when a function was ended by a ":return" command.
987 */
988 int
989current_func_returned()
990{
991 return current_funccal->returned;
992}
993
994
995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * Set an internal variable to a string value. Creates the variable if it does
997 * not already exist.
998 */
999 void
1000set_internal_string_var(name, value)
1001 char_u *name;
1002 char_u *value;
1003{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 val = vim_strsave(value);
1008 if (val != NULL)
1009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 }
1017}
1018
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static char_u *redir_endp = NULL;
1022static char_u *redir_varname = NULL;
1023
1024/*
1025 * Start recording command output to a variable
1026 * Returns OK if successfully completed the setup. FAIL otherwise.
1027 */
1028 int
1029var_redir_start(name, append)
1030 char_u *name;
1031 int append; /* append to an existing variable */
1032{
1033 int save_emsg;
1034 int err;
1035 typval_T tv;
1036
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001037 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001038 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 {
1040 EMSG(_(e_invarg));
1041 return FAIL;
1042 }
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 redir_varname = vim_strsave(name);
1046 if (redir_varname == NULL)
1047 return FAIL;
1048
1049 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1050 if (redir_lval == NULL)
1051 {
1052 var_redir_stop();
1053 return FAIL;
1054 }
1055
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056 /* The output is stored in growarray "redir_ga" until redirection ends. */
1057 ga_init2(&redir_ga, (int)sizeof(char), 500);
1058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001060 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1061 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1063 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001064 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 if (redir_endp != NULL && *redir_endp != NUL)
1066 /* Trailing characters are present after the variable name */
1067 EMSG(_(e_trailing));
1068 else
1069 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 var_redir_stop();
1072 return FAIL;
1073 }
1074
1075 /* check if we can write to the variable: set it to or append an empty
1076 * string */
1077 save_emsg = did_emsg;
1078 did_emsg = FALSE;
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = (char_u *)"";
1081 if (append)
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1083 else
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001085 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001087 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (err)
1089 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 var_redir_stop();
1092 return FAIL;
1093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 * Append "value[value_len]" to the variable set by var_redir_start().
1100 * The actual appending is postponed until redirection ends, because the value
1101 * appended may in fact be the string we write to, changing it may cause freed
1102 * memory to be used:
1103 * :redir => foo
1104 * :let foo
1105 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 */
1107 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
1114 if (redir_lval == NULL)
1115 return;
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 {
1124 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 }
1127 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129}
1130
1131/*
1132 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 */
1135 void
1136var_redir_stop()
1137{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 typval_T tv;
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_lval != NULL)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* If there was no error: assign the text to the variable. */
1143 if (redir_endp != NULL)
1144 {
1145 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1146 tv.v_type = VAR_STRING;
1147 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001148 /* Call get_lval() again, if it's inside a Dict or List it may
1149 * have changed. */
1150 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1151 FALSE, FALSE, FALSE, FNE_CHECK_START);
1152 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1153 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1154 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* free the collected output */
1158 vim_free(redir_ga.ga_data);
1159 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 vim_free(redir_lval);
1162 redir_lval = NULL;
1163 }
1164 vim_free(redir_varname);
1165 redir_varname = NULL;
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168# if defined(FEAT_MBYTE) || defined(PROTO)
1169 int
1170eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1171 char_u *enc_from;
1172 char_u *enc_to;
1173 char_u *fname_from;
1174 char_u *fname_to;
1175{
1176 int err = FALSE;
1177
1178 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1179 set_vim_var_string(VV_CC_TO, enc_to, -1);
1180 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1181 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1182 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1183 err = TRUE;
1184 set_vim_var_string(VV_CC_FROM, NULL, -1);
1185 set_vim_var_string(VV_CC_TO, NULL, -1);
1186 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1187 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1188
1189 if (err)
1190 return FAIL;
1191 return OK;
1192}
1193# endif
1194
1195# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1196 int
1197eval_printexpr(fname, args)
1198 char_u *fname;
1199 char_u *args;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_FNAME_IN, fname, -1);
1204 set_vim_var_string(VV_CMDARG, args, -1);
1205 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1208 set_vim_var_string(VV_CMDARG, NULL, -1);
1209
1210 if (err)
1211 {
1212 mch_remove(fname);
1213 return FAIL;
1214 }
1215 return OK;
1216}
1217# endif
1218
1219# if defined(FEAT_DIFF) || defined(PROTO)
1220 void
1221eval_diff(origfile, newfile, outfile)
1222 char_u *origfile;
1223 char_u *newfile;
1224 char_u *outfile;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1229 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1230 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1231 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1234 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1235}
1236
1237 void
1238eval_patch(origfile, difffile, outfile)
1239 char_u *origfile;
1240 char_u *difffile;
1241 char_u *outfile;
1242{
1243 int err;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253# endif
1254
1255/*
1256 * Top level evaluation function, returning a boolean.
1257 * Sets "error" to TRUE if there was an error.
1258 * Return TRUE or FALSE.
1259 */
1260 int
1261eval_to_bool(arg, error, nextcmd, skip)
1262 char_u *arg;
1263 int *error;
1264 char_u **nextcmd;
1265 int skip; /* only parse, don't execute */
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int retval = FALSE;
1269
1270 if (skip)
1271 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
1275 {
1276 *error = FALSE;
1277 if (!skip)
1278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001279 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
1290 * Top level evaluation function, returning a string. If "skip" is TRUE,
1291 * only parsing to "nextcmd" is done, without reporting errors. Return
1292 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1293 */
1294 char_u *
1295eval_to_string_skip(arg, nextcmd, skip)
1296 char_u *arg;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 char_u *retval;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 retval = NULL;
1307 else
1308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 retval = vim_strsave(get_tv_string(&tv));
1310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319 * Skip over an expression at "*pp".
1320 * Return FAIL for an error, OK otherwise.
1321 */
1322 int
1323skip_expr(pp)
1324 char_u **pp;
1325{
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327
1328 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001330}
1331
1332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 * When "convert" is TRUE convert a List into a sequence of lines and convert
1335 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 * Return pointer to allocated memory, or NULL for failure.
1337 */
1338 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *arg;
1341 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 retval = NULL;
1353 else
1354 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 {
1357 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 if (tv.vval.v_list != NULL)
1359 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 ga_append(&ga, NUL);
1361 retval = (char_u *)ga.ga_data;
1362 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363#ifdef FEAT_FLOAT
1364 else if (convert && tv.v_type == VAR_FLOAT)
1365 {
1366 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1367 retval = vim_strsave(numbuf);
1368 }
1369#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001370 else
1371 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374
1375 return retval;
1376}
1377
1378/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379 * Call eval_to_string() without using current local variables and using
1380 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
1382 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 char_u *arg;
1385 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 char_u *retval;
1389 void *save_funccalp;
1390
1391 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 ++sandbox;
1394 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001395 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 if (use_sandbox)
1397 --sandbox;
1398 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 restore_funccal(save_funccalp);
1400 return retval;
1401}
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403/*
1404 * Top level evaluation function, returning a number.
1405 * Evaluates "expr" silently.
1406 * Returns -1 for an error.
1407 */
1408 int
1409eval_to_number(expr)
1410 char_u *expr;
1411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001414 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 ++emsg_off;
1417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = -1;
1420 else
1421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001422 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 --emsg_off;
1426
1427 return retval;
1428}
1429
Bram Moolenaara40058a2005-07-11 22:42:07 +00001430/*
1431 * Prepare v: variable "idx" to be used.
1432 * Save the current typeval in "save_tv".
1433 * When not used yet add the variable to the v: hashtable.
1434 */
1435 static void
1436prepare_vimvar(idx, save_tv)
1437 int idx;
1438 typval_T *save_tv;
1439{
1440 *save_tv = vimvars[idx].vv_tv;
1441 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1442 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1443}
1444
1445/*
1446 * Restore v: variable "idx" to typeval "save_tv".
1447 * When no longer defined, remove the variable from the v: hashtable.
1448 */
1449 static void
1450restore_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 hashitem_T *hi;
1455
Bram Moolenaara40058a2005-07-11 22:42:07 +00001456 vimvars[idx].vv_tv = *save_tv;
1457 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1458 {
1459 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1460 if (HASHITEM_EMPTY(hi))
1461 EMSG2(_(e_intern2), "restore_vimvar()");
1462 else
1463 hash_remove(&vimvarht, hi);
1464 }
1465}
1466
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001467#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468/*
1469 * Evaluate an expression to a list with suggestions.
1470 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001471 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472 */
1473 list_T *
1474eval_spell_expr(badword, expr)
1475 char_u *badword;
1476 char_u *expr;
1477{
1478 typval_T save_val;
1479 typval_T rettv;
1480 list_T *list = NULL;
1481 char_u *p = skipwhite(expr);
1482
1483 /* Set "v:val" to the bad word. */
1484 prepare_vimvar(VV_VAL, &save_val);
1485 vimvars[VV_VAL].vv_type = VAR_STRING;
1486 vimvars[VV_VAL].vv_str = badword;
1487 if (p_verbose == 0)
1488 ++emsg_off;
1489
1490 if (eval1(&p, &rettv, TRUE) == OK)
1491 {
1492 if (rettv.v_type != VAR_LIST)
1493 clear_tv(&rettv);
1494 else
1495 list = rettv.vval.v_list;
1496 }
1497
1498 if (p_verbose == 0)
1499 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001500 restore_vimvar(VV_VAL, &save_val);
1501
1502 return list;
1503}
1504
1505/*
1506 * "list" is supposed to contain two items: a word and a number. Return the
1507 * word in "pp" and the number as the return value.
1508 * Return -1 if anything isn't right.
1509 * Used to get the good word and score from the eval_spell_expr() result.
1510 */
1511 int
1512get_spellword(list, pp)
1513 list_T *list;
1514 char_u **pp;
1515{
1516 listitem_T *li;
1517
1518 li = list->lv_first;
1519 if (li == NULL)
1520 return -1;
1521 *pp = get_tv_string(&li->li_tv);
1522
1523 li = li->li_next;
1524 if (li == NULL)
1525 return -1;
1526 return get_tv_number(&li->li_tv);
1527}
1528#endif
1529
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001531 * Top level evaluation function.
1532 * Returns an allocated typval_T with the result.
1533 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 */
1535 typval_T *
1536eval_expr(arg, nextcmd)
1537 char_u *arg;
1538 char_u **nextcmd;
1539{
1540 typval_T *tv;
1541
1542 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 {
1545 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001546 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001547 }
1548
1549 return tv;
1550}
1551
1552
Bram Moolenaar4f688582007-07-24 12:34:30 +00001553#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1554 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 * Uses argv[argc] for the function arguments. Only Number and String
1558 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 static int
1562call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char_u *func;
1564 int argc;
1565 char_u **argv;
1566 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* Recognize a number argument, the others must be strings. */
1592 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1593 if (len != 0 && len == (int)STRLEN(argv[i]))
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_NUMBER;
1596 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 else
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_STRING;
1601 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 }
1604
1605 if (safe)
1606 {
1607 save_funccalp = save_funccal();
1608 ++sandbox;
1609 }
1610
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1612 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (safe)
1616 {
1617 --sandbox;
1618 restore_funccal(save_funccalp);
1619 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 vim_free(argvars);
1621
1622 if (ret == FAIL)
1623 clear_tv(rettv);
1624
1625 return ret;
1626}
1627
Bram Moolenaar4f688582007-07-24 12:34:30 +00001628# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001630 * Call vimL function "func" and return the result as a string.
1631 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 * Uses argv[argc] for the function arguments.
1633 */
1634 void *
1635call_func_retstr(func, argc, argv, safe)
1636 char_u *func;
1637 int argc;
1638 char_u **argv;
1639 int safe; /* use the sandbox */
1640{
1641 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001642 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643
1644 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1645 return NULL;
1646
1647 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 return retval;
1650}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001651# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652
Bram Moolenaar4f688582007-07-24 12:34:30 +00001653# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001655 * Call vimL function "func" and return the result as a number.
1656 * Returns -1 when calling the function fails.
1657 * Uses argv[argc] for the function arguments.
1658 */
1659 long
1660call_func_retnr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
1667 long retval;
1668
1669 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1670 return -1;
1671
1672 retval = get_tv_number_chk(&rettv, NULL);
1673 clear_tv(&rettv);
1674 return retval;
1675}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001676# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001677
1678/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001679 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001681 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 */
1683 void *
1684call_func_retlist(func, argc, argv, safe)
1685 char_u *func;
1686 int argc;
1687 char_u **argv;
1688 int safe; /* use the sandbox */
1689{
1690 typval_T rettv;
1691
1692 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1693 return NULL;
1694
1695 if (rettv.v_type != VAR_LIST)
1696 {
1697 clear_tv(&rettv);
1698 return NULL;
1699 }
1700
1701 return rettv.vval.v_list;
1702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
1704
Bram Moolenaar4f688582007-07-24 12:34:30 +00001705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706/*
1707 * Save the current function call pointer, and set it to NULL.
1708 * Used when executing autocommands and for ":source".
1709 */
1710 void *
1711save_funccal()
1712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 current_funccal = NULL;
1716 return (void *)fc;
1717}
1718
1719 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720restore_funccal(vfc)
1721 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723 funccall_T *fc = (funccall_T *)vfc;
1724
1725 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726}
1727
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728#if defined(FEAT_PROFILE) || defined(PROTO)
1729/*
1730 * Prepare profiling for entering a child or something else that is not
1731 * counted for the script/function itself.
1732 * Should always be called in pair with prof_child_exit().
1733 */
1734 void
1735prof_child_enter(tm)
1736 proftime_T *tm; /* place to store waittime */
1737{
1738 funccall_T *fc = current_funccal;
1739
1740 if (fc != NULL && fc->func->uf_profiling)
1741 profile_start(&fc->prof_child);
1742 script_prof_save(tm);
1743}
1744
1745/*
1746 * Take care of time spent in a child.
1747 * Should always be called after prof_child_enter().
1748 */
1749 void
1750prof_child_exit(tm)
1751 proftime_T *tm; /* where waittime was stored */
1752{
1753 funccall_T *fc = current_funccal;
1754
1755 if (fc != NULL && fc->func->uf_profiling)
1756 {
1757 profile_end(&fc->prof_child);
1758 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1759 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1760 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1761 }
1762 script_prof_restore(tm);
1763}
1764#endif
1765
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#ifdef FEAT_FOLDING
1768/*
1769 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1770 * it in "*cp". Doesn't give error messages.
1771 */
1772 int
1773eval_foldexpr(arg, cp)
1774 char_u *arg;
1775 int *cp;
1776{
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int retval;
1779 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001780 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1781 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001784 if (use_sandbox)
1785 ++sandbox;
1786 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 retval = 0;
1790 else
1791 {
1792 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (tv.v_type == VAR_NUMBER)
1794 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001795 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 retval = 0;
1797 else
1798 {
1799 /* If the result is a string, check if there is a non-digit before
1800 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (!VIM_ISDIGIT(*s) && *s != '-')
1803 *cp = *s++;
1804 retval = atol((char *)s);
1805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001809 if (use_sandbox)
1810 --sandbox;
1811 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 return retval;
1814}
1815#endif
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 * ":let" list all variable values
1819 * ":let var1 var2" list variable values
1820 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 * ":let var += expr" assignment command.
1822 * ":let var -= expr" assignment command.
1823 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 */
1826 void
1827ex_let(eap)
1828 exarg_T *eap;
1829{
1830 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001832 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 int var_count = 0;
1835 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001838 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 argend = skip_var_list(arg, &var_count, &semicolon);
1841 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001843 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1844 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001848 /*
1849 * ":let" without "=": list variables
1850 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (*arg == '[')
1852 EMSG(_(e_invarg));
1853 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001855 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 list_glob_vars(&first);
1860 list_buf_vars(&first);
1861 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001862#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001864#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_script_vars(&first);
1866 list_func_vars(&first);
1867 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 eap->nextcmd = check_nextcmd(arg);
1870 }
1871 else
1872 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 op[0] = '=';
1874 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001875 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 {
1877 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1878 op[0] = expr[-1]; /* +=, -= or .= */
1879 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (eap->skip)
1883 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (eap->skip)
1886 {
1887 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 --emsg_skip;
1890 }
1891 else if (i != FAIL)
1892 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 }
1898}
1899
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900/*
1901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1902 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 * When "nextchars" is not NULL it points to a string with characters that
1904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1905 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 * Returns OK or FAIL;
1907 */
1908 static int
1909ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1910 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 int copy; /* copy values from "tv", don't move */
1913 int semicolon; /* from skip_var_list() */
1914 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916{
1917 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 listitem_T *item;
1921 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922
1923 if (*arg != '[')
1924 {
1925 /*
1926 * ":let var = expr" or ":for var in list"
1927 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 return FAIL;
1930 return OK;
1931 }
1932
1933 /*
1934 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1935 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001936 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 {
1938 EMSG(_(e_listreq));
1939 return FAIL;
1940 }
1941
1942 i = list_len(l);
1943 if (semicolon == 0 && var_count < i)
1944 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001945 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return FAIL;
1947 }
1948 if (var_count - semicolon > i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953
1954 item = l->lv_first;
1955 while (*arg != ']')
1956 {
1957 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 item = item->li_next;
1960 if (arg == NULL)
1961 return FAIL;
1962
1963 arg = skipwhite(arg);
1964 if (*arg == ';')
1965 {
1966 /* Put the rest of the list (may be empty) in the var after ';'.
1967 * Create a new list for this. */
1968 l = list_alloc();
1969 if (l == NULL)
1970 return FAIL;
1971 while (item != NULL)
1972 {
1973 list_append_tv(l, &item->li_tv);
1974 item = item->li_next;
1975 }
1976
1977 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001978 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 ltv.vval.v_list = l;
1980 l->lv_refcount = 1;
1981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1983 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 clear_tv(&ltv);
1985 if (arg == NULL)
1986 return FAIL;
1987 break;
1988 }
1989 else if (*arg != ',' && *arg != ']')
1990 {
1991 EMSG2(_(e_intern2), "ex_let_vars()");
1992 return FAIL;
1993 }
1994 }
1995
1996 return OK;
1997}
1998
1999/*
2000 * Skip over assignable variable "var" or list of variables "[var, var]".
2001 * Used for ":let varvar = expr" and ":for varvar in expr".
2002 * For "[var, var]" increment "*var_count" for each variable.
2003 * for "[var, var; var]" set "semicolon".
2004 * Return NULL for an error.
2005 */
2006 static char_u *
2007skip_var_list(arg, var_count, semicolon)
2008 char_u *arg;
2009 int *var_count;
2010 int *semicolon;
2011{
2012 char_u *p, *s;
2013
2014 if (*arg == '[')
2015 {
2016 /* "[var, var]": find the matching ']'. */
2017 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002018 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 {
2020 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2021 s = skip_var_one(p);
2022 if (s == p)
2023 {
2024 EMSG2(_(e_invarg2), p);
2025 return NULL;
2026 }
2027 ++*var_count;
2028
2029 p = skipwhite(s);
2030 if (*p == ']')
2031 break;
2032 else if (*p == ';')
2033 {
2034 if (*semicolon == 1)
2035 {
2036 EMSG(_("Double ; in list of variables"));
2037 return NULL;
2038 }
2039 *semicolon = 1;
2040 }
2041 else if (*p != ',')
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 }
2047 return p + 1;
2048 }
2049 else
2050 return skip_var_one(arg);
2051}
2052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002054 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002055 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 static char_u *
2058skip_var_one(arg)
2059 char_u *arg;
2060{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 if (*arg == '@' && arg[1] != NUL)
2062 return arg + 2;
2063 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2064 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065}
2066
Bram Moolenaara7043832005-01-21 11:56:39 +00002067/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 * List variables for hashtab "ht" with prefix "prefix".
2069 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002076 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077{
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashitem_T *hi;
2079 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 int todo;
2081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002082 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2084 {
2085 if (!HASHITEM_EMPTY(hi))
2086 {
2087 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 di = HI2DI(hi);
2089 if (empty || di->di_tv.v_type != VAR_STRING
2090 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 }
2093 }
2094}
2095
2096/*
2097 * List global variables.
2098 */
2099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_glob_vars(first)
2101 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104}
2105
2106/*
2107 * List buffer variables.
2108 */
2109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_buf_vars(first)
2111 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113 char_u numbuf[NUMBUFLEN];
2114
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2116 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117
2118 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2120 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121}
2122
2123/*
2124 * List window variables.
2125 */
2126 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127list_win_vars(first)
2128 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002129{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2131 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002132}
2133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134#ifdef FEAT_WINDOWS
2135/*
2136 * List tab page variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_tab_vars(first)
2140 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2143 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144}
2145#endif
2146
Bram Moolenaara7043832005-01-21 11:56:39 +00002147/*
2148 * List Vim variables.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_vim_vars(first)
2152 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155}
2156
2157/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002158 * List script-local variables, if there is a script.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_script_vars(first)
2162 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163{
2164 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2166 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167}
2168
2169/*
2170 * List function variables, if there is a function.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_func_vars(first)
2174 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002175{
2176 if (current_funccal != NULL)
2177 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002179}
2180
2181/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 * List variables in "arg".
2183 */
2184 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 exarg_T *eap;
2187 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189{
2190 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002191 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 char_u *name_start;
2194 char_u *arg_subsc;
2195 char_u *tofree;
2196 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197
2198 while (!ends_excmd(*arg) && !got_int)
2199 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002202 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2204 {
2205 emsg_severe = TRUE;
2206 EMSG(_(e_trailing));
2207 break;
2208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 /* get_name_len() takes care of expanding curly braces */
2213 name_start = name = arg;
2214 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2215 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* This is mainly to keep test 49 working: when expanding
2218 * curly braces fails overrule the exception error message. */
2219 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 emsg_severe = TRUE;
2222 EMSG2(_(e_invarg2), arg);
2223 break;
2224 }
2225 error = TRUE;
2226 }
2227 else
2228 {
2229 if (tofree != NULL)
2230 name = tofree;
2231 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 else
2234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 /* handle d.key, l[idx], f(expr) */
2236 arg_subsc = arg;
2237 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 case 'g': list_glob_vars(first); break;
2246 case 'b': list_buf_vars(first); break;
2247 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002248#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002249 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002250#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'v': list_vim_vars(first); break;
2252 case 's': list_script_vars(first); break;
2253 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 default:
2255 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
2258 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 {
2260 char_u numbuf[NUMBUFLEN];
2261 char_u *tf;
2262 int c;
2263 char_u *s;
2264
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002265 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 c = *arg;
2267 *arg = NUL;
2268 list_one_var_a((char_u *)"",
2269 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 tv.v_type,
2271 s == NULL ? (char_u *)"" : s,
2272 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 *arg = c;
2274 vim_free(tf);
2275 }
2276 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 }
2279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280
2281 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283
2284 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286
2287 return arg;
2288}
2289
2290/*
2291 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2292 * Returns a pointer to the char just after the var name.
2293 * Returns NULL if there is an error.
2294 */
2295 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002298 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002300 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302{
2303 int c1;
2304 char_u *name;
2305 char_u *p;
2306 char_u *arg_end = NULL;
2307 int len;
2308 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310
2311 /*
2312 * ":let $VAR = expr": Set environment variable.
2313 */
2314 if (*arg == '$')
2315 {
2316 /* Find the end of the name. */
2317 ++arg;
2318 name = arg;
2319 len = get_env_len(&arg);
2320 if (len == 0)
2321 EMSG2(_(e_invarg2), name - 1);
2322 else
2323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 if (op != NULL && (*op == '+' || *op == '-'))
2325 EMSG2(_(e_letwrong), op);
2326 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002327 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002329 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
2331 c1 = name[len];
2332 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 p = get_tv_string_chk(tv);
2334 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 {
2336 int mustfree = FALSE;
2337 char_u *s = vim_getenv(name, &mustfree);
2338
2339 if (s != NULL)
2340 {
2341 p = tofree = concat_str(s, p);
2342 if (mustfree)
2343 vim_free(s);
2344 }
2345 }
2346 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002349 if (STRICMP(name, "HOME") == 0)
2350 init_homedir();
2351 else if (didset_vim && STRICMP(name, "VIM") == 0)
2352 didset_vim = FALSE;
2353 else if (didset_vimruntime
2354 && STRICMP(name, "VIMRUNTIME") == 0)
2355 didset_vimruntime = FALSE;
2356 arg_end = arg;
2357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
2362 }
2363
2364 /*
2365 * ":let &option = expr": Set option value.
2366 * ":let &l:option = expr": Set local option value.
2367 * ":let &g:option = expr": Set global option value.
2368 */
2369 else if (*arg == '&')
2370 {
2371 /* Find the end of the name. */
2372 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 if (p == NULL || (endchars != NULL
2374 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 EMSG(_(e_letunexp));
2376 else
2377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 long n;
2379 int opt_type;
2380 long numval;
2381 char_u *stringval = NULL;
2382 char_u *s;
2383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 c1 = *p;
2385 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386
2387 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 s = get_tv_string_chk(tv); /* != NULL if number or string */
2389 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 {
2391 opt_type = get_option_value(arg, &numval,
2392 &stringval, opt_flags);
2393 if ((opt_type == 1 && *op == '.')
2394 || (opt_type == 0 && *op != '.'))
2395 EMSG2(_(e_letwrong), op);
2396 else
2397 {
2398 if (opt_type == 1) /* number */
2399 {
2400 if (*op == '+')
2401 n = numval + n;
2402 else
2403 n = numval - n;
2404 }
2405 else if (opt_type == 0 && stringval != NULL) /* string */
2406 {
2407 s = concat_str(stringval, s);
2408 vim_free(stringval);
2409 stringval = s;
2410 }
2411 }
2412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 if (s != NULL)
2414 {
2415 set_option_value(arg, n, s, opt_flags);
2416 arg_end = p;
2417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 }
2421 }
2422
2423 /*
2424 * ":let @r = expr": Set register contents.
2425 */
2426 else if (*arg == '@')
2427 {
2428 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (op != NULL && (*op == '+' || *op == '-'))
2430 EMSG2(_(e_letwrong), op);
2431 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 EMSG(_(e_letunexp));
2434 else
2435 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002436 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u *s;
2438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 p = get_tv_string_chk(tv);
2440 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002442 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 if (s != NULL)
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 vim_free(s);
2447 }
2448 }
2449 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 arg_end = arg + 1;
2453 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457
2458 /*
2459 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002462 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002464 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002466 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 arg_end = p;
2475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479
2480 else
2481 EMSG2(_(e_invarg2), arg);
2482
2483 return arg_end;
2484}
2485
2486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2488 */
2489 static int
2490check_changedtick(arg)
2491 char_u *arg;
2492{
2493 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2494 {
2495 EMSG2(_(e_readonlyvar), arg);
2496 return TRUE;
2497 }
2498 return FALSE;
2499}
2500
2501/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * Get an lval: variable, Dict item or List item that can be assigned a value
2503 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2504 * "name.key", "name.key[expr]" etc.
2505 * Indexing only works if "name" is an existing List or Dictionary.
2506 * "name" points to the start of the name.
2507 * If "rettv" is not NULL it points to the value to be assigned.
2508 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2509 * wrong; must end in space or cmd separator.
2510 *
2511 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002512 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
2515 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 typval_T *rettv;
2519 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 int unlet;
2521 int skip;
2522 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 char_u *expr_start, *expr_end;
2527 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 dictitem_T *v;
2529 typval_T var1;
2530 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539
2540 if (skip)
2541 {
2542 /* When skipping just find the end of the name. */
2543 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 }
2546
2547 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002548 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (expr_start != NULL)
2550 {
2551 /* Don't expand the name when we already know there is an error. */
2552 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2553 && *p != '[' && *p != '.')
2554 {
2555 EMSG(_(e_trailing));
2556 return NULL;
2557 }
2558
2559 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2560 if (lp->ll_exp_name == NULL)
2561 {
2562 /* Report an invalid expression in braces, unless the
2563 * expression evaluation has been cancelled due to an
2564 * aborting error, an interrupt, or an exception. */
2565 if (!aborting() && !quiet)
2566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002567 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 EMSG2(_(e_invarg2), name);
2569 return NULL;
2570 }
2571 }
2572 lp->ll_name = lp->ll_exp_name;
2573 }
2574 else
2575 lp->ll_name = name;
2576
2577 /* Without [idx] or .key we are done. */
2578 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2579 return p;
2580
2581 cc = *p;
2582 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002583 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (v == NULL && !quiet)
2585 EMSG2(_(e_undefvar), lp->ll_name);
2586 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 if (v == NULL)
2588 return NULL;
2589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /*
2591 * Loop until no more [idx] or .key is following.
2592 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2597 && !(lp->ll_tv->v_type == VAR_DICT
2598 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!quiet)
2601 EMSG(_("E689: Can only index a List or Dictionary"));
2602 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E708: [:] must come last"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 len = -1;
2612 if (*p == '.')
2613 {
2614 key = p + 1;
2615 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2616 ;
2617 if (len == 0)
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_(e_emptykey));
2621 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623 p = key + len;
2624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 else
2626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (*p == ':')
2630 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 empty1 = FALSE;
2634 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 if (get_tv_string_chk(&var1) == NULL)
2637 {
2638 /* not a number or string */
2639 clear_tv(&var1);
2640 return NULL;
2641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 }
2643
2644 /* Optionally get the second index [ :expr]. */
2645 if (*p == ':')
2646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 if (!empty1)
2652 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (rettv != NULL && (rettv->v_type != VAR_LIST
2656 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 }
2664 p = skipwhite(p + 1);
2665 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 else
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (!empty1)
2673 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 if (get_tv_string_chk(&var2) == NULL)
2677 {
2678 /* not a number or string */
2679 if (!empty1)
2680 clear_tv(&var1);
2681 clear_tv(&var2);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Skip to past ']'. */
2702 ++p;
2703 }
2704
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
2707 if (len == -1)
2708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (*key == NUL)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 lp->ll_list = NULL;
2720 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002722
2723 /* When assigning to g: check that a function and variable name is
2724 * valid. */
2725 if (rettv != NULL && lp->ll_dict == &globvardict)
2726 {
2727 if (rettv->v_type == VAR_FUNC
2728 && var_check_func_name(key, lp->ll_di == NULL))
2729 return NULL;
2730 if (!valid_varname(key))
2731 return NULL;
2732 }
2733
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002736 /* Can't add "v:" variable. */
2737 if (lp->ll_dict == &vimvardict)
2738 {
2739 EMSG2(_(e_illvar), name);
2740 return NULL;
2741 }
2742
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002743 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002747 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (len == -1)
2749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (len == -1)
2757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 p = NULL;
2760 break;
2761 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762 /* existing variable, need to check if it can be changed */
2763 else if (var_check_ro(lp->ll_di->di_flags, name))
2764 return NULL;
2765
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (len == -1)
2767 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 }
2770 else
2771 {
2772 /*
2773 * Get the number and item for the only or first index of the List.
2774 */
2775 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 else
2778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 clear_tv(&var1);
2781 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_list = lp->ll_tv->vval.v_list;
2784 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2785 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002787 if (lp->ll_n1 < 0)
2788 {
2789 lp->ll_n1 = 0;
2790 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2791 }
2792 }
2793 if (lp->ll_li == NULL)
2794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002797 if (!quiet)
2798 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 }
2801
2802 /*
2803 * May need to find the item or absolute index for the second
2804 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 * When no index given: "lp->ll_empty2" is TRUE.
2806 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002810 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002816 {
2817 if (!quiet)
2818 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002820 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 }
2823
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2825 if (lp->ll_n1 < 0)
2826 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2827 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002828 {
2829 if (!quiet)
2830 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002832 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002833 }
2834
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002837 }
2838
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 return p;
2840}
2841
2842/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002843 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 */
2845 static void
2846clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002847 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848{
2849 vim_free(lp->ll_exp_name);
2850 vim_free(lp->ll_newkey);
2851}
2852
2853/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002854 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002856 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 */
2858 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002859set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865{
2866 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 listitem_T *ri;
2868 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869
2870 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 cc = *endp;
2875 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876 if (op != NULL && *op != '=')
2877 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002880 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002881 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002882 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002883 {
2884 if (tv_op(&tv, rettv, op) == OK)
2885 set_var(lp->ll_name, &tv, FALSE);
2886 clear_tv(&tv);
2887 }
2888 }
2889 else
2890 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002892 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002894 else if (tv_check_lock(lp->ll_newkey == NULL
2895 ? lp->ll_tv->v_lock
2896 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2897 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 else if (lp->ll_range)
2899 {
2900 /*
2901 * Assign the List values to the list items.
2902 */
2903 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002904 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 if (op != NULL && *op != '=')
2906 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2907 else
2908 {
2909 clear_tv(&lp->ll_li->li_tv);
2910 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2911 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 ri = ri->li_next;
2913 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2914 break;
2915 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002916 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002918 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 ri = NULL;
2921 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002922 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 lp->ll_li = lp->ll_li->li_next;
2925 ++lp->ll_n1;
2926 }
2927 if (ri != NULL)
2928 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 else if (lp->ll_empty2
2930 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 : lp->ll_n1 != lp->ll_n2)
2932 EMSG(_("E711: List value has not enough items"));
2933 }
2934 else
2935 {
2936 /*
2937 * Assign to a List or Dictionary item.
2938 */
2939 if (lp->ll_newkey != NULL)
2940 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 if (op != NULL && *op != '=')
2942 {
2943 EMSG2(_(e_letwrong), op);
2944 return;
2945 }
2946
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002948 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 if (di == NULL)
2950 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002951 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2952 {
2953 vim_free(di);
2954 return;
2955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 else if (op != NULL && *op != '=')
2959 {
2960 tv_op(lp->ll_tv, rettv, op);
2961 return;
2962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002965
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 /*
2967 * Assign the value to the variable or list item.
2968 */
2969 if (copy)
2970 copy_tv(rettv, lp->ll_tv);
2971 else
2972 {
2973 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002974 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 }
2977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978}
2979
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002980/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2982 * Returns OK or FAIL.
2983 */
2984 static int
2985tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 typval_T *tv1;
2987 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988 char_u *op;
2989{
2990 long n;
2991 char_u numbuf[NUMBUFLEN];
2992 char_u *s;
2993
2994 /* Can't do anything with a Funcref or a Dict on the right. */
2995 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2996 {
2997 switch (tv1->v_type)
2998 {
2999 case VAR_DICT:
3000 case VAR_FUNC:
3001 break;
3002
3003 case VAR_LIST:
3004 if (*op != '+' || tv2->v_type != VAR_LIST)
3005 break;
3006 /* List += List */
3007 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3008 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3009 return OK;
3010
3011 case VAR_NUMBER:
3012 case VAR_STRING:
3013 if (tv2->v_type == VAR_LIST)
3014 break;
3015 if (*op == '+' || *op == '-')
3016 {
3017 /* nr += nr or nr -= nr*/
3018 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019#ifdef FEAT_FLOAT
3020 if (tv2->v_type == VAR_FLOAT)
3021 {
3022 float_T f = n;
3023
3024 if (*op == '+')
3025 f += tv2->vval.v_float;
3026 else
3027 f -= tv2->vval.v_float;
3028 clear_tv(tv1);
3029 tv1->v_type = VAR_FLOAT;
3030 tv1->vval.v_float = f;
3031 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003033#endif
3034 {
3035 if (*op == '+')
3036 n += get_tv_number(tv2);
3037 else
3038 n -= get_tv_number(tv2);
3039 clear_tv(tv1);
3040 tv1->v_type = VAR_NUMBER;
3041 tv1->vval.v_number = n;
3042 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003043 }
3044 else
3045 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046 if (tv2->v_type == VAR_FLOAT)
3047 break;
3048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 /* str .= str */
3050 s = get_tv_string(tv1);
3051 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3052 clear_tv(tv1);
3053 tv1->v_type = VAR_STRING;
3054 tv1->vval.v_string = s;
3055 }
3056 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057
3058#ifdef FEAT_FLOAT
3059 case VAR_FLOAT:
3060 {
3061 float_T f;
3062
3063 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3064 && tv2->v_type != VAR_NUMBER
3065 && tv2->v_type != VAR_STRING))
3066 break;
3067 if (tv2->v_type == VAR_FLOAT)
3068 f = tv2->vval.v_float;
3069 else
3070 f = get_tv_number(tv2);
3071 if (*op == '+')
3072 tv1->vval.v_float += f;
3073 else
3074 tv1->vval.v_float -= f;
3075 }
3076 return OK;
3077#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003078 }
3079 }
3080
3081 EMSG2(_(e_letwrong), op);
3082 return FAIL;
3083}
3084
3085/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003086 * Add a watcher to a list.
3087 */
3088 static void
3089list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 list_T *l;
3091 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092{
3093 lw->lw_next = l->lv_watch;
3094 l->lv_watch = lw;
3095}
3096
3097/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003098 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003099 * No warning when it isn't found...
3100 */
3101 static void
3102list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003103 list_T *l;
3104 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003105{
Bram Moolenaar33570922005-01-25 22:26:29 +00003106 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107
3108 lwp = &l->lv_watch;
3109 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3110 {
3111 if (lw == lwrem)
3112 {
3113 *lwp = lw->lw_next;
3114 break;
3115 }
3116 lwp = &lw->lw_next;
3117 }
3118}
3119
3120/*
3121 * Just before removing an item from a list: advance watchers to the next
3122 * item.
3123 */
3124 static void
3125list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 list_T *l;
3127 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128{
Bram Moolenaar33570922005-01-25 22:26:29 +00003129 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130
3131 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3132 if (lw->lw_item == item)
3133 lw->lw_item = item->li_next;
3134}
3135
3136/*
3137 * Evaluate the expression used in a ":for var in expr" command.
3138 * "arg" points to "var".
3139 * Set "*errp" to TRUE for an error, FALSE otherwise;
3140 * Return a pointer that holds the info. Null when there is an error.
3141 */
3142 void *
3143eval_for_line(arg, errp, nextcmdp, skip)
3144 char_u *arg;
3145 int *errp;
3146 char_u **nextcmdp;
3147 int skip;
3148{
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 typval_T tv;
3152 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153
3154 *errp = TRUE; /* default: there is an error */
3155
Bram Moolenaar33570922005-01-25 22:26:29 +00003156 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157 if (fi == NULL)
3158 return NULL;
3159
3160 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3161 if (expr == NULL)
3162 return fi;
3163
3164 expr = skipwhite(expr);
3165 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3166 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003167 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 return fi;
3169 }
3170
3171 if (skip)
3172 ++emsg_skip;
3173 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3174 {
3175 *errp = FALSE;
3176 if (!skip)
3177 {
3178 l = tv.vval.v_list;
3179 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003180 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003182 clear_tv(&tv);
3183 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 else
3185 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003186 /* No need to increment the refcount, it's already set for the
3187 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 fi->fi_list = l;
3189 list_add_watch(l, &fi->fi_lw);
3190 fi->fi_lw.lw_item = l->lv_first;
3191 }
3192 }
3193 }
3194 if (skip)
3195 --emsg_skip;
3196
3197 return fi;
3198}
3199
3200/*
3201 * Use the first item in a ":for" list. Advance to the next.
3202 * Assign the values to the variable (list). "arg" points to the first one.
3203 * Return TRUE when a valid item was found, FALSE when at end of list or
3204 * something wrong.
3205 */
3206 int
3207next_for_item(fi_void, arg)
3208 void *fi_void;
3209 char_u *arg;
3210{
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 item = fi->fi_lw.lw_item;
3216 if (item == NULL)
3217 result = FALSE;
3218 else
3219 {
3220 fi->fi_lw.lw_item = item->li_next;
3221 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3222 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3223 }
3224 return result;
3225}
3226
3227/*
3228 * Free the structure used to store info used by ":for".
3229 */
3230 void
3231free_for_info(fi_void)
3232 void *fi_void;
3233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003236 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003237 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003239 list_unref(fi->fi_list);
3240 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 vim_free(fi);
3242}
3243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3245
3246 void
3247set_context_for_expression(xp, arg, cmdidx)
3248 expand_T *xp;
3249 char_u *arg;
3250 cmdidx_T cmdidx;
3251{
3252 int got_eq = FALSE;
3253 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 if (cmdidx == CMD_let)
3257 {
3258 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003259 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 {
3261 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003262 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 {
3264 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 if (vim_iswhite(*p))
3267 break;
3268 }
3269 return;
3270 }
3271 }
3272 else
3273 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3274 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 while ((xp->xp_pattern = vim_strpbrk(arg,
3276 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3277 {
3278 c = *xp->xp_pattern;
3279 if (c == '&')
3280 {
3281 c = xp->xp_pattern[1];
3282 if (c == '&')
3283 {
3284 ++xp->xp_pattern;
3285 xp->xp_context = cmdidx != CMD_let || got_eq
3286 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3287 }
3288 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003291 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3292 xp->xp_pattern += 2;
3293
3294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296 else if (c == '$')
3297 {
3298 /* environment variable */
3299 xp->xp_context = EXPAND_ENV_VARS;
3300 }
3301 else if (c == '=')
3302 {
3303 got_eq = TRUE;
3304 xp->xp_context = EXPAND_EXPRESSION;
3305 }
3306 else if (c == '<'
3307 && xp->xp_context == EXPAND_FUNCTIONS
3308 && vim_strchr(xp->xp_pattern, '(') == NULL)
3309 {
3310 /* Function name can start with "<SNR>" */
3311 break;
3312 }
3313 else if (cmdidx != CMD_let || got_eq)
3314 {
3315 if (c == '"') /* string */
3316 {
3317 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3318 if (c == '\\' && xp->xp_pattern[1] != NUL)
3319 ++xp->xp_pattern;
3320 xp->xp_context = EXPAND_NOTHING;
3321 }
3322 else if (c == '\'') /* literal string */
3323 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003324 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3326 /* skip */ ;
3327 xp->xp_context = EXPAND_NOTHING;
3328 }
3329 else if (c == '|')
3330 {
3331 if (xp->xp_pattern[1] == '|')
3332 {
3333 ++xp->xp_pattern;
3334 xp->xp_context = EXPAND_EXPRESSION;
3335 }
3336 else
3337 xp->xp_context = EXPAND_COMMANDS;
3338 }
3339 else
3340 xp->xp_context = EXPAND_EXPRESSION;
3341 }
3342 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 /* Doesn't look like something valid, expand as an expression
3344 * anyway. */
3345 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 arg = xp->xp_pattern;
3347 if (*arg != NUL)
3348 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3349 /* skip */ ;
3350 }
3351 xp->xp_pattern = arg;
3352}
3353
3354#endif /* FEAT_CMDL_COMPL */
3355
3356/*
3357 * ":1,25call func(arg1, arg2)" function call.
3358 */
3359 void
3360ex_call(eap)
3361 exarg_T *eap;
3362{
3363 char_u *arg = eap->arg;
3364 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003366 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003368 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 linenr_T lnum;
3370 int doesrange;
3371 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003372 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003374 if (eap->skip)
3375 {
3376 /* trans_function_name() doesn't work well when skipping, use eval0()
3377 * instead to skip to any following command, e.g. for:
3378 * :if 0 | call dict.foo().bar() | endif */
3379 eval0(eap->arg, &rettv, &eap->nextcmd, FALSE);
3380 return;
3381 }
3382
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003383 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003384 if (fudi.fd_newkey != NULL)
3385 {
3386 /* Still need to give an error message for missing key. */
3387 EMSG2(_(e_dictkey), fudi.fd_newkey);
3388 vim_free(fudi.fd_newkey);
3389 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003390 if (tofree == NULL)
3391 return;
3392
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003393 /* Increase refcount on dictionary, it could get deleted when evaluating
3394 * the arguments. */
3395 if (fudi.fd_dict != NULL)
3396 ++fudi.fd_dict->dv_refcount;
3397
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003399 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003400 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar532c7802005-01-27 14:44:31 +00003402 /* Skip white space to allow ":call func ()". Not good, but required for
3403 * backward compatibility. */
3404 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003405 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
3407 if (*startarg != '(')
3408 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003409 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 goto end;
3411 }
3412
3413 /*
3414 * When skipping, evaluate the function once, to find the end of the
3415 * arguments.
3416 * When the function takes a range, this is discovered after the first
3417 * call, and the loop is broken.
3418 */
3419 if (eap->skip)
3420 {
3421 ++emsg_skip;
3422 lnum = eap->line2; /* do it once, also with an invalid range */
3423 }
3424 else
3425 lnum = eap->line1;
3426 for ( ; lnum <= eap->line2; ++lnum)
3427 {
3428 if (!eap->skip && eap->addr_count > 0)
3429 {
3430 curwin->w_cursor.lnum = lnum;
3431 curwin->w_cursor.col = 0;
3432 }
3433 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003435 eap->line1, eap->line2, &doesrange,
3436 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
3438 failed = TRUE;
3439 break;
3440 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003441
3442 /* Handle a function returning a Funcref, Dictionary or List. */
3443 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3444 {
3445 failed = TRUE;
3446 break;
3447 }
3448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 if (doesrange || eap->skip)
3451 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003454 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003456 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (aborting())
3458 break;
3459 }
3460 if (eap->skip)
3461 --emsg_skip;
3462
3463 if (!failed)
3464 {
3465 /* Check for trailing illegal characters and a following command. */
3466 if (!ends_excmd(*arg))
3467 {
3468 emsg_severe = TRUE;
3469 EMSG(_(e_trailing));
3470 }
3471 else
3472 eap->nextcmd = check_nextcmd(arg);
3473 }
3474
3475end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003476 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478}
3479
3480/*
3481 * ":unlet[!] var1 ... " command.
3482 */
3483 void
3484ex_unlet(eap)
3485 exarg_T *eap;
3486{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003487 ex_unletlock(eap, eap->arg, 0);
3488}
3489
3490/*
3491 * ":lockvar" and ":unlockvar" commands
3492 */
3493 void
3494ex_lockvar(eap)
3495 exarg_T *eap;
3496{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003498 int deep = 2;
3499
3500 if (eap->forceit)
3501 deep = -1;
3502 else if (vim_isdigit(*arg))
3503 {
3504 deep = getdigits(&arg);
3505 arg = skipwhite(arg);
3506 }
3507
3508 ex_unletlock(eap, arg, deep);
3509}
3510
3511/*
3512 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3513 */
3514 static void
3515ex_unletlock(eap, argstart, deep)
3516 exarg_T *eap;
3517 char_u *argstart;
3518 int deep;
3519{
3520 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003523 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 do
3526 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003527 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003528 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3529 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003530 if (lv.ll_name == NULL)
3531 error = TRUE; /* error but continue parsing */
3532 if (name_end == NULL || (!vim_iswhite(*name_end)
3533 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003535 if (name_end != NULL)
3536 {
3537 emsg_severe = TRUE;
3538 EMSG(_(e_trailing));
3539 }
3540 if (!(eap->skip || error))
3541 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 break;
3543 }
3544
3545 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003546 {
3547 if (eap->cmdidx == CMD_unlet)
3548 {
3549 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3550 error = TRUE;
3551 }
3552 else
3553 {
3554 if (do_lock_var(&lv, name_end, deep,
3555 eap->cmdidx == CMD_lockvar) == FAIL)
3556 error = TRUE;
3557 }
3558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 if (!eap->skip)
3561 clear_lval(&lv);
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 arg = skipwhite(name_end);
3564 } while (!ends_excmd(*arg));
3565
3566 eap->nextcmd = check_nextcmd(arg);
3567}
3568
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003570do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573 int forceit;
3574{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003575 int ret = OK;
3576 int cc;
3577
3578 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003579 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003580 cc = *name_end;
3581 *name_end = NUL;
3582
3583 /* Normal name or expanded name. */
3584 if (check_changedtick(lp->ll_name))
3585 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3591 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003592 else if (lp->ll_range)
3593 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595
3596 /* Delete a range of List items. */
3597 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3598 {
3599 li = lp->ll_li->li_next;
3600 listitem_remove(lp->ll_list, lp->ll_li);
3601 lp->ll_li = li;
3602 ++lp->ll_n1;
3603 }
3604 }
3605 else
3606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 /* unlet a List item. */
3609 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003612 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 }
3614
3615 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616}
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618/*
3619 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 */
3622 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003625 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
Bram Moolenaar33570922005-01-25 22:26:29 +00003627 hashtab_T *ht;
3628 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003629 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003630 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaar33570922005-01-25 22:26:29 +00003632 ht = find_var_ht(name, &varname);
3633 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003635 hi = hash_find(ht, varname);
3636 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003637 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003638 di = HI2DI(hi);
3639 if (var_check_fixed(di->di_flags, name)
3640 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 return FAIL;
3642 delete_var(ht, hi);
3643 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 if (forceit)
3647 return OK;
3648 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return FAIL;
3650}
3651
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652/*
3653 * Lock or unlock variable indicated by "lp".
3654 * "deep" is the levels to go (-1 for unlimited);
3655 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3656 */
3657 static int
3658do_lock_var(lp, name_end, deep, lock)
3659 lval_T *lp;
3660 char_u *name_end;
3661 int deep;
3662 int lock;
3663{
3664 int ret = OK;
3665 int cc;
3666 dictitem_T *di;
3667
3668 if (deep == 0) /* nothing to do */
3669 return OK;
3670
3671 if (lp->ll_tv == NULL)
3672 {
3673 cc = *name_end;
3674 *name_end = NUL;
3675
3676 /* Normal name or expanded name. */
3677 if (check_changedtick(lp->ll_name))
3678 ret = FAIL;
3679 else
3680 {
3681 di = find_var(lp->ll_name, NULL);
3682 if (di == NULL)
3683 ret = FAIL;
3684 else
3685 {
3686 if (lock)
3687 di->di_flags |= DI_FLAGS_LOCK;
3688 else
3689 di->di_flags &= ~DI_FLAGS_LOCK;
3690 item_lock(&di->di_tv, deep, lock);
3691 }
3692 }
3693 *name_end = cc;
3694 }
3695 else if (lp->ll_range)
3696 {
3697 listitem_T *li = lp->ll_li;
3698
3699 /* (un)lock a range of List items. */
3700 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3701 {
3702 item_lock(&li->li_tv, deep, lock);
3703 li = li->li_next;
3704 ++lp->ll_n1;
3705 }
3706 }
3707 else if (lp->ll_list != NULL)
3708 /* (un)lock a List item. */
3709 item_lock(&lp->ll_li->li_tv, deep, lock);
3710 else
3711 /* un(lock) a Dictionary item. */
3712 item_lock(&lp->ll_di->di_tv, deep, lock);
3713
3714 return ret;
3715}
3716
3717/*
3718 * Lock or unlock an item. "deep" is nr of levels to go.
3719 */
3720 static void
3721item_lock(tv, deep, lock)
3722 typval_T *tv;
3723 int deep;
3724 int lock;
3725{
3726 static int recurse = 0;
3727 list_T *l;
3728 listitem_T *li;
3729 dict_T *d;
3730 hashitem_T *hi;
3731 int todo;
3732
3733 if (recurse >= DICT_MAXNEST)
3734 {
3735 EMSG(_("E743: variable nested too deep for (un)lock"));
3736 return;
3737 }
3738 if (deep == 0)
3739 return;
3740 ++recurse;
3741
3742 /* lock/unlock the item itself */
3743 if (lock)
3744 tv->v_lock |= VAR_LOCKED;
3745 else
3746 tv->v_lock &= ~VAR_LOCKED;
3747
3748 switch (tv->v_type)
3749 {
3750 case VAR_LIST:
3751 if ((l = tv->vval.v_list) != NULL)
3752 {
3753 if (lock)
3754 l->lv_lock |= VAR_LOCKED;
3755 else
3756 l->lv_lock &= ~VAR_LOCKED;
3757 if (deep < 0 || deep > 1)
3758 /* recursive: lock/unlock the items the List contains */
3759 for (li = l->lv_first; li != NULL; li = li->li_next)
3760 item_lock(&li->li_tv, deep - 1, lock);
3761 }
3762 break;
3763 case VAR_DICT:
3764 if ((d = tv->vval.v_dict) != NULL)
3765 {
3766 if (lock)
3767 d->dv_lock |= VAR_LOCKED;
3768 else
3769 d->dv_lock &= ~VAR_LOCKED;
3770 if (deep < 0 || deep > 1)
3771 {
3772 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003773 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3775 {
3776 if (!HASHITEM_EMPTY(hi))
3777 {
3778 --todo;
3779 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3780 }
3781 }
3782 }
3783 }
3784 }
3785 --recurse;
3786}
3787
Bram Moolenaara40058a2005-07-11 22:42:07 +00003788/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003789 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3790 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003791 */
3792 static int
3793tv_islocked(tv)
3794 typval_T *tv;
3795{
3796 return (tv->v_lock & VAR_LOCKED)
3797 || (tv->v_type == VAR_LIST
3798 && tv->vval.v_list != NULL
3799 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3800 || (tv->v_type == VAR_DICT
3801 && tv->vval.v_dict != NULL
3802 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3803}
3804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3806/*
3807 * Delete all "menutrans_" variables.
3808 */
3809 void
3810del_menutrans_vars()
3811{
Bram Moolenaar33570922005-01-25 22:26:29 +00003812 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003813 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
Bram Moolenaar33570922005-01-25 22:26:29 +00003815 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003816 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003817 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003818 {
3819 if (!HASHITEM_EMPTY(hi))
3820 {
3821 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003822 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3823 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003824 }
3825 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827}
3828#endif
3829
3830#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3831
3832/*
3833 * Local string buffer for the next two functions to store a variable name
3834 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3835 * get_user_var_name().
3836 */
3837
3838static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3839
3840static char_u *varnamebuf = NULL;
3841static int varnamebuflen = 0;
3842
3843/*
3844 * Function to concatenate a prefix and a variable name.
3845 */
3846 static char_u *
3847cat_prefix_varname(prefix, name)
3848 int prefix;
3849 char_u *name;
3850{
3851 int len;
3852
3853 len = (int)STRLEN(name) + 3;
3854 if (len > varnamebuflen)
3855 {
3856 vim_free(varnamebuf);
3857 len += 10; /* some additional space */
3858 varnamebuf = alloc(len);
3859 if (varnamebuf == NULL)
3860 {
3861 varnamebuflen = 0;
3862 return NULL;
3863 }
3864 varnamebuflen = len;
3865 }
3866 *varnamebuf = prefix;
3867 varnamebuf[1] = ':';
3868 STRCPY(varnamebuf + 2, name);
3869 return varnamebuf;
3870}
3871
3872/*
3873 * Function given to ExpandGeneric() to obtain the list of user defined
3874 * (global/buffer/window/built-in) variable names.
3875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 char_u *
3877get_user_var_name(xp, idx)
3878 expand_T *xp;
3879 int idx;
3880{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003881 static long_u gdone;
3882 static long_u bdone;
3883 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003884#ifdef FEAT_WINDOWS
3885 static long_u tdone;
3886#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003887 static int vidx;
3888 static hashitem_T *hi;
3889 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003892 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003893 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003894#ifdef FEAT_WINDOWS
3895 tdone = 0;
3896#endif
3897 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003898
3899 /* Global variables */
3900 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003902 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003903 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003904 else
3905 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003906 while (HASHITEM_EMPTY(hi))
3907 ++hi;
3908 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3909 return cat_prefix_varname('g', hi->hi_key);
3910 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003912
3913 /* b: variables */
3914 ht = &curbuf->b_vars.dv_hashtab;
3915 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003917 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003918 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003919 else
3920 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 while (HASHITEM_EMPTY(hi))
3922 ++hi;
3923 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003927 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 return (char_u *)"b:changedtick";
3929 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003930
3931 /* w: variables */
3932 ht = &curwin->w_vars.dv_hashtab;
3933 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003935 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003937 else
3938 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 while (HASHITEM_EMPTY(hi))
3940 ++hi;
3941 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003943
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003944#ifdef FEAT_WINDOWS
3945 /* t: variables */
3946 ht = &curtab->tp_vars.dv_hashtab;
3947 if (tdone < ht->ht_used)
3948 {
3949 if (tdone++ == 0)
3950 hi = ht->ht_array;
3951 else
3952 ++hi;
3953 while (HASHITEM_EMPTY(hi))
3954 ++hi;
3955 return cat_prefix_varname('t', hi->hi_key);
3956 }
3957#endif
3958
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 /* v: variables */
3960 if (vidx < VV_LEN)
3961 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 vim_free(varnamebuf);
3964 varnamebuf = NULL;
3965 varnamebuflen = 0;
3966 return NULL;
3967}
3968
3969#endif /* FEAT_CMDL_COMPL */
3970
3971/*
3972 * types for expressions.
3973 */
3974typedef enum
3975{
3976 TYPE_UNKNOWN = 0
3977 , TYPE_EQUAL /* == */
3978 , TYPE_NEQUAL /* != */
3979 , TYPE_GREATER /* > */
3980 , TYPE_GEQUAL /* >= */
3981 , TYPE_SMALLER /* < */
3982 , TYPE_SEQUAL /* <= */
3983 , TYPE_MATCH /* =~ */
3984 , TYPE_NOMATCH /* !~ */
3985} exptype_T;
3986
3987/*
3988 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3991 */
3992
3993/*
3994 * Handle zero level expression.
3995 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003997 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * Return OK or FAIL.
3999 */
4000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 char_u **nextcmd;
4005 int evaluate;
4006{
4007 int ret;
4008 char_u *p;
4009
4010 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (ret == FAIL || !ends_excmd(*p))
4013 {
4014 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 /*
4017 * Report the invalid expression unless the expression evaluation has
4018 * been cancelled due to an aborting error, an interrupt, or an
4019 * exception.
4020 */
4021 if (!aborting())
4022 EMSG2(_(e_invexpr2), arg);
4023 ret = FAIL;
4024 }
4025 if (nextcmd != NULL)
4026 *nextcmd = check_nextcmd(p);
4027
4028 return ret;
4029}
4030
4031/*
4032 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004033 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 *
4035 * "arg" must point to the first non-white of the expression.
4036 * "arg" is advanced to the next non-white after the recognized expression.
4037 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004038 * Note: "rettv.v_lock" is not set.
4039 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 * Return OK or FAIL.
4041 */
4042 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int evaluate;
4047{
4048 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051 /*
4052 * Get the first variable.
4053 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return FAIL;
4056
4057 if ((*arg)[0] == '?')
4058 {
4059 result = FALSE;
4060 if (evaluate)
4061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004062 int error = FALSE;
4063
4064 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 if (error)
4068 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070
4071 /*
4072 * Get the second variable.
4073 */
4074 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 return FAIL;
4077
4078 /*
4079 * Check for the ":".
4080 */
4081 if ((*arg)[0] != ':')
4082 {
4083 EMSG(_("E109: Missing ':' after '?'"));
4084 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return FAIL;
4087 }
4088
4089 /*
4090 * Get the third variable.
4091 */
4092 *arg = skipwhite(*arg + 1);
4093 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4094 {
4095 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 return FAIL;
4098 }
4099 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102
4103 return OK;
4104}
4105
4106/*
4107 * Handle first level expression:
4108 * expr2 || expr2 || expr2 logical OR
4109 *
4110 * "arg" must point to the first non-white of the expression.
4111 * "arg" is advanced to the next non-white after the recognized expression.
4112 *
4113 * Return OK or FAIL.
4114 */
4115 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 int evaluate;
4120{
Bram Moolenaar33570922005-01-25 22:26:29 +00004121 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 long result;
4123 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 /*
4127 * Get the first variable.
4128 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131
4132 /*
4133 * Repeat until there is no following "||".
4134 */
4135 first = TRUE;
4136 result = FALSE;
4137 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4138 {
4139 if (evaluate && first)
4140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004141 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 if (error)
4145 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 first = FALSE;
4147 }
4148
4149 /*
4150 * Get the second variable.
4151 */
4152 *arg = skipwhite(*arg + 2);
4153 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4154 return FAIL;
4155
4156 /*
4157 * Compute the result.
4158 */
4159 if (evaluate && !result)
4160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (error)
4165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 if (evaluate)
4168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 rettv->v_type = VAR_NUMBER;
4170 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 }
4173
4174 return OK;
4175}
4176
4177/*
4178 * Handle second level expression:
4179 * expr3 && expr3 && expr3 logical AND
4180 *
4181 * "arg" must point to the first non-white of the expression.
4182 * "arg" is advanced to the next non-white after the recognized expression.
4183 *
4184 * Return OK or FAIL.
4185 */
4186 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 int evaluate;
4191{
Bram Moolenaar33570922005-01-25 22:26:29 +00004192 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 long result;
4194 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197 /*
4198 * Get the first variable.
4199 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202
4203 /*
4204 * Repeat until there is no following "&&".
4205 */
4206 first = TRUE;
4207 result = TRUE;
4208 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4209 {
4210 if (evaluate && first)
4211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004212 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 if (error)
4216 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 first = FALSE;
4218 }
4219
4220 /*
4221 * Get the second variable.
4222 */
4223 *arg = skipwhite(*arg + 2);
4224 if (eval4(arg, &var2, evaluate && result) == FAIL)
4225 return FAIL;
4226
4227 /*
4228 * Compute the result.
4229 */
4230 if (evaluate && result)
4231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004235 if (error)
4236 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238 if (evaluate)
4239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 rettv->v_type = VAR_NUMBER;
4241 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244
4245 return OK;
4246}
4247
4248/*
4249 * Handle third level expression:
4250 * var1 == var2
4251 * var1 =~ var2
4252 * var1 != var2
4253 * var1 !~ var2
4254 * var1 > var2
4255 * var1 >= var2
4256 * var1 < var2
4257 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004258 * var1 is var2
4259 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 *
4261 * "arg" must point to the first non-white of the expression.
4262 * "arg" is advanced to the next non-white after the recognized expression.
4263 *
4264 * Return OK or FAIL.
4265 */
4266 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 int evaluate;
4271{
Bram Moolenaar33570922005-01-25 22:26:29 +00004272 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 char_u *p;
4274 int i;
4275 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004276 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 int len = 2;
4278 long n1, n2;
4279 char_u *s1, *s2;
4280 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4281 regmatch_T regmatch;
4282 int ic;
4283 char_u *save_cpo;
4284
4285 /*
4286 * Get the first variable.
4287 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return FAIL;
4290
4291 p = *arg;
4292 switch (p[0])
4293 {
4294 case '=': if (p[1] == '=')
4295 type = TYPE_EQUAL;
4296 else if (p[1] == '~')
4297 type = TYPE_MATCH;
4298 break;
4299 case '!': if (p[1] == '=')
4300 type = TYPE_NEQUAL;
4301 else if (p[1] == '~')
4302 type = TYPE_NOMATCH;
4303 break;
4304 case '>': if (p[1] != '=')
4305 {
4306 type = TYPE_GREATER;
4307 len = 1;
4308 }
4309 else
4310 type = TYPE_GEQUAL;
4311 break;
4312 case '<': if (p[1] != '=')
4313 {
4314 type = TYPE_SMALLER;
4315 len = 1;
4316 }
4317 else
4318 type = TYPE_SEQUAL;
4319 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004320 case 'i': if (p[1] == 's')
4321 {
4322 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4323 len = 5;
4324 if (!vim_isIDc(p[len]))
4325 {
4326 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4327 type_is = TRUE;
4328 }
4329 }
4330 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332
4333 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004334 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 */
4336 if (type != TYPE_UNKNOWN)
4337 {
4338 /* extra question mark appended: ignore case */
4339 if (p[len] == '?')
4340 {
4341 ic = TRUE;
4342 ++len;
4343 }
4344 /* extra '#' appended: match case */
4345 else if (p[len] == '#')
4346 {
4347 ic = FALSE;
4348 ++len;
4349 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004350 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 else
4352 ic = p_ic;
4353
4354 /*
4355 * Get the second variable.
4356 */
4357 *arg = skipwhite(p + len);
4358 if (eval5(arg, &var2, evaluate) == FAIL)
4359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 return FAIL;
4362 }
4363
4364 if (evaluate)
4365 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004366 if (type_is && rettv->v_type != var2.v_type)
4367 {
4368 /* For "is" a different type always means FALSE, for "notis"
4369 * it means TRUE. */
4370 n1 = (type == TYPE_NEQUAL);
4371 }
4372 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4373 {
4374 if (type_is)
4375 {
4376 n1 = (rettv->v_type == var2.v_type
4377 && rettv->vval.v_list == var2.vval.v_list);
4378 if (type == TYPE_NEQUAL)
4379 n1 = !n1;
4380 }
4381 else if (rettv->v_type != var2.v_type
4382 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4383 {
4384 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004385 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004387 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004388 clear_tv(rettv);
4389 clear_tv(&var2);
4390 return FAIL;
4391 }
4392 else
4393 {
4394 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004395 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4396 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 if (type == TYPE_NEQUAL)
4398 n1 = !n1;
4399 }
4400 }
4401
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004402 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4403 {
4404 if (type_is)
4405 {
4406 n1 = (rettv->v_type == var2.v_type
4407 && rettv->vval.v_dict == var2.vval.v_dict);
4408 if (type == TYPE_NEQUAL)
4409 n1 = !n1;
4410 }
4411 else if (rettv->v_type != var2.v_type
4412 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4413 {
4414 if (rettv->v_type != var2.v_type)
4415 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4416 else
4417 EMSG(_("E736: Invalid operation for Dictionary"));
4418 clear_tv(rettv);
4419 clear_tv(&var2);
4420 return FAIL;
4421 }
4422 else
4423 {
4424 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004425 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4426 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004427 if (type == TYPE_NEQUAL)
4428 n1 = !n1;
4429 }
4430 }
4431
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4433 {
4434 if (rettv->v_type != var2.v_type
4435 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4436 {
4437 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004438 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004440 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 clear_tv(rettv);
4442 clear_tv(&var2);
4443 return FAIL;
4444 }
4445 else
4446 {
4447 /* Compare two Funcrefs for being equal or unequal. */
4448 if (rettv->vval.v_string == NULL
4449 || var2.vval.v_string == NULL)
4450 n1 = FALSE;
4451 else
4452 n1 = STRCMP(rettv->vval.v_string,
4453 var2.vval.v_string) == 0;
4454 if (type == TYPE_NEQUAL)
4455 n1 = !n1;
4456 }
4457 }
4458
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004459#ifdef FEAT_FLOAT
4460 /*
4461 * If one of the two variables is a float, compare as a float.
4462 * When using "=~" or "!~", always compare as string.
4463 */
4464 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4465 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4466 {
4467 float_T f1, f2;
4468
4469 if (rettv->v_type == VAR_FLOAT)
4470 f1 = rettv->vval.v_float;
4471 else
4472 f1 = get_tv_number(rettv);
4473 if (var2.v_type == VAR_FLOAT)
4474 f2 = var2.vval.v_float;
4475 else
4476 f2 = get_tv_number(&var2);
4477 n1 = FALSE;
4478 switch (type)
4479 {
4480 case TYPE_EQUAL: n1 = (f1 == f2); break;
4481 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4482 case TYPE_GREATER: n1 = (f1 > f2); break;
4483 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4484 case TYPE_SMALLER: n1 = (f1 < f2); break;
4485 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4486 case TYPE_UNKNOWN:
4487 case TYPE_MATCH:
4488 case TYPE_NOMATCH: break; /* avoid gcc warning */
4489 }
4490 }
4491#endif
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 /*
4494 * If one of the two variables is a number, compare as a number.
4495 * When using "=~" or "!~", always compare as string.
4496 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 n1 = get_tv_number(rettv);
4501 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 switch (type)
4503 {
4504 case TYPE_EQUAL: n1 = (n1 == n2); break;
4505 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4506 case TYPE_GREATER: n1 = (n1 > n2); break;
4507 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4508 case TYPE_SMALLER: n1 = (n1 < n2); break;
4509 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4510 case TYPE_UNKNOWN:
4511 case TYPE_MATCH:
4512 case TYPE_NOMATCH: break; /* avoid gcc warning */
4513 }
4514 }
4515 else
4516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004517 s1 = get_tv_string_buf(rettv, buf1);
4518 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4520 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4521 else
4522 i = 0;
4523 n1 = FALSE;
4524 switch (type)
4525 {
4526 case TYPE_EQUAL: n1 = (i == 0); break;
4527 case TYPE_NEQUAL: n1 = (i != 0); break;
4528 case TYPE_GREATER: n1 = (i > 0); break;
4529 case TYPE_GEQUAL: n1 = (i >= 0); break;
4530 case TYPE_SMALLER: n1 = (i < 0); break;
4531 case TYPE_SEQUAL: n1 = (i <= 0); break;
4532
4533 case TYPE_MATCH:
4534 case TYPE_NOMATCH:
4535 /* avoid 'l' flag in 'cpoptions' */
4536 save_cpo = p_cpo;
4537 p_cpo = (char_u *)"";
4538 regmatch.regprog = vim_regcomp(s2,
4539 RE_MAGIC + RE_STRING);
4540 regmatch.rm_ic = ic;
4541 if (regmatch.regprog != NULL)
4542 {
4543 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4544 vim_free(regmatch.regprog);
4545 if (type == TYPE_NOMATCH)
4546 n1 = !n1;
4547 }
4548 p_cpo = save_cpo;
4549 break;
4550
4551 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4552 }
4553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 clear_tv(rettv);
4555 clear_tv(&var2);
4556 rettv->v_type = VAR_NUMBER;
4557 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559 }
4560
4561 return OK;
4562}
4563
4564/*
4565 * Handle fourth level expression:
4566 * + number addition
4567 * - number subtraction
4568 * . string concatenation
4569 *
4570 * "arg" must point to the first non-white of the expression.
4571 * "arg" is advanced to the next non-white after the recognized expression.
4572 *
4573 * Return OK or FAIL.
4574 */
4575 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 int evaluate;
4580{
Bram Moolenaar33570922005-01-25 22:26:29 +00004581 typval_T var2;
4582 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 int op;
4584 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004585#ifdef FEAT_FLOAT
4586 float_T f1 = 0, f2 = 0;
4587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 char_u *s1, *s2;
4589 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4590 char_u *p;
4591
4592 /*
4593 * Get the first variable.
4594 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004595 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 return FAIL;
4597
4598 /*
4599 * Repeat computing, until no '+', '-' or '.' is following.
4600 */
4601 for (;;)
4602 {
4603 op = **arg;
4604 if (op != '+' && op != '-' && op != '.')
4605 break;
4606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004607 if ((op != '+' || rettv->v_type != VAR_LIST)
4608#ifdef FEAT_FLOAT
4609 && (op == '.' || rettv->v_type != VAR_FLOAT)
4610#endif
4611 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004612 {
4613 /* For "list + ...", an illegal use of the first operand as
4614 * a number cannot be determined before evaluating the 2nd
4615 * operand: if this is also a list, all is ok.
4616 * For "something . ...", "something - ..." or "non-list + ...",
4617 * we know that the first operand needs to be a string or number
4618 * without evaluating the 2nd operand. So check before to avoid
4619 * side effects after an error. */
4620 if (evaluate && get_tv_string_chk(rettv) == NULL)
4621 {
4622 clear_tv(rettv);
4623 return FAIL;
4624 }
4625 }
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 /*
4628 * Get the second variable.
4629 */
4630 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004631 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return FAIL;
4635 }
4636
4637 if (evaluate)
4638 {
4639 /*
4640 * Compute the result.
4641 */
4642 if (op == '.')
4643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4645 s2 = get_tv_string_buf_chk(&var2, buf2);
4646 if (s2 == NULL) /* type error ? */
4647 {
4648 clear_tv(rettv);
4649 clear_tv(&var2);
4650 return FAIL;
4651 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004652 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 clear_tv(rettv);
4654 rettv->v_type = VAR_STRING;
4655 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004657 else if (op == '+' && rettv->v_type == VAR_LIST
4658 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004659 {
4660 /* concatenate Lists */
4661 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4662 &var3) == FAIL)
4663 {
4664 clear_tv(rettv);
4665 clear_tv(&var2);
4666 return FAIL;
4667 }
4668 clear_tv(rettv);
4669 *rettv = var3;
4670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 else
4672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 int error = FALSE;
4674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004675#ifdef FEAT_FLOAT
4676 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004677 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004678 f1 = rettv->vval.v_float;
4679 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004680 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004681 else
4682#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004684 n1 = get_tv_number_chk(rettv, &error);
4685 if (error)
4686 {
4687 /* This can only happen for "list + non-list". For
4688 * "non-list + ..." or "something - ...", we returned
4689 * before evaluating the 2nd operand. */
4690 clear_tv(rettv);
4691 return FAIL;
4692 }
4693#ifdef FEAT_FLOAT
4694 if (var2.v_type == VAR_FLOAT)
4695 f1 = n1;
4696#endif
4697 }
4698#ifdef FEAT_FLOAT
4699 if (var2.v_type == VAR_FLOAT)
4700 {
4701 f2 = var2.vval.v_float;
4702 n2 = 0;
4703 }
4704 else
4705#endif
4706 {
4707 n2 = get_tv_number_chk(&var2, &error);
4708 if (error)
4709 {
4710 clear_tv(rettv);
4711 clear_tv(&var2);
4712 return FAIL;
4713 }
4714#ifdef FEAT_FLOAT
4715 if (rettv->v_type == VAR_FLOAT)
4716 f2 = n2;
4717#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720
4721#ifdef FEAT_FLOAT
4722 /* If there is a float on either side the result is a float. */
4723 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4724 {
4725 if (op == '+')
4726 f1 = f1 + f2;
4727 else
4728 f1 = f1 - f2;
4729 rettv->v_type = VAR_FLOAT;
4730 rettv->vval.v_float = f1;
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733#endif
4734 {
4735 if (op == '+')
4736 n1 = n1 + n2;
4737 else
4738 n1 = n1 - n2;
4739 rettv->v_type = VAR_NUMBER;
4740 rettv->vval.v_number = n1;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 }
4746 return OK;
4747}
4748
4749/*
4750 * Handle fifth level expression:
4751 * * number multiplication
4752 * / number division
4753 * % number modulo
4754 *
4755 * "arg" must point to the first non-white of the expression.
4756 * "arg" is advanced to the next non-white after the recognized expression.
4757 *
4758 * Return OK or FAIL.
4759 */
4760 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004761eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004765 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 int op;
4769 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#ifdef FEAT_FLOAT
4771 int use_float = FALSE;
4772 float_T f1 = 0, f2;
4773#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
4776 /*
4777 * Get the first variable.
4778 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004779 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return FAIL;
4781
4782 /*
4783 * Repeat computing, until no '*', '/' or '%' is following.
4784 */
4785 for (;;)
4786 {
4787 op = **arg;
4788 if (op != '*' && op != '/' && op != '%')
4789 break;
4790
4791 if (evaluate)
4792 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793#ifdef FEAT_FLOAT
4794 if (rettv->v_type == VAR_FLOAT)
4795 {
4796 f1 = rettv->vval.v_float;
4797 use_float = TRUE;
4798 n1 = 0;
4799 }
4800 else
4801#endif
4802 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004803 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 if (error)
4805 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 else
4808 n1 = 0;
4809
4810 /*
4811 * Get the second variable.
4812 */
4813 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004814 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 return FAIL;
4816
4817 if (evaluate)
4818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819#ifdef FEAT_FLOAT
4820 if (var2.v_type == VAR_FLOAT)
4821 {
4822 if (!use_float)
4823 {
4824 f1 = n1;
4825 use_float = TRUE;
4826 }
4827 f2 = var2.vval.v_float;
4828 n2 = 0;
4829 }
4830 else
4831#endif
4832 {
4833 n2 = get_tv_number_chk(&var2, &error);
4834 clear_tv(&var2);
4835 if (error)
4836 return FAIL;
4837#ifdef FEAT_FLOAT
4838 if (use_float)
4839 f2 = n2;
4840#endif
4841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 /*
4844 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847#ifdef FEAT_FLOAT
4848 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850 if (op == '*')
4851 f1 = f1 * f2;
4852 else if (op == '/')
4853 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004854# ifdef VMS
4855 /* VMS crashes on divide by zero, work around it */
4856 if (f2 == 0.0)
4857 {
4858 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004859 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004860 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004861 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004862 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004863 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004864 }
4865 else
4866 f1 = f1 / f2;
4867# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 /* We rely on the floating point library to handle divide
4869 * by zero to result in "inf" and not a crash. */
4870 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004871# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004875 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 return FAIL;
4877 }
4878 rettv->v_type = VAR_FLOAT;
4879 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004884 if (op == '*')
4885 n1 = n1 * n2;
4886 else if (op == '/')
4887 {
4888 if (n2 == 0) /* give an error message? */
4889 {
4890 if (n1 == 0)
4891 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4892 else if (n1 < 0)
4893 n1 = -0x7fffffffL;
4894 else
4895 n1 = 0x7fffffffL;
4896 }
4897 else
4898 n1 = n1 / n2;
4899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 {
4902 if (n2 == 0) /* give an error message? */
4903 n1 = 0;
4904 else
4905 n1 = n1 % n2;
4906 }
4907 rettv->v_type = VAR_NUMBER;
4908 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 }
4912
4913 return OK;
4914}
4915
4916/*
4917 * Handle sixth level expression:
4918 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004919 * "string" string constant
4920 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 * &option-name option value
4922 * @r register contents
4923 * identifier variable value
4924 * function() function call
4925 * $VAR environment variable
4926 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004927 * [expr, expr] List
4928 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 *
4930 * Also handle:
4931 * ! in front logical NOT
4932 * - in front unary minus
4933 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 * trailing [] subscript in String or List
4935 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 *
4937 * "arg" must point to the first non-white of the expression.
4938 * "arg" is advanced to the next non-white after the recognized expression.
4939 *
4940 * Return OK or FAIL.
4941 */
4942 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004943eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004947 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 long n;
4950 int len;
4951 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 char_u *start_leader, *end_leader;
4953 int ret = OK;
4954 char_u *alias;
4955
4956 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004957 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004960 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 /*
4963 * Skip '!' and '-' characters. They are handled later.
4964 */
4965 start_leader = *arg;
4966 while (**arg == '!' || **arg == '-' || **arg == '+')
4967 *arg = skipwhite(*arg + 1);
4968 end_leader = *arg;
4969
4970 switch (**arg)
4971 {
4972 /*
4973 * Number constant.
4974 */
4975 case '0':
4976 case '1':
4977 case '2':
4978 case '3':
4979 case '4':
4980 case '5':
4981 case '6':
4982 case '7':
4983 case '8':
4984 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
4986#ifdef FEAT_FLOAT
4987 char_u *p = skipdigits(*arg + 1);
4988 int get_float = FALSE;
4989
4990 /* We accept a float when the format matches
4991 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004992 * strict to avoid backwards compatibility problems.
4993 * Don't look for a float after the "." operator, so that
4994 * ":let vers = 1.2.3" doesn't fail. */
4995 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 get_float = TRUE;
4998 p = skipdigits(p + 2);
4999 if (*p == 'e' || *p == 'E')
5000 {
5001 ++p;
5002 if (*p == '-' || *p == '+')
5003 ++p;
5004 if (!vim_isdigit(*p))
5005 get_float = FALSE;
5006 else
5007 p = skipdigits(p + 1);
5008 }
5009 if (ASCII_ISALPHA(*p) || *p == '.')
5010 get_float = FALSE;
5011 }
5012 if (get_float)
5013 {
5014 float_T f;
5015
5016 *arg += string2float(*arg, &f);
5017 if (evaluate)
5018 {
5019 rettv->v_type = VAR_FLOAT;
5020 rettv->vval.v_float = f;
5021 }
5022 }
5023 else
5024#endif
5025 {
5026 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5027 *arg += len;
5028 if (evaluate)
5029 {
5030 rettv->v_type = VAR_NUMBER;
5031 rettv->vval.v_number = n;
5032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037 /*
5038 * String constant: "string".
5039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 break;
5042
5043 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005047 break;
5048
5049 /*
5050 * List: [expr, expr]
5051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054
5055 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 * Dictionary: {key: val, key: val}
5057 */
5058 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5059 break;
5060
5061 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005062 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005064 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 break;
5066
5067 /*
5068 * Environment variable: $VAR.
5069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 break;
5072
5073 /*
5074 * Register contents: @r.
5075 */
5076 case '@': ++*arg;
5077 if (evaluate)
5078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005080 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 if (**arg != NUL)
5083 ++*arg;
5084 break;
5085
5086 /*
5087 * nested expression: (expression).
5088 */
5089 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 if (**arg == ')')
5092 ++*arg;
5093 else if (ret == OK)
5094 {
5095 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 ret = FAIL;
5098 }
5099 break;
5100
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104
5105 if (ret == NOTDONE)
5106 {
5107 /*
5108 * Must be a variable or function name.
5109 * Can also be a curly-braces kind of name: {expr}.
5110 */
5111 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005112 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 if (alias != NULL)
5114 s = alias;
5115
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005116 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 ret = FAIL;
5118 else
5119 {
5120 if (**arg == '(') /* recursive! */
5121 {
5122 /* If "s" is the name of a variable of type VAR_FUNC
5123 * use its contents. */
5124 s = deref_func_name(s, &len);
5125
5126 /* Invoke the function. */
5127 ret = get_func_tv(s, len, rettv, arg,
5128 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005129 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 /* Stop the expression evaluation when immediately
5131 * aborting on error, or when an interrupt occurred or
5132 * an exception was thrown but not caught. */
5133 if (aborting())
5134 {
5135 if (ret == OK)
5136 clear_tv(rettv);
5137 ret = FAIL;
5138 }
5139 }
5140 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005141 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005142 else
5143 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005145 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 }
5147
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 *arg = skipwhite(*arg);
5149
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005150 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5151 * expr(expr). */
5152 if (ret == OK)
5153 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
5155 /*
5156 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5157 */
5158 if (ret == OK && evaluate && end_leader > start_leader)
5159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005160 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005161 int val = 0;
5162#ifdef FEAT_FLOAT
5163 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165 if (rettv->v_type == VAR_FLOAT)
5166 f = rettv->vval.v_float;
5167 else
5168#endif
5169 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005170 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005172 clear_tv(rettv);
5173 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 else
5176 {
5177 while (end_leader > start_leader)
5178 {
5179 --end_leader;
5180 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005181 {
5182#ifdef FEAT_FLOAT
5183 if (rettv->v_type == VAR_FLOAT)
5184 f = !f;
5185 else
5186#endif
5187 val = !val;
5188 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005189 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005190 {
5191#ifdef FEAT_FLOAT
5192 if (rettv->v_type == VAR_FLOAT)
5193 f = -f;
5194 else
5195#endif
5196 val = -val;
5197 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005198 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005199#ifdef FEAT_FLOAT
5200 if (rettv->v_type == VAR_FLOAT)
5201 {
5202 clear_tv(rettv);
5203 rettv->vval.v_float = f;
5204 }
5205 else
5206#endif
5207 {
5208 clear_tv(rettv);
5209 rettv->v_type = VAR_NUMBER;
5210 rettv->vval.v_number = val;
5211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214
5215 return ret;
5216}
5217
5218/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005219 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5220 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5222 */
5223 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005226 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229{
5230 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005231 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 long len = -1;
5234 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005235 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005238 if (rettv->v_type == VAR_FUNC
5239#ifdef FEAT_FLOAT
5240 || rettv->v_type == VAR_FLOAT
5241#endif
5242 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 if (verbose)
5245 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 return FAIL;
5247 }
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 /*
5252 * dict.name
5253 */
5254 key = *arg + 1;
5255 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5256 ;
5257 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 }
5261 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /*
5264 * something[idx]
5265 *
5266 * Get the (first) variable from inside the [].
5267 */
5268 *arg = skipwhite(*arg + 1);
5269 if (**arg == ':')
5270 empty1 = TRUE;
5271 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5272 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5274 {
5275 /* not a number or string */
5276 clear_tv(&var1);
5277 return FAIL;
5278 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279
5280 /*
5281 * Get the second variable from inside the [:].
5282 */
5283 if (**arg == ':')
5284 {
5285 range = TRUE;
5286 *arg = skipwhite(*arg + 1);
5287 if (**arg == ']')
5288 empty2 = TRUE;
5289 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 if (!empty1)
5292 clear_tv(&var1);
5293 return FAIL;
5294 }
5295 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5296 {
5297 /* not a number or string */
5298 if (!empty1)
5299 clear_tv(&var1);
5300 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 return FAIL;
5302 }
5303 }
5304
5305 /* Check for the ']'. */
5306 if (**arg != ']')
5307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005308 if (verbose)
5309 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 clear_tv(&var1);
5311 if (range)
5312 clear_tv(&var2);
5313 return FAIL;
5314 }
5315 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 }
5317
5318 if (evaluate)
5319 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 n1 = 0;
5321 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005323 n1 = get_tv_number(&var1);
5324 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 }
5326 if (range)
5327 {
5328 if (empty2)
5329 n2 = -1;
5330 else
5331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005332 n2 = get_tv_number(&var2);
5333 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 }
5335 }
5336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005337 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 {
5339 case VAR_NUMBER:
5340 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005341 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 len = (long)STRLEN(s);
5343 if (range)
5344 {
5345 /* The resulting variable is a substring. If the indexes
5346 * are out of range the result is empty. */
5347 if (n1 < 0)
5348 {
5349 n1 = len + n1;
5350 if (n1 < 0)
5351 n1 = 0;
5352 }
5353 if (n2 < 0)
5354 n2 = len + n2;
5355 else if (n2 >= len)
5356 n2 = len;
5357 if (n1 >= len || n2 < 0 || n1 > n2)
5358 s = NULL;
5359 else
5360 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5361 }
5362 else
5363 {
5364 /* The resulting variable is a string of a single
5365 * character. If the index is too big or negative the
5366 * result is empty. */
5367 if (n1 >= len || n1 < 0)
5368 s = NULL;
5369 else
5370 s = vim_strnsave(s + n1, 1);
5371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 clear_tv(rettv);
5373 rettv->v_type = VAR_STRING;
5374 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 break;
5376
5377 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 if (n1 < 0)
5380 n1 = len + n1;
5381 if (!empty1 && (n1 < 0 || n1 >= len))
5382 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005383 /* For a range we allow invalid values and return an empty
5384 * list. A list index out of range is an error. */
5385 if (!range)
5386 {
5387 if (verbose)
5388 EMSGN(_(e_listidx), n1);
5389 return FAIL;
5390 }
5391 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 }
5393 if (range)
5394 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 list_T *l;
5396 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397
5398 if (n2 < 0)
5399 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005400 else if (n2 >= len)
5401 n2 = len - 1;
5402 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005403 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 l = list_alloc();
5405 if (l == NULL)
5406 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 n1 <= n2; ++n1)
5409 {
5410 if (list_append_tv(l, &item->li_tv) == FAIL)
5411 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005412 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 return FAIL;
5414 }
5415 item = item->li_next;
5416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417 clear_tv(rettv);
5418 rettv->v_type = VAR_LIST;
5419 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005420 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 }
5422 else
5423 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005424 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 clear_tv(rettv);
5426 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 }
5428 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429
5430 case VAR_DICT:
5431 if (range)
5432 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005433 if (verbose)
5434 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 if (len == -1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441
5442 if (len == -1)
5443 {
5444 key = get_tv_string(&var1);
5445 if (*key == NUL)
5446 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005447 if (verbose)
5448 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005449 clear_tv(&var1);
5450 return FAIL;
5451 }
5452 }
5453
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005454 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005456 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005457 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 if (len == -1)
5459 clear_tv(&var1);
5460 if (item == NULL)
5461 return FAIL;
5462
5463 copy_tv(&item->di_tv, &var1);
5464 clear_tv(rettv);
5465 *rettv = var1;
5466 }
5467 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 }
5470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 return OK;
5472}
5473
5474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 * Get an option value.
5476 * "arg" points to the '&' or '+' before the option name.
5477 * "arg" is advanced to character after the option name.
5478 * Return OK or FAIL.
5479 */
5480 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 int evaluate;
5485{
5486 char_u *option_end;
5487 long numval;
5488 char_u *stringval;
5489 int opt_type;
5490 int c;
5491 int working = (**arg == '+'); /* has("+option") */
5492 int ret = OK;
5493 int opt_flags;
5494
5495 /*
5496 * Isolate the option name and find its value.
5497 */
5498 option_end = find_option_end(arg, &opt_flags);
5499 if (option_end == NULL)
5500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005501 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 EMSG2(_("E112: Option name missing: %s"), *arg);
5503 return FAIL;
5504 }
5505
5506 if (!evaluate)
5507 {
5508 *arg = option_end;
5509 return OK;
5510 }
5511
5512 c = *option_end;
5513 *option_end = NUL;
5514 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 if (opt_type == -3) /* invalid name */
5518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 EMSG2(_("E113: Unknown option: %s"), *arg);
5521 ret = FAIL;
5522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
5525 if (opt_type == -2) /* hidden string option */
5526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 rettv->v_type = VAR_STRING;
5528 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
5530 else if (opt_type == -1) /* hidden number option */
5531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 rettv->v_type = VAR_NUMBER;
5533 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535 else if (opt_type == 1) /* number option */
5536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 rettv->v_type = VAR_NUMBER;
5538 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
5540 else /* string option */
5541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 rettv->v_type = VAR_STRING;
5543 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
5545 }
5546 else if (working && (opt_type == -2 || opt_type == -1))
5547 ret = FAIL;
5548
5549 *option_end = c; /* put back for error messages */
5550 *arg = option_end;
5551
5552 return ret;
5553}
5554
5555/*
5556 * Allocate a variable for a string constant.
5557 * Return OK or FAIL.
5558 */
5559 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 int evaluate;
5564{
5565 char_u *p;
5566 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 int extra = 0;
5568
5569 /*
5570 * Find the end of the string, skipping backslashed characters.
5571 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005572 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
5574 if (*p == '\\' && p[1] != NUL)
5575 {
5576 ++p;
5577 /* A "\<x>" form occupies at least 4 characters, and produces up
5578 * to 6 characters: reserve space for 2 extra */
5579 if (*p == '<')
5580 extra += 2;
5581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583
5584 if (*p != '"')
5585 {
5586 EMSG2(_("E114: Missing quote: %s"), *arg);
5587 return FAIL;
5588 }
5589
5590 /* If only parsing, set *arg and return here */
5591 if (!evaluate)
5592 {
5593 *arg = p + 1;
5594 return OK;
5595 }
5596
5597 /*
5598 * Copy the string into allocated memory, handling backslashed
5599 * characters.
5600 */
5601 name = alloc((unsigned)(p - *arg + extra));
5602 if (name == NULL)
5603 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 if (*p == '\\')
5610 {
5611 switch (*++p)
5612 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 case 'b': *name++ = BS; ++p; break;
5614 case 'e': *name++ = ESC; ++p; break;
5615 case 'f': *name++ = FF; ++p; break;
5616 case 'n': *name++ = NL; ++p; break;
5617 case 'r': *name++ = CAR; ++p; break;
5618 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 case 'X': /* hex: "\x1", "\x12" */
5621 case 'x':
5622 case 'u': /* Unicode: "\u0023" */
5623 case 'U':
5624 if (vim_isxdigit(p[1]))
5625 {
5626 int n, nr;
5627 int c = toupper(*p);
5628
5629 if (c == 'X')
5630 n = 2;
5631 else
5632 n = 4;
5633 nr = 0;
5634 while (--n >= 0 && vim_isxdigit(p[1]))
5635 {
5636 ++p;
5637 nr = (nr << 4) + hex2nr(*p);
5638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005639 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640#ifdef FEAT_MBYTE
5641 /* For "\u" store the number according to
5642 * 'encoding'. */
5643 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 else
5646#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 break;
5650
5651 /* octal: "\1", "\12", "\123" */
5652 case '0':
5653 case '1':
5654 case '2':
5655 case '3':
5656 case '4':
5657 case '5':
5658 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 case '7': *name = *p++ - '0';
5660 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 *name = (*name << 3) + *p++ - '0';
5663 if (*p >= '0' && *p <= '7')
5664 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 break;
5668
5669 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (extra != 0)
5672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 break;
5675 }
5676 /* FALLTHROUGH */
5677
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 break;
5680 }
5681 }
5682 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 *arg = p + 1;
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 return OK;
5690}
5691
5692/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 * Return OK or FAIL.
5695 */
5696 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 int evaluate;
5701{
5702 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005703 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 int reduce = 0;
5705
5706 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005708 */
5709 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5710 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005714 break;
5715 ++reduce;
5716 ++p;
5717 }
5718 }
5719
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723 return FAIL;
5724 }
5725
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 if (!evaluate)
5728 {
5729 *arg = p + 1;
5730 return OK;
5731 }
5732
5733 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 */
5736 str = alloc((unsigned)((p - *arg) - reduce));
5737 if (str == NULL)
5738 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 rettv->v_type = VAR_STRING;
5740 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 break;
5748 ++p;
5749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 *arg = p + 1;
5754
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 return OK;
5756}
5757
5758/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 * Allocate a variable for a List and fill it from "*arg".
5760 * Return OK or FAIL.
5761 */
5762 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005763get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005765 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 int evaluate;
5767{
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 list_T *l = NULL;
5769 typval_T tv;
5770 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771
5772 if (evaluate)
5773 {
5774 l = list_alloc();
5775 if (l == NULL)
5776 return FAIL;
5777 }
5778
5779 *arg = skipwhite(*arg + 1);
5780 while (**arg != ']' && **arg != NUL)
5781 {
5782 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5783 goto failret;
5784 if (evaluate)
5785 {
5786 item = listitem_alloc();
5787 if (item != NULL)
5788 {
5789 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005790 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 list_append(l, item);
5792 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005793 else
5794 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 }
5796
5797 if (**arg == ']')
5798 break;
5799 if (**arg != ',')
5800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 goto failret;
5803 }
5804 *arg = skipwhite(*arg + 1);
5805 }
5806
5807 if (**arg != ']')
5808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810failret:
5811 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005812 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 return FAIL;
5814 }
5815
5816 *arg = skipwhite(*arg + 1);
5817 if (evaluate)
5818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005819 rettv->v_type = VAR_LIST;
5820 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 ++l->lv_refcount;
5822 }
5823
5824 return OK;
5825}
5826
5827/*
5828 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005829 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005831 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832list_alloc()
5833{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005834 list_T *l;
5835
5836 l = (list_T *)alloc_clear(sizeof(list_T));
5837 if (l != NULL)
5838 {
5839 /* Prepend the list to the list of lists for garbage collection. */
5840 if (first_list != NULL)
5841 first_list->lv_used_prev = l;
5842 l->lv_used_prev = NULL;
5843 l->lv_used_next = first_list;
5844 first_list = l;
5845 }
5846 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847}
5848
5849/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005850 * Allocate an empty list for a return value.
5851 * Returns OK or FAIL.
5852 */
5853 static int
5854rettv_list_alloc(rettv)
5855 typval_T *rettv;
5856{
5857 list_T *l = list_alloc();
5858
5859 if (l == NULL)
5860 return FAIL;
5861
5862 rettv->vval.v_list = l;
5863 rettv->v_type = VAR_LIST;
5864 ++l->lv_refcount;
5865 return OK;
5866}
5867
5868/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869 * Unreference a list: decrement the reference count and free it when it
5870 * becomes zero.
5871 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005872 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005876 if (l != NULL && --l->lv_refcount <= 0)
5877 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878}
5879
5880/*
5881 * Free a list, including all items it points to.
5882 * Ignores the reference count.
5883 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005884 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005885list_free(l, recurse)
5886 list_T *l;
5887 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888{
Bram Moolenaar33570922005-01-25 22:26:29 +00005889 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 /* Remove the list from the list of lists for garbage collection. */
5892 if (l->lv_used_prev == NULL)
5893 first_list = l->lv_used_next;
5894 else
5895 l->lv_used_prev->lv_used_next = l->lv_used_next;
5896 if (l->lv_used_next != NULL)
5897 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5898
Bram Moolenaard9fba312005-06-26 22:34:35 +00005899 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005901 /* Remove the item before deleting it. */
5902 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005903 if (recurse || (item->li_tv.v_type != VAR_LIST
5904 && item->li_tv.v_type != VAR_DICT))
5905 clear_tv(&item->li_tv);
5906 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 }
5908 vim_free(l);
5909}
5910
5911/*
5912 * Allocate a list item.
5913 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915listitem_alloc()
5916{
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918}
5919
5920/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005921 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 */
5923 static void
5924listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005927 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 vim_free(item);
5929}
5930
5931/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005932 * Remove a list item from a List and free it. Also clears the value.
5933 */
5934 static void
5935listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
5937 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005938{
5939 list_remove(l, item, item);
5940 listitem_free(item);
5941}
5942
5943/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 * Get the number of items in a list.
5945 */
5946 static long
5947list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 if (l == NULL)
5951 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005952 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953}
5954
5955/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005956 * Return TRUE when two lists have exactly the same values.
5957 */
5958 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005959list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 list_T *l1;
5961 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005962 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005963 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964{
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005966
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005967 if (l1 == NULL || l2 == NULL)
5968 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005969 if (l1 == l2)
5970 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005971 if (list_len(l1) != list_len(l2))
5972 return FALSE;
5973
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974 for (item1 = l1->lv_first, item2 = l2->lv_first;
5975 item1 != NULL && item2 != NULL;
5976 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005977 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978 return FALSE;
5979 return item1 == NULL && item2 == NULL;
5980}
5981
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005982#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5983 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005984/*
5985 * Return the dictitem that an entry in a hashtable points to.
5986 */
5987 dictitem_T *
5988dict_lookup(hi)
5989 hashitem_T *hi;
5990{
5991 return HI2DI(hi);
5992}
5993#endif
5994
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005996 * Return TRUE when two dictionaries have exactly the same key/values.
5997 */
5998 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005999dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 dict_T *d1;
6001 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006002 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 hashitem_T *hi;
6006 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006007 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006008
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006009 if (d1 == NULL || d2 == NULL)
6010 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006011 if (d1 == d2)
6012 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006013 if (dict_len(d1) != dict_len(d2))
6014 return FALSE;
6015
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006016 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006019 if (!HASHITEM_EMPTY(hi))
6020 {
6021 item2 = dict_find(d2, hi->hi_key, -1);
6022 if (item2 == NULL)
6023 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006024 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006025 return FALSE;
6026 --todo;
6027 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006028 }
6029 return TRUE;
6030}
6031
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032static int tv_equal_recurse_limit;
6033
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006034/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006036 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006037 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006038 */
6039 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 typval_T *tv1;
6042 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043 int ic; /* ignore case */
6044 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006045{
6046 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006047 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006048 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006049 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006050
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006051 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006052 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006053
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006054 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 * recursiveness to a limit. We guess they are equal then.
6056 * A fixed limit has the problem of still taking an awful long time.
6057 * Reduce the limit every time running into it. That should work fine for
6058 * deeply linked structures that are not recursively linked and catch
6059 * recursiveness quickly. */
6060 if (!recursive)
6061 tv_equal_recurse_limit = 1000;
6062 if (recursive_cnt >= tv_equal_recurse_limit)
6063 {
6064 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006065 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006066 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006067
6068 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006069 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006070 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071 ++recursive_cnt;
6072 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6073 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006074 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006075
6076 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077 ++recursive_cnt;
6078 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6079 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006080 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006081
6082 case VAR_FUNC:
6083 return (tv1->vval.v_string != NULL
6084 && tv2->vval.v_string != NULL
6085 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6086
6087 case VAR_NUMBER:
6088 return tv1->vval.v_number == tv2->vval.v_number;
6089
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006090#ifdef FEAT_FLOAT
6091 case VAR_FLOAT:
6092 return tv1->vval.v_float == tv2->vval.v_float;
6093#endif
6094
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006095 case VAR_STRING:
6096 s1 = get_tv_string_buf(tv1, buf1);
6097 s2 = get_tv_string_buf(tv2, buf2);
6098 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006100
6101 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 return TRUE;
6103}
6104
6105/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106 * Locate item with index "n" in list "l" and return it.
6107 * A negative index is counted from the end; -1 is the last item.
6108 * Returns NULL when "n" is out of range.
6109 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113 long n;
6114{
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 long idx;
6117
6118 if (l == NULL)
6119 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006120
6121 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006123 n = l->lv_len + n;
6124
6125 /* Check for index out of range. */
6126 if (n < 0 || n >= l->lv_len)
6127 return NULL;
6128
6129 /* When there is a cached index may start search from there. */
6130 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006132 if (n < l->lv_idx / 2)
6133 {
6134 /* closest to the start of the list */
6135 item = l->lv_first;
6136 idx = 0;
6137 }
6138 else if (n > (l->lv_idx + l->lv_len) / 2)
6139 {
6140 /* closest to the end of the list */
6141 item = l->lv_last;
6142 idx = l->lv_len - 1;
6143 }
6144 else
6145 {
6146 /* closest to the cached index */
6147 item = l->lv_idx_item;
6148 idx = l->lv_idx;
6149 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 }
6151 else
6152 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006153 if (n < l->lv_len / 2)
6154 {
6155 /* closest to the start of the list */
6156 item = l->lv_first;
6157 idx = 0;
6158 }
6159 else
6160 {
6161 /* closest to the end of the list */
6162 item = l->lv_last;
6163 idx = l->lv_len - 1;
6164 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006166
6167 while (n > idx)
6168 {
6169 /* search forward */
6170 item = item->li_next;
6171 ++idx;
6172 }
6173 while (n < idx)
6174 {
6175 /* search backward */
6176 item = item->li_prev;
6177 --idx;
6178 }
6179
6180 /* cache the used index */
6181 l->lv_idx = idx;
6182 l->lv_idx_item = item;
6183
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 return item;
6185}
6186
6187/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006188 * Get list item "l[idx]" as a number.
6189 */
6190 static long
6191list_find_nr(l, idx, errorp)
6192 list_T *l;
6193 long idx;
6194 int *errorp; /* set to TRUE when something wrong */
6195{
6196 listitem_T *li;
6197
6198 li = list_find(l, idx);
6199 if (li == NULL)
6200 {
6201 if (errorp != NULL)
6202 *errorp = TRUE;
6203 return -1L;
6204 }
6205 return get_tv_number_chk(&li->li_tv, errorp);
6206}
6207
6208/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006209 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6210 */
6211 char_u *
6212list_find_str(l, idx)
6213 list_T *l;
6214 long idx;
6215{
6216 listitem_T *li;
6217
6218 li = list_find(l, idx - 1);
6219 if (li == NULL)
6220 {
6221 EMSGN(_(e_listidx), idx);
6222 return NULL;
6223 }
6224 return get_tv_string(&li->li_tv);
6225}
6226
6227/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006228 * Locate "item" list "l" and return its index.
6229 * Returns -1 when "item" is not in the list.
6230 */
6231 static long
6232list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 list_T *l;
6234 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006235{
6236 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006238
6239 if (l == NULL)
6240 return -1;
6241 idx = 0;
6242 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6243 ++idx;
6244 if (li == NULL)
6245 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006246 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006247}
6248
6249/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 * Append item "item" to the end of list "l".
6251 */
6252 static void
6253list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 list_T *l;
6255 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256{
6257 if (l->lv_last == NULL)
6258 {
6259 /* empty list */
6260 l->lv_first = item;
6261 l->lv_last = item;
6262 item->li_prev = NULL;
6263 }
6264 else
6265 {
6266 l->lv_last->li_next = item;
6267 item->li_prev = l->lv_last;
6268 l->lv_last = item;
6269 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 item->li_next = NULL;
6272}
6273
6274/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006276 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006278 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 list_T *l;
6281 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006283 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284
Bram Moolenaar05159a02005-02-26 23:04:13 +00006285 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006287 copy_tv(tv, &li->li_tv);
6288 list_append(l, li);
6289 return OK;
6290}
6291
6292/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006293 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006294 * Return FAIL when out of memory.
6295 */
6296 int
6297list_append_dict(list, dict)
6298 list_T *list;
6299 dict_T *dict;
6300{
6301 listitem_T *li = listitem_alloc();
6302
6303 if (li == NULL)
6304 return FAIL;
6305 li->li_tv.v_type = VAR_DICT;
6306 li->li_tv.v_lock = 0;
6307 li->li_tv.vval.v_dict = dict;
6308 list_append(list, li);
6309 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 return OK;
6311}
6312
6313/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006314 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006315 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006316 * Returns FAIL when out of memory.
6317 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006318 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006319list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006320 list_T *l;
6321 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006322 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006323{
6324 listitem_T *li = listitem_alloc();
6325
6326 if (li == NULL)
6327 return FAIL;
6328 list_append(l, li);
6329 li->li_tv.v_type = VAR_STRING;
6330 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006331 if (str == NULL)
6332 li->li_tv.vval.v_string = NULL;
6333 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006334 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006335 return FAIL;
6336 return OK;
6337}
6338
6339/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006340 * Append "n" to list "l".
6341 * Returns FAIL when out of memory.
6342 */
6343 static int
6344list_append_number(l, n)
6345 list_T *l;
6346 varnumber_T n;
6347{
6348 listitem_T *li;
6349
6350 li = listitem_alloc();
6351 if (li == NULL)
6352 return FAIL;
6353 li->li_tv.v_type = VAR_NUMBER;
6354 li->li_tv.v_lock = 0;
6355 li->li_tv.vval.v_number = n;
6356 list_append(l, li);
6357 return OK;
6358}
6359
6360/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006362 * If "item" is NULL append at the end.
6363 * Return FAIL when out of memory.
6364 */
6365 static int
6366list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 list_T *l;
6368 typval_T *tv;
6369 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006370{
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006372
6373 if (ni == NULL)
6374 return FAIL;
6375 copy_tv(tv, &ni->li_tv);
6376 if (item == NULL)
6377 /* Append new item at end of list. */
6378 list_append(l, ni);
6379 else
6380 {
6381 /* Insert new item before existing item. */
6382 ni->li_prev = item->li_prev;
6383 ni->li_next = item;
6384 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006385 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006386 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006387 ++l->lv_idx;
6388 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006389 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006390 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006391 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006392 l->lv_idx_item = NULL;
6393 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006394 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006395 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006396 }
6397 return OK;
6398}
6399
6400/*
6401 * Extend "l1" with "l2".
6402 * If "bef" is NULL append at the end, otherwise insert before this item.
6403 * Returns FAIL when out of memory.
6404 */
6405 static int
6406list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 list_T *l1;
6408 list_T *l2;
6409 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410{
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006412 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006414 /* We also quit the loop when we have inserted the original item count of
6415 * the list, avoid a hang when we extend a list with itself. */
6416 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6418 return FAIL;
6419 return OK;
6420}
6421
6422/*
6423 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6424 * Return FAIL when out of memory.
6425 */
6426 static int
6427list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 list_T *l1;
6429 list_T *l2;
6430 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431{
Bram Moolenaar33570922005-01-25 22:26:29 +00006432 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006434 if (l1 == NULL || l2 == NULL)
6435 return FAIL;
6436
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439 if (l == NULL)
6440 return FAIL;
6441 tv->v_type = VAR_LIST;
6442 tv->vval.v_list = l;
6443
6444 /* append all items from the second list */
6445 return list_extend(l, l2, NULL);
6446}
6447
6448/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006449 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452 * Returns NULL when out of memory.
6453 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006455list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006456 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006458 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459{
Bram Moolenaar33570922005-01-25 22:26:29 +00006460 list_T *copy;
6461 listitem_T *item;
6462 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463
6464 if (orig == NULL)
6465 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006466
6467 copy = list_alloc();
6468 if (copy != NULL)
6469 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006470 if (copyID != 0)
6471 {
6472 /* Do this before adding the items, because one of the items may
6473 * refer back to this list. */
6474 orig->lv_copyID = copyID;
6475 orig->lv_copylist = copy;
6476 }
6477 for (item = orig->lv_first; item != NULL && !got_int;
6478 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479 {
6480 ni = listitem_alloc();
6481 if (ni == NULL)
6482 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 {
6485 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6486 {
6487 vim_free(ni);
6488 break;
6489 }
6490 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006492 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 list_append(copy, ni);
6494 }
6495 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 if (item != NULL)
6497 {
6498 list_unref(copy);
6499 copy = NULL;
6500 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501 }
6502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 return copy;
6504}
6505
6506/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006508 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006511list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006512 list_T *l;
6513 listitem_T *item;
6514 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515{
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 /* notify watchers */
6519 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 list_fix_watch(l, ip);
6523 if (ip == item2)
6524 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526
6527 if (item2->li_next == NULL)
6528 l->lv_last = item->li_prev;
6529 else
6530 item2->li_next->li_prev = item->li_prev;
6531 if (item->li_prev == NULL)
6532 l->lv_first = item2->li_next;
6533 else
6534 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006535 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536}
6537
6538/*
6539 * Return an allocated string with the string representation of a list.
6540 * May return NULL.
6541 */
6542 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006543list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006545 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546{
6547 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548
6549 if (tv->vval.v_list == NULL)
6550 return NULL;
6551 ga_init2(&ga, (int)sizeof(char), 80);
6552 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006553 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006554 {
6555 vim_free(ga.ga_data);
6556 return NULL;
6557 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 ga_append(&ga, ']');
6559 ga_append(&ga, NUL);
6560 return (char_u *)ga.ga_data;
6561}
6562
6563/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006564 * Join list "l" into a string in "*gap", using separator "sep".
6565 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006566 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006567 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006569list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006570 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006572 char_u *sep;
6573 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006574 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006575{
6576 int first = TRUE;
6577 char_u *tofree;
6578 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006580 char_u *s;
6581
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006582 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006583 {
6584 if (first)
6585 first = FALSE;
6586 else
6587 ga_concat(gap, sep);
6588
6589 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006591 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006592 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006593 if (s != NULL)
6594 ga_concat(gap, s);
6595 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 if (s == NULL)
6597 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006598 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006599 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006601}
6602
6603/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006604 * Garbage collection for lists and dictionaries.
6605 *
6606 * We use reference counts to be able to free most items right away when they
6607 * are no longer used. But for composite items it's possible that it becomes
6608 * unused while the reference count is > 0: When there is a recursive
6609 * reference. Example:
6610 * :let l = [1, 2, 3]
6611 * :let d = {9: l}
6612 * :let l[1] = d
6613 *
6614 * Since this is quite unusual we handle this with garbage collection: every
6615 * once in a while find out which lists and dicts are not referenced from any
6616 * variable.
6617 *
6618 * Here is a good reference text about garbage collection (refers to Python
6619 * but it applies to all reference-counting mechanisms):
6620 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006621 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006622
6623/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006624 * Do garbage collection for lists and dicts.
6625 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006626 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006627 int
6628garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006629{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006630 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006631 buf_T *buf;
6632 win_T *wp;
6633 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006634 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006635 int did_free;
6636 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006637#ifdef FEAT_WINDOWS
6638 tabpage_T *tp;
6639#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006640
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006641 /* Only do this once. */
6642 want_garbage_collect = FALSE;
6643 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006644 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006645
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006646 /* We advance by two because we add one for items referenced through
6647 * previous_funccal. */
6648 current_copyID += COPYID_INC;
6649 copyID = current_copyID;
6650
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006651 /*
6652 * 1. Go through all accessible variables and mark all lists and dicts
6653 * with copyID.
6654 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006655
6656 /* Don't free variables in the previous_funccal list unless they are only
6657 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006658 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006659 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6660 {
6661 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6662 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6663 }
6664
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006665 /* script-local variables */
6666 for (i = 1; i <= ga_scripts.ga_len; ++i)
6667 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6668
6669 /* buffer-local variables */
6670 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6671 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6672
6673 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006674 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006675 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6676
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006677#ifdef FEAT_WINDOWS
6678 /* tabpage-local variables */
6679 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6680 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6681#endif
6682
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006683 /* global variables */
6684 set_ref_in_ht(&globvarht, copyID);
6685
6686 /* function-local variables */
6687 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6688 {
6689 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6690 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6691 }
6692
Bram Moolenaard812df62008-11-09 12:46:09 +00006693 /* v: vars */
6694 set_ref_in_ht(&vimvarht, copyID);
6695
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006696 /*
6697 * 2. Free lists and dictionaries that are not referenced.
6698 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006699 did_free = free_unref_items(copyID);
6700
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006701 /*
6702 * 3. Check if any funccal can be freed now.
6703 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006704 for (pfc = &previous_funccal; *pfc != NULL; )
6705 {
6706 if (can_free_funccal(*pfc, copyID))
6707 {
6708 fc = *pfc;
6709 *pfc = fc->caller;
6710 free_funccal(fc, TRUE);
6711 did_free = TRUE;
6712 did_free_funccal = TRUE;
6713 }
6714 else
6715 pfc = &(*pfc)->caller;
6716 }
6717 if (did_free_funccal)
6718 /* When a funccal was freed some more items might be garbage
6719 * collected, so run again. */
6720 (void)garbage_collect();
6721
6722 return did_free;
6723}
6724
6725/*
6726 * Free lists and dictionaries that are no longer referenced.
6727 */
6728 static int
6729free_unref_items(copyID)
6730 int copyID;
6731{
6732 dict_T *dd;
6733 list_T *ll;
6734 int did_free = FALSE;
6735
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006737 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006738 */
6739 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006740 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006741 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006742 /* Free the Dictionary and ordinary items it contains, but don't
6743 * recurse into Lists and Dictionaries, they will be in the list
6744 * of dicts or list of lists. */
6745 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 did_free = TRUE;
6747
6748 /* restart, next dict may also have been freed */
6749 dd = first_dict;
6750 }
6751 else
6752 dd = dd->dv_used_next;
6753
6754 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006755 * Go through the list of lists and free items without the copyID.
6756 * But don't free a list that has a watcher (used in a for loop), these
6757 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006758 */
6759 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006760 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6761 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006762 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006763 /* Free the List and ordinary items it contains, but don't recurse
6764 * into Lists and Dictionaries, they will be in the list of dicts
6765 * or list of lists. */
6766 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 did_free = TRUE;
6768
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006769 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 ll = first_list;
6771 }
6772 else
6773 ll = ll->lv_used_next;
6774
6775 return did_free;
6776}
6777
6778/*
6779 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6780 */
6781 static void
6782set_ref_in_ht(ht, copyID)
6783 hashtab_T *ht;
6784 int copyID;
6785{
6786 int todo;
6787 hashitem_T *hi;
6788
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006789 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006790 for (hi = ht->ht_array; todo > 0; ++hi)
6791 if (!HASHITEM_EMPTY(hi))
6792 {
6793 --todo;
6794 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6795 }
6796}
6797
6798/*
6799 * Mark all lists and dicts referenced through list "l" with "copyID".
6800 */
6801 static void
6802set_ref_in_list(l, copyID)
6803 list_T *l;
6804 int copyID;
6805{
6806 listitem_T *li;
6807
6808 for (li = l->lv_first; li != NULL; li = li->li_next)
6809 set_ref_in_item(&li->li_tv, copyID);
6810}
6811
6812/*
6813 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6814 */
6815 static void
6816set_ref_in_item(tv, copyID)
6817 typval_T *tv;
6818 int copyID;
6819{
6820 dict_T *dd;
6821 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006822
6823 switch (tv->v_type)
6824 {
6825 case VAR_DICT:
6826 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006827 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829 /* Didn't see this dict yet. */
6830 dd->dv_copyID = copyID;
6831 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834
6835 case VAR_LIST:
6836 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006837 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006838 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 /* Didn't see this list yet. */
6840 ll->lv_copyID = copyID;
6841 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846}
6847
6848/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006849 * Allocate an empty header for a dictionary.
6850 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006851 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006852dict_alloc()
6853{
Bram Moolenaar33570922005-01-25 22:26:29 +00006854 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006855
Bram Moolenaar33570922005-01-25 22:26:29 +00006856 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006858 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006859 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 if (first_dict != NULL)
6861 first_dict->dv_used_prev = d;
6862 d->dv_used_next = first_dict;
6863 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006864 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865
Bram Moolenaar33570922005-01-25 22:26:29 +00006866 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006867 d->dv_lock = 0;
6868 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006869 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006870 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006871 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006872}
6873
6874/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006875 * Allocate an empty dict for a return value.
6876 * Returns OK or FAIL.
6877 */
6878 static int
6879rettv_dict_alloc(rettv)
6880 typval_T *rettv;
6881{
6882 dict_T *d = dict_alloc();
6883
6884 if (d == NULL)
6885 return FAIL;
6886
6887 rettv->vval.v_dict = d;
6888 rettv->v_type = VAR_DICT;
6889 ++d->dv_refcount;
6890 return OK;
6891}
6892
6893
6894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006895 * Unreference a Dictionary: decrement the reference count and free it when it
6896 * becomes zero.
6897 */
6898 static void
6899dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006901{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006902 if (d != NULL && --d->dv_refcount <= 0)
6903 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006904}
6905
6906/*
6907 * Free a Dictionary, including all items it contains.
6908 * Ignores the reference count.
6909 */
6910 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006911dict_free(d, recurse)
6912 dict_T *d;
6913 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006914{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006915 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006917 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006918
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 /* Remove the dict from the list of dicts for garbage collection. */
6920 if (d->dv_used_prev == NULL)
6921 first_dict = d->dv_used_next;
6922 else
6923 d->dv_used_prev->dv_used_next = d->dv_used_next;
6924 if (d->dv_used_next != NULL)
6925 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6926
6927 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006928 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006929 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006930 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006932 if (!HASHITEM_EMPTY(hi))
6933 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006934 /* Remove the item before deleting it, just in case there is
6935 * something recursive causing trouble. */
6936 di = HI2DI(hi);
6937 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006938 if (recurse || (di->di_tv.v_type != VAR_LIST
6939 && di->di_tv.v_type != VAR_DICT))
6940 clear_tv(&di->di_tv);
6941 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006942 --todo;
6943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006946 vim_free(d);
6947}
6948
6949/*
6950 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006951 * The "key" is copied to the new item.
6952 * Note that the value of the item "di_tv" still needs to be initialized!
6953 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006954 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006955 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006956dictitem_alloc(key)
6957 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006958{
Bram Moolenaar33570922005-01-25 22:26:29 +00006959 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006960
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006961 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006962 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006963 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006964 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006965 di->di_flags = 0;
6966 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968}
6969
6970/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971 * Make a copy of a Dictionary item.
6972 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006974dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006976{
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006979 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6980 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981 if (di != NULL)
6982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006984 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006985 copy_tv(&org->di_tv, &di->di_tv);
6986 }
6987 return di;
6988}
6989
6990/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006991 * Remove item "item" from Dictionary "dict" and free it.
6992 */
6993 static void
6994dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 dict_T *dict;
6996 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006997{
Bram Moolenaar33570922005-01-25 22:26:29 +00006998 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006999
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007001 if (HASHITEM_EMPTY(hi))
7002 EMSG2(_(e_intern2), "dictitem_remove()");
7003 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007004 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007005 dictitem_free(item);
7006}
7007
7008/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009 * Free a dict item. Also clears the value.
7010 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007011 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007012dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015 clear_tv(&item->di_tv);
7016 vim_free(item);
7017}
7018
7019/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007020 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7021 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007022 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007023 * Returns NULL when out of memory.
7024 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007026dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007029 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007030{
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 dict_T *copy;
7032 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007035
7036 if (orig == NULL)
7037 return NULL;
7038
7039 copy = dict_alloc();
7040 if (copy != NULL)
7041 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007042 if (copyID != 0)
7043 {
7044 orig->dv_copyID = copyID;
7045 orig->dv_copydict = copy;
7046 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007047 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007048 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052 --todo;
7053
7054 di = dictitem_alloc(hi->hi_key);
7055 if (di == NULL)
7056 break;
7057 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007058 {
7059 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7060 copyID) == FAIL)
7061 {
7062 vim_free(di);
7063 break;
7064 }
7065 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007066 else
7067 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7068 if (dict_add(copy, di) == FAIL)
7069 {
7070 dictitem_free(di);
7071 break;
7072 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007073 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007074 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075
Bram Moolenaare9a41262005-01-15 22:18:47 +00007076 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007077 if (todo > 0)
7078 {
7079 dict_unref(copy);
7080 copy = NULL;
7081 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007082 }
7083
7084 return copy;
7085}
7086
7087/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007089 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007091 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 dict_T *d;
7094 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095{
Bram Moolenaar33570922005-01-25 22:26:29 +00007096 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097}
7098
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007100 * Add a number or string entry to dictionary "d".
7101 * When "str" is NULL use number "nr", otherwise use "str".
7102 * Returns FAIL when out of memory and when key already exists.
7103 */
7104 int
7105dict_add_nr_str(d, key, nr, str)
7106 dict_T *d;
7107 char *key;
7108 long nr;
7109 char_u *str;
7110{
7111 dictitem_T *item;
7112
7113 item = dictitem_alloc((char_u *)key);
7114 if (item == NULL)
7115 return FAIL;
7116 item->di_tv.v_lock = 0;
7117 if (str == NULL)
7118 {
7119 item->di_tv.v_type = VAR_NUMBER;
7120 item->di_tv.vval.v_number = nr;
7121 }
7122 else
7123 {
7124 item->di_tv.v_type = VAR_STRING;
7125 item->di_tv.vval.v_string = vim_strsave(str);
7126 }
7127 if (dict_add(d, item) == FAIL)
7128 {
7129 dictitem_free(item);
7130 return FAIL;
7131 }
7132 return OK;
7133}
7134
7135/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007136 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007137 * Returns FAIL when out of memory and when key already exists.
7138 */
7139 int
7140dict_add_list(d, key, list)
7141 dict_T *d;
7142 char *key;
7143 list_T *list;
7144{
7145 dictitem_T *item;
7146
7147 item = dictitem_alloc((char_u *)key);
7148 if (item == NULL)
7149 return FAIL;
7150 item->di_tv.v_lock = 0;
7151 item->di_tv.v_type = VAR_LIST;
7152 item->di_tv.vval.v_list = list;
7153 if (dict_add(d, item) == FAIL)
7154 {
7155 dictitem_free(item);
7156 return FAIL;
7157 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007158 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007159 return OK;
7160}
7161
7162/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007163 * Get the number of items in a Dictionary.
7164 */
7165 static long
7166dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007167 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007168{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 if (d == NULL)
7170 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007171 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007172}
7173
7174/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 * Find item "key[len]" in Dictionary "d".
7176 * If "len" is negative use strlen(key).
7177 * Returns NULL when not found.
7178 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007179 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 char_u *key;
7183 int len;
7184{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185#define AKEYLEN 200
7186 char_u buf[AKEYLEN];
7187 char_u *akey;
7188 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191 if (len < 0)
7192 akey = key;
7193 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007194 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 tofree = akey = vim_strnsave(key, len);
7196 if (akey == NULL)
7197 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 else
7200 {
7201 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007202 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 akey = buf;
7204 }
7205
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 vim_free(tofree);
7208 if (HASHITEM_EMPTY(hi))
7209 return NULL;
7210 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211}
7212
7213/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007214 * Get a string item from a dictionary.
7215 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007216 * Returns NULL if the entry doesn't exist or out of memory.
7217 */
7218 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007219get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007220 dict_T *d;
7221 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007222 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007223{
7224 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007225 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007226
7227 di = dict_find(d, key, -1);
7228 if (di == NULL)
7229 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007230 s = get_tv_string(&di->di_tv);
7231 if (save && s != NULL)
7232 s = vim_strsave(s);
7233 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007234}
7235
7236/*
7237 * Get a number item from a dictionary.
7238 * Returns 0 if the entry doesn't exist or out of memory.
7239 */
7240 long
7241get_dict_number(d, key)
7242 dict_T *d;
7243 char_u *key;
7244{
7245 dictitem_T *di;
7246
7247 di = dict_find(d, key, -1);
7248 if (di == NULL)
7249 return 0;
7250 return get_tv_number(&di->di_tv);
7251}
7252
7253/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 * Return an allocated string with the string representation of a Dictionary.
7255 * May return NULL.
7256 */
7257 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007258dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007260 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261{
7262 garray_T ga;
7263 int first = TRUE;
7264 char_u *tofree;
7265 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 return NULL;
7273 ga_init2(&ga, (int)sizeof(char), 80);
7274 ga_append(&ga, '{');
7275
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007276 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007277 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007281 --todo;
7282
7283 if (first)
7284 first = FALSE;
7285 else
7286 ga_concat(&ga, (char_u *)", ");
7287
7288 tofree = string_quote(hi->hi_key, FALSE);
7289 if (tofree != NULL)
7290 {
7291 ga_concat(&ga, tofree);
7292 vim_free(tofree);
7293 }
7294 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007295 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 if (s != NULL)
7297 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007299 if (s == NULL)
7300 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007303 if (todo > 0)
7304 {
7305 vim_free(ga.ga_data);
7306 return NULL;
7307 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308
7309 ga_append(&ga, '}');
7310 ga_append(&ga, NUL);
7311 return (char_u *)ga.ga_data;
7312}
7313
7314/*
7315 * Allocate a variable for a Dictionary and fill it from "*arg".
7316 * Return OK or FAIL. Returns NOTDONE for {expr}.
7317 */
7318 static int
7319get_dict_tv(arg, rettv, evaluate)
7320 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 int evaluate;
7323{
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 dict_T *d = NULL;
7325 typval_T tvkey;
7326 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007327 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331
7332 /*
7333 * First check if it's not a curly-braces thing: {expr}.
7334 * Must do this without evaluating, otherwise a function may be called
7335 * twice. Unfortunately this means we need to call eval1() twice for the
7336 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 if (*start != '}')
7340 {
7341 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7342 return FAIL;
7343 if (*start == '}')
7344 return NOTDONE;
7345 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346
7347 if (evaluate)
7348 {
7349 d = dict_alloc();
7350 if (d == NULL)
7351 return FAIL;
7352 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 tvkey.v_type = VAR_UNKNOWN;
7354 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355
7356 *arg = skipwhite(*arg + 1);
7357 while (**arg != '}' && **arg != NUL)
7358 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360 goto failret;
7361 if (**arg != ':')
7362 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007363 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365 goto failret;
7366 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007367 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007369 key = get_tv_string_buf_chk(&tvkey, buf);
7370 if (key == NULL || *key == NUL)
7371 {
7372 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7373 if (key != NULL)
7374 EMSG(_(e_emptykey));
7375 clear_tv(&tvkey);
7376 goto failret;
7377 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379
7380 *arg = skipwhite(*arg + 1);
7381 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7382 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007383 if (evaluate)
7384 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 goto failret;
7386 }
7387 if (evaluate)
7388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 if (item != NULL)
7391 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007392 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 clear_tv(&tv);
7395 goto failret;
7396 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 item = dictitem_alloc(key);
7398 clear_tv(&tvkey);
7399 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007402 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 if (dict_add(d, item) == FAIL)
7404 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 }
7406 }
7407
7408 if (**arg == '}')
7409 break;
7410 if (**arg != ',')
7411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007412 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 goto failret;
7414 }
7415 *arg = skipwhite(*arg + 1);
7416 }
7417
7418 if (**arg != '}')
7419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007420 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421failret:
7422 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007423 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 return FAIL;
7425 }
7426
7427 *arg = skipwhite(*arg + 1);
7428 if (evaluate)
7429 {
7430 rettv->v_type = VAR_DICT;
7431 rettv->vval.v_dict = d;
7432 ++d->dv_refcount;
7433 }
7434
7435 return OK;
7436}
7437
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007439 * Return a string with the string representation of a variable.
7440 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007441 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007442 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007443 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007444 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007445 */
7446 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007447echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007449 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007450 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007452{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 static int recurse = 0;
7454 char_u *r = NULL;
7455
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007458 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 *tofree = NULL;
7460 return NULL;
7461 }
7462 ++recurse;
7463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007464 switch (tv->v_type)
7465 {
7466 case VAR_FUNC:
7467 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 r = tv->vval.v_string;
7469 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007470
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007471 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007472 if (tv->vval.v_list == NULL)
7473 {
7474 *tofree = NULL;
7475 r = NULL;
7476 }
7477 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7478 {
7479 *tofree = NULL;
7480 r = (char_u *)"[...]";
7481 }
7482 else
7483 {
7484 tv->vval.v_list->lv_copyID = copyID;
7485 *tofree = list2string(tv, copyID);
7486 r = *tofree;
7487 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007489
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007491 if (tv->vval.v_dict == NULL)
7492 {
7493 *tofree = NULL;
7494 r = NULL;
7495 }
7496 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7497 {
7498 *tofree = NULL;
7499 r = (char_u *)"{...}";
7500 }
7501 else
7502 {
7503 tv->vval.v_dict->dv_copyID = copyID;
7504 *tofree = dict2string(tv, copyID);
7505 r = *tofree;
7506 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007508
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509 case VAR_STRING:
7510 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511 *tofree = NULL;
7512 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007513 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007514
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007515#ifdef FEAT_FLOAT
7516 case VAR_FLOAT:
7517 *tofree = NULL;
7518 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7519 r = numbuf;
7520 break;
7521#endif
7522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007523 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007524 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007525 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007526 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527
7528 --recurse;
7529 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007530}
7531
7532/*
7533 * Return a string with the string representation of a variable.
7534 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7535 * "numbuf" is used for a number.
7536 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007537 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538 */
7539 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007540tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007542 char_u **tofree;
7543 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007544 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007545{
7546 switch (tv->v_type)
7547 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007548 case VAR_FUNC:
7549 *tofree = string_quote(tv->vval.v_string, TRUE);
7550 return *tofree;
7551 case VAR_STRING:
7552 *tofree = string_quote(tv->vval.v_string, FALSE);
7553 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007554#ifdef FEAT_FLOAT
7555 case VAR_FLOAT:
7556 *tofree = NULL;
7557 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7558 return numbuf;
7559#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007560 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007561 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007563 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007564 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568}
7569
7570/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 * Return string "str" in ' quotes, doubling ' characters.
7572 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007574 */
7575 static char_u *
7576string_quote(str, function)
7577 char_u *str;
7578 int function;
7579{
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007581 char_u *p, *r, *s;
7582
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 len = (function ? 13 : 3);
7584 if (str != NULL)
7585 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007586 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 for (p = str; *p != NUL; mb_ptr_adv(p))
7588 if (*p == '\'')
7589 ++len;
7590 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007591 s = r = alloc(len);
7592 if (r != NULL)
7593 {
7594 if (function)
7595 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007597 r += 10;
7598 }
7599 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 if (str != NULL)
7602 for (p = str; *p != NUL; )
7603 {
7604 if (*p == '\'')
7605 *r++ = '\'';
7606 MB_COPY_CHAR(p, r);
7607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007609 if (function)
7610 *r++ = ')';
7611 *r++ = NUL;
7612 }
7613 return s;
7614}
7615
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007616#ifdef FEAT_FLOAT
7617/*
7618 * Convert the string "text" to a floating point number.
7619 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7620 * this always uses a decimal point.
7621 * Returns the length of the text that was consumed.
7622 */
7623 static int
7624string2float(text, value)
7625 char_u *text;
7626 float_T *value; /* result stored here */
7627{
7628 char *s = (char *)text;
7629 float_T f;
7630
7631 f = strtod(s, &s);
7632 *value = f;
7633 return (int)((char_u *)s - text);
7634}
7635#endif
7636
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 * Get the value of an environment variable.
7639 * "arg" is pointing to the '$'. It is advanced to after the name.
7640 * If the environment variable was not set, silently assume it is empty.
7641 * Always return OK.
7642 */
7643 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 int evaluate;
7648{
7649 char_u *string = NULL;
7650 int len;
7651 int cc;
7652 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007653 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
7655 ++*arg;
7656 name = *arg;
7657 len = get_env_len(arg);
7658 if (evaluate)
7659 {
7660 if (len != 0)
7661 {
7662 cc = name[len];
7663 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007664 /* first try vim_getenv(), fast for normal environment vars */
7665 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007667 {
7668 if (!mustfree)
7669 string = vim_strsave(string);
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 else
7672 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007673 if (mustfree)
7674 vim_free(string);
7675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 /* next try expanding things like $VIM and ${HOME} */
7677 string = expand_env_save(name - 1);
7678 if (string != NULL && *string == '$')
7679 {
7680 vim_free(string);
7681 string = NULL;
7682 }
7683 }
7684 name[len] = cc;
7685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 rettv->v_type = VAR_STRING;
7687 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689
7690 return OK;
7691}
7692
7693/*
7694 * Array with names and number of arguments of all internal functions
7695 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7696 */
7697static struct fst
7698{
7699 char *f_name; /* function name */
7700 char f_min_argc; /* minimal number of arguments */
7701 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007703 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704} functions[] =
7705{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007706#ifdef FEAT_FLOAT
7707 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007708 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007709#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007710 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {"append", 2, 2, f_append},
7712 {"argc", 0, 0, f_argc},
7713 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007714 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007715#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007716 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007717 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007718 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007721 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {"bufexists", 1, 1, f_bufexists},
7723 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7724 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7725 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7726 {"buflisted", 1, 1, f_buflisted},
7727 {"bufloaded", 1, 1, f_bufloaded},
7728 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007729 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {"bufwinnr", 1, 1, f_bufwinnr},
7731 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007732 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007733 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734#ifdef FEAT_FLOAT
7735 {"ceil", 1, 1, f_ceil},
7736#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007737 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {"char2nr", 1, 1, f_char2nr},
7739 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007740 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007742#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007743 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007744 {"complete_add", 1, 1, f_complete_add},
7745 {"complete_check", 0, 0, f_complete_check},
7746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007749#ifdef FEAT_FLOAT
7750 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007751 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007752#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007755 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007756 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {"delete", 1, 1, f_delete},
7758 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007759 {"diff_filler", 1, 1, f_diff_filler},
7760 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007761 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007763 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {"eventhandler", 0, 0, f_eventhandler},
7765 {"executable", 1, 1, f_executable},
7766 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007767#ifdef FEAT_FLOAT
7768 {"exp", 1, 1, f_exp},
7769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007772 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7774 {"filereadable", 1, 1, f_filereadable},
7775 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007776 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007777 {"finddir", 1, 3, f_finddir},
7778 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007779#ifdef FEAT_FLOAT
7780 {"float2nr", 1, 1, f_float2nr},
7781 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007782 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007783#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007784 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 {"fnamemodify", 2, 2, f_fnamemodify},
7786 {"foldclosed", 1, 1, f_foldclosed},
7787 {"foldclosedend", 1, 1, f_foldclosedend},
7788 {"foldlevel", 1, 1, f_foldlevel},
7789 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007790 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007792 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007793 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007794 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007795 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {"getbufvar", 2, 2, f_getbufvar},
7797 {"getchar", 0, 1, f_getchar},
7798 {"getcharmod", 0, 0, f_getcharmod},
7799 {"getcmdline", 0, 0, f_getcmdline},
7800 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007801 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007803 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007804 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {"getfsize", 1, 1, f_getfsize},
7806 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007807 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007808 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007809 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007810 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007811 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007812 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007813 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007814 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007816 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007817 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {"getwinposx", 0, 0, f_getwinposx},
7819 {"getwinposy", 0, 0, f_getwinposy},
7820 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007821 {"glob", 1, 2, f_glob},
7822 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007825 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007826 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7828 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7829 {"histadd", 2, 2, f_histadd},
7830 {"histdel", 1, 2, f_histdel},
7831 {"histget", 1, 2, f_histget},
7832 {"histnr", 1, 1, f_histnr},
7833 {"hlID", 1, 1, f_hlID},
7834 {"hlexists", 1, 1, f_hlexists},
7835 {"hostname", 0, 0, f_hostname},
7836 {"iconv", 3, 3, f_iconv},
7837 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007839 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007841 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"inputrestore", 0, 0, f_inputrestore},
7843 {"inputsave", 0, 0, f_inputsave},
7844 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007845 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007847 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007849 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007850 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007852 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"libcall", 3, 3, f_libcall},
7854 {"libcallnr", 3, 3, f_libcallnr},
7855 {"line", 1, 1, f_line},
7856 {"line2byte", 1, 1, f_line2byte},
7857 {"lispindent", 1, 1, f_lispindent},
7858 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007860 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007861 {"log10", 1, 1, f_log10},
7862#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007863 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007864 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007865 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007866 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007867 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007868 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007869 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007870 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007871 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007872 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007873 {"max", 1, 1, f_max},
7874 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007875#ifdef vim_mkdir
7876 {"mkdir", 1, 3, f_mkdir},
7877#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007878 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007879#ifdef FEAT_MZSCHEME
7880 {"mzeval", 1, 1, f_mzeval},
7881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {"nextnonblank", 1, 1, f_nextnonblank},
7883 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007884 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885#ifdef FEAT_FLOAT
7886 {"pow", 2, 2, f_pow},
7887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007889 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007890 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007891 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007892 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007893 {"reltime", 0, 2, f_reltime},
7894 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"remote_expr", 2, 3, f_remote_expr},
7896 {"remote_foreground", 1, 1, f_remote_foreground},
7897 {"remote_peek", 1, 2, f_remote_peek},
7898 {"remote_read", 1, 1, f_remote_read},
7899 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007900 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007902 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007904 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007905#ifdef FEAT_FLOAT
7906 {"round", 1, 1, f_round},
7907#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007908 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007909 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007910 {"searchpair", 3, 7, f_searchpair},
7911 {"searchpairpos", 3, 7, f_searchpairpos},
7912 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"server2client", 2, 2, f_server2client},
7914 {"serverlist", 0, 0, f_serverlist},
7915 {"setbufvar", 3, 3, f_setbufvar},
7916 {"setcmdpos", 1, 1, f_setcmdpos},
7917 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007918 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007919 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007920 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007921 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007923 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007924 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007926 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007928#ifdef FEAT_FLOAT
7929 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007930 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007931#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007932 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007933 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007934 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007935 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007936 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007937#ifdef FEAT_FLOAT
7938 {"sqrt", 1, 1, f_sqrt},
7939 {"str2float", 1, 1, f_str2float},
7940#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007941 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007942 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007943 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944#ifdef HAVE_STRFTIME
7945 {"strftime", 1, 2, f_strftime},
7946#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007948 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"strlen", 1, 1, f_strlen},
7950 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007951 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007953 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"submatch", 1, 1, f_submatch},
7955 {"substitute", 4, 4, f_substitute},
7956 {"synID", 3, 3, f_synID},
7957 {"synIDattr", 2, 3, f_synIDattr},
7958 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007959 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007960 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007961 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007962 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007963 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007964 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007965 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007966 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007967#ifdef FEAT_FLOAT
7968 {"tan", 1, 1, f_tan},
7969 {"tanh", 1, 1, f_tanh},
7970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007972 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"tolower", 1, 1, f_tolower},
7974 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007975 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007976#ifdef FEAT_FLOAT
7977 {"trunc", 1, 1, f_trunc},
7978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007980 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007981 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"virtcol", 1, 1, f_virtcol},
7984 {"visualmode", 0, 1, f_visualmode},
7985 {"winbufnr", 1, 1, f_winbufnr},
7986 {"wincol", 0, 0, f_wincol},
7987 {"winheight", 1, 1, f_winheight},
7988 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007989 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007991 {"winrestview", 1, 1, f_winrestview},
7992 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007994 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995};
7996
7997#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7998
7999/*
8000 * Function given to ExpandGeneric() to obtain the list of internal
8001 * or user defined function names.
8002 */
8003 char_u *
8004get_function_name(xp, idx)
8005 expand_T *xp;
8006 int idx;
8007{
8008 static int intidx = -1;
8009 char_u *name;
8010
8011 if (idx == 0)
8012 intidx = -1;
8013 if (intidx < 0)
8014 {
8015 name = get_user_func_name(xp, idx);
8016 if (name != NULL)
8017 return name;
8018 }
8019 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8020 {
8021 STRCPY(IObuff, functions[intidx].f_name);
8022 STRCAT(IObuff, "(");
8023 if (functions[intidx].f_max_argc == 0)
8024 STRCAT(IObuff, ")");
8025 return IObuff;
8026 }
8027
8028 return NULL;
8029}
8030
8031/*
8032 * Function given to ExpandGeneric() to obtain the list of internal or
8033 * user defined variable or function names.
8034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 char_u *
8036get_expr_name(xp, idx)
8037 expand_T *xp;
8038 int idx;
8039{
8040 static int intidx = -1;
8041 char_u *name;
8042
8043 if (idx == 0)
8044 intidx = -1;
8045 if (intidx < 0)
8046 {
8047 name = get_function_name(xp, idx);
8048 if (name != NULL)
8049 return name;
8050 }
8051 return get_user_var_name(xp, ++intidx);
8052}
8053
8054#endif /* FEAT_CMDL_COMPL */
8055
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008056#if defined(EBCDIC) || defined(PROTO)
8057/*
8058 * Compare struct fst by function name.
8059 */
8060 static int
8061compare_func_name(s1, s2)
8062 const void *s1;
8063 const void *s2;
8064{
8065 struct fst *p1 = (struct fst *)s1;
8066 struct fst *p2 = (struct fst *)s2;
8067
8068 return STRCMP(p1->f_name, p2->f_name);
8069}
8070
8071/*
8072 * Sort the function table by function name.
8073 * The sorting of the table above is ASCII dependant.
8074 * On machines using EBCDIC we have to sort it.
8075 */
8076 static void
8077sortFunctions()
8078{
8079 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8080
8081 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8082}
8083#endif
8084
8085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086/*
8087 * Find internal function in table above.
8088 * Return index, or -1 if not found
8089 */
8090 static int
8091find_internal_func(name)
8092 char_u *name; /* name of the function */
8093{
8094 int first = 0;
8095 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8096 int cmp;
8097 int x;
8098
8099 /*
8100 * Find the function name in the table. Binary search.
8101 */
8102 while (first <= last)
8103 {
8104 x = first + ((unsigned)(last - first) >> 1);
8105 cmp = STRCMP(name, functions[x].f_name);
8106 if (cmp < 0)
8107 last = x - 1;
8108 else if (cmp > 0)
8109 first = x + 1;
8110 else
8111 return x;
8112 }
8113 return -1;
8114}
8115
8116/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008117 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8118 * name it contains, otherwise return "name".
8119 */
8120 static char_u *
8121deref_func_name(name, lenp)
8122 char_u *name;
8123 int *lenp;
8124{
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 int cc;
8127
8128 cc = name[*lenp];
8129 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008130 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008131 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008133 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008134 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 {
8136 *lenp = 0;
8137 return (char_u *)""; /* just in case */
8138 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008139 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141 }
8142
8143 return name;
8144}
8145
8146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 * Allocate a variable for the result of a function.
8148 * Return OK or FAIL.
8149 */
8150 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008151get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8152 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 char_u *name; /* name of the function */
8154 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 char_u **arg; /* argument, pointing to the '(' */
8157 linenr_T firstline; /* first line of range */
8158 linenr_T lastline; /* last line of range */
8159 int *doesrange; /* return: function handled range */
8160 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 char_u *argp;
8164 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008165 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 int argcount = 0; /* number of arguments found */
8167
8168 /*
8169 * Get the arguments.
8170 */
8171 argp = *arg;
8172 while (argcount < MAX_FUNC_ARGS)
8173 {
8174 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8175 if (*argp == ')' || *argp == ',' || *argp == NUL)
8176 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8178 {
8179 ret = FAIL;
8180 break;
8181 }
8182 ++argcount;
8183 if (*argp != ',')
8184 break;
8185 }
8186 if (*argp == ')')
8187 ++argp;
8188 else
8189 ret = FAIL;
8190
8191 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 {
8196 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008197 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008199 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
8202 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
8205 *arg = skipwhite(argp);
8206 return ret;
8207}
8208
8209
8210/*
8211 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008212 * Return OK when the function can't be called, FAIL otherwise.
8213 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 */
8215 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008216call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008217 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008218 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008220 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008222 typval_T *argvars; /* vars for arguments, must have "argcount"
8223 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 linenr_T firstline; /* first line of range */
8225 linenr_T lastline; /* last line of range */
8226 int *doesrange; /* return: function handled range */
8227 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231#define ERROR_UNKNOWN 0
8232#define ERROR_TOOMANY 1
8233#define ERROR_TOOFEW 2
8234#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008235#define ERROR_DICT 4
8236#define ERROR_NONE 5
8237#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 int error = ERROR_NONE;
8239 int i;
8240 int llen;
8241 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242#define FLEN_FIXED 40
8243 char_u fname_buf[FLEN_FIXED + 1];
8244 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008245 char_u *name;
8246
8247 /* Make a copy of the name, if it comes from a funcref variable it could
8248 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008249 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008250 if (name == NULL)
8251 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
8253 /*
8254 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8255 * Change <SNR>123_name() to K_SNR 123_name().
8256 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 llen = eval_fname_script(name);
8259 if (llen > 0)
8260 {
8261 fname_buf[0] = K_SPECIAL;
8262 fname_buf[1] = KS_EXTRA;
8263 fname_buf[2] = (int)KE_SNR;
8264 i = 3;
8265 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8266 {
8267 if (current_SID <= 0)
8268 error = ERROR_SCRIPT;
8269 else
8270 {
8271 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8272 i = (int)STRLEN(fname_buf);
8273 }
8274 }
8275 if (i + STRLEN(name + llen) < FLEN_FIXED)
8276 {
8277 STRCPY(fname_buf + i, name + llen);
8278 fname = fname_buf;
8279 }
8280 else
8281 {
8282 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8283 if (fname == NULL)
8284 error = ERROR_OTHER;
8285 else
8286 {
8287 mch_memmove(fname, fname_buf, (size_t)i);
8288 STRCPY(fname + i, name + llen);
8289 }
8290 }
8291 }
8292 else
8293 fname = name;
8294
8295 *doesrange = FALSE;
8296
8297
8298 /* execute the function if no errors detected and executing */
8299 if (evaluate && error == ERROR_NONE)
8300 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008301 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8302 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 error = ERROR_UNKNOWN;
8304
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008305 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
8307 /*
8308 * User defined function.
8309 */
8310 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008311
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008313 /* Trigger FuncUndefined event, may load the function. */
8314 if (fp == NULL
8315 && apply_autocmds(EVENT_FUNCUNDEFINED,
8316 fname, fname, TRUE, NULL)
8317 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008319 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 fp = find_func(fname);
8321 }
8322#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008323 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008324 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008325 {
8326 /* loaded a package, search for the function again */
8327 fp = find_func(fname);
8328 }
8329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (fp != NULL)
8331 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008332 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008334 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008336 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008338 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008339 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 else
8341 {
8342 /*
8343 * Call the user function.
8344 * Save and restore search patterns, script variables and
8345 * redo buffer.
8346 */
8347 save_search_patterns();
8348 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008349 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008350 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008351 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008352 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8353 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8354 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008355 /* Function was unreferenced while being used, free it
8356 * now. */
8357 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 restoreRedobuff();
8359 restore_search_patterns();
8360 error = ERROR_NONE;
8361 }
8362 }
8363 }
8364 else
8365 {
8366 /*
8367 * Find the function name in the table, call its implementation.
8368 */
8369 i = find_internal_func(fname);
8370 if (i >= 0)
8371 {
8372 if (argcount < functions[i].f_min_argc)
8373 error = ERROR_TOOFEW;
8374 else if (argcount > functions[i].f_max_argc)
8375 error = ERROR_TOOMANY;
8376 else
8377 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008378 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 error = ERROR_NONE;
8381 }
8382 }
8383 }
8384 /*
8385 * The function call (or "FuncUndefined" autocommand sequence) might
8386 * have been aborted by an error, an interrupt, or an explicitly thrown
8387 * exception that has not been caught so far. This situation can be
8388 * tested for by calling aborting(). For an error in an internal
8389 * function or for the "E132" error in call_user_func(), however, the
8390 * throw point at which the "force_abort" flag (temporarily reset by
8391 * emsg()) is normally updated has not been reached yet. We need to
8392 * update that flag first to make aborting() reliable.
8393 */
8394 update_force_abort();
8395 }
8396 if (error == ERROR_NONE)
8397 ret = OK;
8398
8399 /*
8400 * Report an error unless the argument evaluation or function call has been
8401 * cancelled due to an aborting error, an interrupt, or an exception.
8402 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008403 if (!aborting())
8404 {
8405 switch (error)
8406 {
8407 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008408 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008409 break;
8410 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008411 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008412 break;
8413 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008414 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008415 name);
8416 break;
8417 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008418 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008419 name);
8420 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008421 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008422 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008423 name);
8424 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008425 }
8426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 if (fname != name && fname != fname_buf)
8429 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008430 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431
8432 return ret;
8433}
8434
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008435/*
8436 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008437 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008438 */
8439 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008440emsg_funcname(ermsg, name)
8441 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008442 char_u *name;
8443{
8444 char_u *p;
8445
8446 if (*name == K_SPECIAL)
8447 p = concat_str((char_u *)"<SNR>", name + 3);
8448 else
8449 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008450 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008451 if (p != name)
8452 vim_free(p);
8453}
8454
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008455/*
8456 * Return TRUE for a non-zero Number and a non-empty String.
8457 */
8458 static int
8459non_zero_arg(argvars)
8460 typval_T *argvars;
8461{
8462 return ((argvars[0].v_type == VAR_NUMBER
8463 && argvars[0].vval.v_number != 0)
8464 || (argvars[0].v_type == VAR_STRING
8465 && argvars[0].vval.v_string != NULL
8466 && *argvars[0].vval.v_string != NUL));
8467}
8468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469/*********************************************
8470 * Implementation of the built-in functions
8471 */
8472
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008473#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008474static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8475
8476/*
8477 * Get the float value of "argvars[0]" into "f".
8478 * Returns FAIL when the argument is not a Number or Float.
8479 */
8480 static int
8481get_float_arg(argvars, f)
8482 typval_T *argvars;
8483 float_T *f;
8484{
8485 if (argvars[0].v_type == VAR_FLOAT)
8486 {
8487 *f = argvars[0].vval.v_float;
8488 return OK;
8489 }
8490 if (argvars[0].v_type == VAR_NUMBER)
8491 {
8492 *f = (float_T)argvars[0].vval.v_number;
8493 return OK;
8494 }
8495 EMSG(_("E808: Number or Float required"));
8496 return FAIL;
8497}
8498
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008499/*
8500 * "abs(expr)" function
8501 */
8502 static void
8503f_abs(argvars, rettv)
8504 typval_T *argvars;
8505 typval_T *rettv;
8506{
8507 if (argvars[0].v_type == VAR_FLOAT)
8508 {
8509 rettv->v_type = VAR_FLOAT;
8510 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8511 }
8512 else
8513 {
8514 varnumber_T n;
8515 int error = FALSE;
8516
8517 n = get_tv_number_chk(&argvars[0], &error);
8518 if (error)
8519 rettv->vval.v_number = -1;
8520 else if (n > 0)
8521 rettv->vval.v_number = n;
8522 else
8523 rettv->vval.v_number = -n;
8524 }
8525}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008526
8527/*
8528 * "acos()" function
8529 */
8530 static void
8531f_acos(argvars, rettv)
8532 typval_T *argvars;
8533 typval_T *rettv;
8534{
8535 float_T f;
8536
8537 rettv->v_type = VAR_FLOAT;
8538 if (get_float_arg(argvars, &f) == OK)
8539 rettv->vval.v_float = acos(f);
8540 else
8541 rettv->vval.v_float = 0.0;
8542}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008543#endif
8544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008546 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 */
8548 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008549f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 typval_T *argvars;
8551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008556 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008558 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008559 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008560 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008562 }
8563 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008564 EMSG(_(e_listreq));
8565}
8566
8567/*
8568 * "append(lnum, string/list)" function
8569 */
8570 static void
8571f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *argvars;
8573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008574{
8575 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008576 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 list_T *l = NULL;
8578 listitem_T *li = NULL;
8579 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008580 long added = 0;
8581
Bram Moolenaar0d660222005-01-07 21:51:51 +00008582 lnum = get_tv_lnum(argvars);
8583 if (lnum >= 0
8584 && lnum <= curbuf->b_ml.ml_line_count
8585 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008586 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008587 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008588 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008589 l = argvars[1].vval.v_list;
8590 if (l == NULL)
8591 return;
8592 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008594 for (;;)
8595 {
8596 if (l == NULL)
8597 tv = &argvars[1]; /* append a string */
8598 else if (li == NULL)
8599 break; /* end of list */
8600 else
8601 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008602 line = get_tv_string_chk(tv);
8603 if (line == NULL) /* type error */
8604 {
8605 rettv->vval.v_number = 1; /* Failed */
8606 break;
8607 }
8608 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008609 ++added;
8610 if (l == NULL)
8611 break;
8612 li = li->li_next;
8613 }
8614
8615 appended_lines_mark(lnum, added);
8616 if (curwin->w_cursor.lnum > lnum)
8617 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008619 else
8620 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621}
8622
8623/*
8624 * "argc()" function
8625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008628 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632}
8633
8634/*
8635 * "argidx()" function
8636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643}
8644
8645/*
8646 * "argv(nr)" function
8647 */
8648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008650 typval_T *argvars;
8651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653 int idx;
8654
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008655 if (argvars[0].v_type != VAR_UNKNOWN)
8656 {
8657 idx = get_tv_number_chk(&argvars[0], NULL);
8658 if (idx >= 0 && idx < ARGCOUNT)
8659 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8660 else
8661 rettv->vval.v_string = NULL;
8662 rettv->v_type = VAR_STRING;
8663 }
8664 else if (rettv_list_alloc(rettv) == OK)
8665 for (idx = 0; idx < ARGCOUNT; ++idx)
8666 list_append_string(rettv->vval.v_list,
8667 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668}
8669
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008670#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008671/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008672 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008673 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008674 static void
8675f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008676 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008677 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008678{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008679 float_T f;
8680
8681 rettv->v_type = VAR_FLOAT;
8682 if (get_float_arg(argvars, &f) == OK)
8683 rettv->vval.v_float = asin(f);
8684 else
8685 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686}
8687
8688/*
8689 * "atan()" function
8690 */
8691 static void
8692f_atan(argvars, rettv)
8693 typval_T *argvars;
8694 typval_T *rettv;
8695{
8696 float_T f;
8697
8698 rettv->v_type = VAR_FLOAT;
8699 if (get_float_arg(argvars, &f) == OK)
8700 rettv->vval.v_float = atan(f);
8701 else
8702 rettv->vval.v_float = 0.0;
8703}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008704
8705/*
8706 * "atan2()" function
8707 */
8708 static void
8709f_atan2(argvars, rettv)
8710 typval_T *argvars;
8711 typval_T *rettv;
8712{
8713 float_T fx, fy;
8714
8715 rettv->v_type = VAR_FLOAT;
8716 if (get_float_arg(argvars, &fx) == OK
8717 && get_float_arg(&argvars[1], &fy) == OK)
8718 rettv->vval.v_float = atan2(fx, fy);
8719 else
8720 rettv->vval.v_float = 0.0;
8721}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008722#endif
8723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724/*
8725 * "browse(save, title, initdir, default)" function
8726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
8732#ifdef FEAT_BROWSE
8733 int save;
8734 char_u *title;
8735 char_u *initdir;
8736 char_u *defname;
8737 char_u buf[NUMBUFLEN];
8738 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008741 save = get_tv_number_chk(&argvars[0], &error);
8742 title = get_tv_string_chk(&argvars[1]);
8743 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8744 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008746 if (error || title == NULL || initdir == NULL || defname == NULL)
8747 rettv->vval.v_string = NULL;
8748 else
8749 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008750 do_browse(save ? BROWSE_SAVE : 0,
8751 title, defname, NULL, initdir, NULL, curbuf);
8752#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008754#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008756}
8757
8758/*
8759 * "browsedir(title, initdir)" function
8760 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008764 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008765{
8766#ifdef FEAT_BROWSE
8767 char_u *title;
8768 char_u *initdir;
8769 char_u buf[NUMBUFLEN];
8770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 title = get_tv_string_chk(&argvars[0]);
8772 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 if (title == NULL || initdir == NULL)
8775 rettv->vval.v_string = NULL;
8776 else
8777 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008778 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783}
8784
Bram Moolenaar33570922005-01-25 22:26:29 +00008785static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008786
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787/*
8788 * Find a buffer by number or exact name.
8789 */
8790 static buf_T *
8791find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
8794 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008796 if (avar->v_type == VAR_NUMBER)
8797 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008798 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008800 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008801 if (buf == NULL)
8802 {
8803 /* No full path name match, try a match with a URL or a "nofile"
8804 * buffer, these don't use the full path. */
8805 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8806 if (buf->b_fname != NULL
8807 && (path_with_url(buf->b_fname)
8808#ifdef FEAT_QUICKFIX
8809 || bt_nofile(buf)
8810#endif
8811 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008812 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008813 break;
8814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 }
8816 return buf;
8817}
8818
8819/*
8820 * "bufexists(expr)" function
8821 */
8822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *argvars;
8825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828}
8829
8830/*
8831 * "buflisted(expr)" function
8832 */
8833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 typval_T *argvars;
8836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
8838 buf_T *buf;
8839
8840 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842}
8843
8844/*
8845 * "bufloaded(expr)" function
8846 */
8847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008849 typval_T *argvars;
8850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
8852 buf_T *buf;
8853
8854 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856}
8857
Bram Moolenaar33570922005-01-25 22:26:29 +00008858static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860/*
8861 * Get buffer by number or pattern.
8862 */
8863 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008865 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 int save_magic;
8869 char_u *save_cpo;
8870 buf_T *buf;
8871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 if (tv->v_type == VAR_NUMBER)
8873 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008874 if (tv->v_type != VAR_STRING)
8875 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 if (name == NULL || *name == NUL)
8877 return curbuf;
8878 if (name[0] == '$' && name[1] == NUL)
8879 return lastbuf;
8880
8881 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8882 save_magic = p_magic;
8883 p_magic = TRUE;
8884 save_cpo = p_cpo;
8885 p_cpo = (char_u *)"";
8886
8887 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8888 TRUE, FALSE));
8889
8890 p_magic = save_magic;
8891 p_cpo = save_cpo;
8892
8893 /* If not found, try expanding the name, like done for bufexists(). */
8894 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
8897 return buf;
8898}
8899
8900/*
8901 * "bufname(expr)" function
8902 */
8903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008905 typval_T *argvars;
8906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907{
8908 buf_T *buf;
8909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912 buf = get_buf_tv(&argvars[0]);
8913 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 --emsg_off;
8919}
8920
8921/*
8922 * "bufnr(expr)" function
8923 */
8924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008926 typval_T *argvars;
8927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008930 int error = FALSE;
8931 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008936 --emsg_off;
8937
8938 /* If the buffer isn't found and the second argument is not zero create a
8939 * new buffer. */
8940 if (buf == NULL
8941 && argvars[1].v_type != VAR_UNKNOWN
8942 && get_tv_number_chk(&argvars[1], &error) != 0
8943 && !error
8944 && (name = get_tv_string_chk(&argvars[0])) != NULL
8945 && !error)
8946 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8947
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952}
8953
8954/*
8955 * "bufwinnr(nr)" function
8956 */
8957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 typval_T *argvars;
8960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962#ifdef FEAT_WINDOWS
8963 win_T *wp;
8964 int winnr = 0;
8965#endif
8966 buf_T *buf;
8967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971#ifdef FEAT_WINDOWS
8972 for (wp = firstwin; wp; wp = wp->w_next)
8973 {
8974 ++winnr;
8975 if (wp->w_buffer == buf)
8976 break;
8977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981#endif
8982 --emsg_off;
8983}
8984
8985/*
8986 * "byte2line(byte)" function
8987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008990 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992{
8993#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#else
8996 long boff = 0;
8997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 (linenr_T)0, &boff);
9004#endif
9005}
9006
9007/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009008 * "byteidx()" function
9009 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 typval_T *argvars;
9013 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009014{
9015#ifdef FEAT_MBYTE
9016 char_u *t;
9017#endif
9018 char_u *str;
9019 long idx;
9020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 str = get_tv_string_chk(&argvars[0]);
9022 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009025 return;
9026
9027#ifdef FEAT_MBYTE
9028 t = str;
9029 for ( ; idx > 0; idx--)
9030 {
9031 if (*t == NUL) /* EOL reached */
9032 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009033 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009034 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009035 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009036#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009037 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009038 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009039#endif
9040}
9041
9042/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009043 * "call(func, arglist)" function
9044 */
9045 static void
9046f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009047 typval_T *argvars;
9048 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009049{
9050 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009051 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009052 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009053 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009054 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009055 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009057 if (argvars[1].v_type != VAR_LIST)
9058 {
9059 EMSG(_(e_listreq));
9060 return;
9061 }
9062 if (argvars[1].vval.v_list == NULL)
9063 return;
9064
9065 if (argvars[0].v_type == VAR_FUNC)
9066 func = argvars[0].vval.v_string;
9067 else
9068 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 if (*func == NUL)
9070 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009071
Bram Moolenaare9a41262005-01-15 22:18:47 +00009072 if (argvars[2].v_type != VAR_UNKNOWN)
9073 {
9074 if (argvars[2].v_type != VAR_DICT)
9075 {
9076 EMSG(_(e_dictreq));
9077 return;
9078 }
9079 selfdict = argvars[2].vval.v_dict;
9080 }
9081
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009082 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9083 item = item->li_next)
9084 {
9085 if (argc == MAX_FUNC_ARGS)
9086 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009087 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009088 break;
9089 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009090 /* Make a copy of each argument. This is needed to be able to set
9091 * v_lock to VAR_FIXED in the copy without changing the original list.
9092 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009093 copy_tv(&item->li_tv, &argv[argc++]);
9094 }
9095
9096 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009097 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009098 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9099 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009100
9101 /* Free the arguments. */
9102 while (argc > 0)
9103 clear_tv(&argv[--argc]);
9104}
9105
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009106#ifdef FEAT_FLOAT
9107/*
9108 * "ceil({float})" function
9109 */
9110 static void
9111f_ceil(argvars, rettv)
9112 typval_T *argvars;
9113 typval_T *rettv;
9114{
9115 float_T f;
9116
9117 rettv->v_type = VAR_FLOAT;
9118 if (get_float_arg(argvars, &f) == OK)
9119 rettv->vval.v_float = ceil(f);
9120 else
9121 rettv->vval.v_float = 0.0;
9122}
9123#endif
9124
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009125/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009126 * "changenr()" function
9127 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009128 static void
9129f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009130 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009131 typval_T *rettv;
9132{
9133 rettv->vval.v_number = curbuf->b_u_seq_cur;
9134}
9135
9136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 * "char2nr(string)" function
9138 */
9139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009141 typval_T *argvars;
9142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144#ifdef FEAT_MBYTE
9145 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 else
9148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150}
9151
9152/*
9153 * "cindent(lnum)" function
9154 */
9155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009157 typval_T *argvars;
9158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160#ifdef FEAT_CINDENT
9161 pos_T pos;
9162 linenr_T lnum;
9163
9164 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9167 {
9168 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 curwin->w_cursor = pos;
9171 }
9172 else
9173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175}
9176
9177/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009178 * "clearmatches()" function
9179 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009180 static void
9181f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009182 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009183 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009184{
9185#ifdef FEAT_SEARCH_EXTRA
9186 clear_matches(curwin);
9187#endif
9188}
9189
9190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 * "col(string)" function
9192 */
9193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009195 typval_T *argvars;
9196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
9198 colnr_T col = 0;
9199 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009200 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009202 fp = var2fpos(&argvars[0], FALSE, &fnum);
9203 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 {
9205 if (fp->col == MAXCOL)
9206 {
9207 /* '> can be MAXCOL, get the length of the line then */
9208 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009209 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 else
9211 col = MAXCOL;
9212 }
9213 else
9214 {
9215 col = fp->col + 1;
9216#ifdef FEAT_VIRTUALEDIT
9217 /* col(".") when the cursor is on the NUL at the end of the line
9218 * because of "coladd" can be seen as an extra column. */
9219 if (virtual_active() && fp == &curwin->w_cursor)
9220 {
9221 char_u *p = ml_get_cursor();
9222
9223 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9224 curwin->w_virtcol - curwin->w_cursor.coladd))
9225 {
9226# ifdef FEAT_MBYTE
9227 int l;
9228
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009229 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 col += l;
9231# else
9232 if (*p != NUL && p[1] == NUL)
9233 ++col;
9234# endif
9235 }
9236 }
9237#endif
9238 }
9239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241}
9242
Bram Moolenaar572cb562005-08-05 21:35:02 +00009243#if defined(FEAT_INS_EXPAND)
9244/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009245 * "complete()" function
9246 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009247 static void
9248f_complete(argvars, rettv)
9249 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009250 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009251{
9252 int startcol;
9253
9254 if ((State & INSERT) == 0)
9255 {
9256 EMSG(_("E785: complete() can only be used in Insert mode"));
9257 return;
9258 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009259
9260 /* Check for undo allowed here, because if something was already inserted
9261 * the line was already saved for undo and this check isn't done. */
9262 if (!undo_allowed())
9263 return;
9264
Bram Moolenaarade00832006-03-10 21:46:58 +00009265 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9266 {
9267 EMSG(_(e_invarg));
9268 return;
9269 }
9270
9271 startcol = get_tv_number_chk(&argvars[0], NULL);
9272 if (startcol <= 0)
9273 return;
9274
9275 set_completion(startcol - 1, argvars[1].vval.v_list);
9276}
9277
9278/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009279 * "complete_add()" function
9280 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009281 static void
9282f_complete_add(argvars, rettv)
9283 typval_T *argvars;
9284 typval_T *rettv;
9285{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009286 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009287}
9288
9289/*
9290 * "complete_check()" function
9291 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009292 static void
9293f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009294 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009295 typval_T *rettv;
9296{
9297 int saved = RedrawingDisabled;
9298
9299 RedrawingDisabled = 0;
9300 ins_compl_check_keys(0);
9301 rettv->vval.v_number = compl_interrupted;
9302 RedrawingDisabled = saved;
9303}
9304#endif
9305
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306/*
9307 * "confirm(message, buttons[, default [, type]])" function
9308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009311 typval_T *argvars UNUSED;
9312 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9315 char_u *message;
9316 char_u *buttons = NULL;
9317 char_u buf[NUMBUFLEN];
9318 char_u buf2[NUMBUFLEN];
9319 int def = 1;
9320 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 char_u *typestr;
9322 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009324 message = get_tv_string_chk(&argvars[0]);
9325 if (message == NULL)
9326 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009327 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9330 if (buttons == NULL)
9331 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009332 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009334 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009335 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9338 if (typestr == NULL)
9339 error = TRUE;
9340 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009342 switch (TOUPPER_ASC(*typestr))
9343 {
9344 case 'E': type = VIM_ERROR; break;
9345 case 'Q': type = VIM_QUESTION; break;
9346 case 'I': type = VIM_INFO; break;
9347 case 'W': type = VIM_WARNING; break;
9348 case 'G': type = VIM_GENERIC; break;
9349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 }
9351 }
9352 }
9353 }
9354
9355 if (buttons == NULL || *buttons == NUL)
9356 buttons = (char_u *)_("&Ok");
9357
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009358 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009360 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361#endif
9362}
9363
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009364/*
9365 * "copy()" function
9366 */
9367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009371{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009372 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009373}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009375#ifdef FEAT_FLOAT
9376/*
9377 * "cos()" function
9378 */
9379 static void
9380f_cos(argvars, rettv)
9381 typval_T *argvars;
9382 typval_T *rettv;
9383{
9384 float_T f;
9385
9386 rettv->v_type = VAR_FLOAT;
9387 if (get_float_arg(argvars, &f) == OK)
9388 rettv->vval.v_float = cos(f);
9389 else
9390 rettv->vval.v_float = 0.0;
9391}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009392
9393/*
9394 * "cosh()" function
9395 */
9396 static void
9397f_cosh(argvars, rettv)
9398 typval_T *argvars;
9399 typval_T *rettv;
9400{
9401 float_T f;
9402
9403 rettv->v_type = VAR_FLOAT;
9404 if (get_float_arg(argvars, &f) == OK)
9405 rettv->vval.v_float = cosh(f);
9406 else
9407 rettv->vval.v_float = 0.0;
9408}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009409#endif
9410
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009412 * "count()" function
9413 */
9414 static void
9415f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 typval_T *argvars;
9417 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009418{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009419 long n = 0;
9420 int ic = FALSE;
9421
Bram Moolenaare9a41262005-01-15 22:18:47 +00009422 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009423 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009424 listitem_T *li;
9425 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009426 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009427
Bram Moolenaare9a41262005-01-15 22:18:47 +00009428 if ((l = argvars[0].vval.v_list) != NULL)
9429 {
9430 li = l->lv_first;
9431 if (argvars[2].v_type != VAR_UNKNOWN)
9432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 int error = FALSE;
9434
9435 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009436 if (argvars[3].v_type != VAR_UNKNOWN)
9437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009438 idx = get_tv_number_chk(&argvars[3], &error);
9439 if (!error)
9440 {
9441 li = list_find(l, idx);
9442 if (li == NULL)
9443 EMSGN(_(e_listidx), idx);
9444 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009446 if (error)
9447 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009448 }
9449
9450 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009451 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009452 ++n;
9453 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009454 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009455 else if (argvars[0].v_type == VAR_DICT)
9456 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 int todo;
9458 dict_T *d;
9459 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009461 if ((d = argvars[0].vval.v_dict) != NULL)
9462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 int error = FALSE;
9464
Bram Moolenaare9a41262005-01-15 22:18:47 +00009465 if (argvars[2].v_type != VAR_UNKNOWN)
9466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009468 if (argvars[3].v_type != VAR_UNKNOWN)
9469 EMSG(_(e_invarg));
9470 }
9471
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009472 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009473 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009474 {
9475 if (!HASHITEM_EMPTY(hi))
9476 {
9477 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009478 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009479 ++n;
9480 }
9481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009482 }
9483 }
9484 else
9485 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009486 rettv->vval.v_number = n;
9487}
9488
9489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9491 *
9492 * Checks the existence of a cscope connection.
9493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009496 typval_T *argvars UNUSED;
9497 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499#ifdef FEAT_CSCOPE
9500 int num = 0;
9501 char_u *dbpath = NULL;
9502 char_u *prepend = NULL;
9503 char_u buf[NUMBUFLEN];
9504
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009505 if (argvars[0].v_type != VAR_UNKNOWN
9506 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508 num = (int)get_tv_number(&argvars[0]);
9509 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009510 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 }
9513
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515#endif
9516}
9517
9518/*
9519 * "cursor(lnum, col)" function
9520 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009521 * Moves the cursor to the specified line and column.
9522 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009530#ifdef FEAT_VIRTUALEDIT
9531 long coladd = 0;
9532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009534 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009535 if (argvars[1].v_type == VAR_UNKNOWN)
9536 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009537 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009538
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009539 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009540 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009541 line = pos.lnum;
9542 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009543#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009544 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009545#endif
9546 }
9547 else
9548 {
9549 line = get_tv_lnum(argvars);
9550 col = get_tv_number_chk(&argvars[1], NULL);
9551#ifdef FEAT_VIRTUALEDIT
9552 if (argvars[2].v_type != VAR_UNKNOWN)
9553 coladd = get_tv_number_chk(&argvars[2], NULL);
9554#endif
9555 }
9556 if (line < 0 || col < 0
9557#ifdef FEAT_VIRTUALEDIT
9558 || coladd < 0
9559#endif
9560 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 if (line > 0)
9563 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 if (col > 0)
9565 curwin->w_cursor.col = col - 1;
9566#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009567 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568#endif
9569
9570 /* Make sure the cursor is in a valid position. */
9571 check_cursor();
9572#ifdef FEAT_MBYTE
9573 /* Correct cursor for multi-byte character. */
9574 if (has_mbyte)
9575 mb_adjust_cursor();
9576#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009577
9578 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009579 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580}
9581
9582/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009583 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 */
9585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009587 typval_T *argvars;
9588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009590 int noref = 0;
9591
9592 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009594 if (noref < 0 || noref > 1)
9595 EMSG(_(e_invarg));
9596 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009597 {
9598 current_copyID += COPYID_INC;
9599 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
9603/*
9604 * "delete()" function
9605 */
9606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009607f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009608 typval_T *argvars;
9609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615}
9616
9617/*
9618 * "did_filetype()" function
9619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009622 typval_T *argvars UNUSED;
9623 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627#endif
9628}
9629
9630/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009631 * "diff_filler()" function
9632 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009635 typval_T *argvars UNUSED;
9636 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009637{
9638#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009640#endif
9641}
9642
9643/*
9644 * "diff_hlID()" function
9645 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009648 typval_T *argvars UNUSED;
9649 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009650{
9651#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009653 static linenr_T prev_lnum = 0;
9654 static int changedtick = 0;
9655 static int fnum = 0;
9656 static int change_start = 0;
9657 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009658 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009659 int filler_lines;
9660 int col;
9661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009662 if (lnum < 0) /* ignore type error in {lnum} arg */
9663 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009664 if (lnum != prev_lnum
9665 || changedtick != curbuf->b_changedtick
9666 || fnum != curbuf->b_fnum)
9667 {
9668 /* New line, buffer, change: need to get the values. */
9669 filler_lines = diff_check(curwin, lnum);
9670 if (filler_lines < 0)
9671 {
9672 if (filler_lines == -1)
9673 {
9674 change_start = MAXCOL;
9675 change_end = -1;
9676 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9677 hlID = HLF_ADD; /* added line */
9678 else
9679 hlID = HLF_CHD; /* changed line */
9680 }
9681 else
9682 hlID = HLF_ADD; /* added line */
9683 }
9684 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009685 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009686 prev_lnum = lnum;
9687 changedtick = curbuf->b_changedtick;
9688 fnum = curbuf->b_fnum;
9689 }
9690
9691 if (hlID == HLF_CHD || hlID == HLF_TXD)
9692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009694 if (col >= change_start && col <= change_end)
9695 hlID = HLF_TXD; /* changed text */
9696 else
9697 hlID = HLF_CHD; /* changed line */
9698 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009699 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009700#endif
9701}
9702
9703/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009704 * "empty({expr})" function
9705 */
9706 static void
9707f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *argvars;
9709 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009710{
9711 int n;
9712
9713 switch (argvars[0].v_type)
9714 {
9715 case VAR_STRING:
9716 case VAR_FUNC:
9717 n = argvars[0].vval.v_string == NULL
9718 || *argvars[0].vval.v_string == NUL;
9719 break;
9720 case VAR_NUMBER:
9721 n = argvars[0].vval.v_number == 0;
9722 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009723#ifdef FEAT_FLOAT
9724 case VAR_FLOAT:
9725 n = argvars[0].vval.v_float == 0.0;
9726 break;
9727#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009728 case VAR_LIST:
9729 n = argvars[0].vval.v_list == NULL
9730 || argvars[0].vval.v_list->lv_first == NULL;
9731 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009732 case VAR_DICT:
9733 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009735 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009736 default:
9737 EMSG2(_(e_intern2), "f_empty()");
9738 n = 0;
9739 }
9740
9741 rettv->vval.v_number = n;
9742}
9743
9744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 * "escape({string}, {chars})" function
9746 */
9747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009749 typval_T *argvars;
9750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751{
9752 char_u buf[NUMBUFLEN];
9753
Bram Moolenaar758711c2005-02-02 23:11:38 +00009754 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9755 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009760 * "eval()" function
9761 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009762 static void
9763f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009766{
9767 char_u *s;
9768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 s = get_tv_string_chk(&argvars[0]);
9770 if (s != NULL)
9771 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009773 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9774 {
9775 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009776 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009778 else if (*s != NUL)
9779 EMSG(_(e_trailing));
9780}
9781
9782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 * "eventhandler()" function
9784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791}
9792
9793/*
9794 * "executable()" function
9795 */
9796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009798 typval_T *argvars;
9799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802}
9803
9804/*
9805 * "exists()" function
9806 */
9807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 typval_T *argvars;
9810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812 char_u *p;
9813 char_u *name;
9814 int n = FALSE;
9815 int len = 0;
9816
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009817 no_autoload = TRUE;
9818
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (*p == '$') /* environment variable */
9821 {
9822 /* first try "normal" environment variables (fast) */
9823 if (mch_getenv(p + 1) != NULL)
9824 n = TRUE;
9825 else
9826 {
9827 /* try expanding things like $VIM and ${HOME} */
9828 p = expand_env_save(p);
9829 if (p != NULL && *p != '$')
9830 n = TRUE;
9831 vim_free(p);
9832 }
9833 }
9834 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009837 if (*skipwhite(p) != NUL)
9838 n = FALSE; /* trailing garbage */
9839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 else if (*p == '*') /* internal or user defined function */
9841 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009842 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 }
9844 else if (*p == ':')
9845 {
9846 n = cmd_exists(p + 1);
9847 }
9848 else if (*p == '#')
9849 {
9850#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009851 if (p[1] == '#')
9852 n = autocmd_supported(p + 2);
9853 else
9854 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855#endif
9856 }
9857 else /* internal variable */
9858 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009859 char_u *tofree;
9860 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009862 /* get_name_len() takes care of expanding curly braces */
9863 name = p;
9864 len = get_name_len(&p, &tofree, TRUE, FALSE);
9865 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009867 if (tofree != NULL)
9868 name = tofree;
9869 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9870 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009872 /* handle d.key, l[idx], f(expr) */
9873 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9874 if (n)
9875 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 }
9877 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009878 if (*p != NUL)
9879 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009881 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 }
9883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009885
9886 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887}
9888
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009889#ifdef FEAT_FLOAT
9890/*
9891 * "exp()" function
9892 */
9893 static void
9894f_exp(argvars, rettv)
9895 typval_T *argvars;
9896 typval_T *rettv;
9897{
9898 float_T f;
9899
9900 rettv->v_type = VAR_FLOAT;
9901 if (get_float_arg(argvars, &f) == OK)
9902 rettv->vval.v_float = exp(f);
9903 else
9904 rettv->vval.v_float = 0.0;
9905}
9906#endif
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908/*
9909 * "expand()" function
9910 */
9911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 typval_T *argvars;
9914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 char_u *s;
9917 int len;
9918 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009919 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009921 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923 rettv->v_type = VAR_STRING;
9924 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 if (*s == '%' || *s == '#' || *s == '<')
9926 {
9927 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009928 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 --emsg_off;
9930 }
9931 else
9932 {
9933 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009934 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 if (argvars[1].v_type != VAR_UNKNOWN
9936 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009937 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 if (!error)
9939 {
9940 ExpandInit(&xpc);
9941 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +01009942 if (p_wic)
9943 options += WILD_ICASE;
9944 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, options, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 }
9946 else
9947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 }
9949}
9950
9951/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009952 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009953 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009954 */
9955 static void
9956f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009959{
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009960 char *arg_errmsg = N_("extend() argument");
9961
Bram Moolenaare9a41262005-01-15 22:18:47 +00009962 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009963 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009964 list_T *l1, *l2;
9965 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009966 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009967 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009968
Bram Moolenaare9a41262005-01-15 22:18:47 +00009969 l1 = argvars[0].vval.v_list;
9970 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +02009971 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009972 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 {
9974 if (argvars[2].v_type != VAR_UNKNOWN)
9975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009976 before = get_tv_number_chk(&argvars[2], &error);
9977 if (error)
9978 return; /* type error; errmsg already given */
9979
Bram Moolenaar758711c2005-02-02 23:11:38 +00009980 if (before == l1->lv_len)
9981 item = NULL;
9982 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009984 item = list_find(l1, before);
9985 if (item == NULL)
9986 {
9987 EMSGN(_(e_listidx), before);
9988 return;
9989 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009990 }
9991 }
9992 else
9993 item = NULL;
9994 list_extend(l1, l2, item);
9995
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 copy_tv(&argvars[0], rettv);
9997 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009998 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10000 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 dict_T *d1, *d2;
10002 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010003 char_u *action;
10004 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010005 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010006 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010007
10008 d1 = argvars[0].vval.v_dict;
10009 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010010 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010011 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010012 {
10013 /* Check the third argument. */
10014 if (argvars[2].v_type != VAR_UNKNOWN)
10015 {
10016 static char *(av[]) = {"keep", "force", "error"};
10017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 action = get_tv_string_chk(&argvars[2]);
10019 if (action == NULL)
10020 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010021 for (i = 0; i < 3; ++i)
10022 if (STRCMP(action, av[i]) == 0)
10023 break;
10024 if (i == 3)
10025 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010026 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010027 return;
10028 }
10029 }
10030 else
10031 action = (char_u *)"force";
10032
10033 /* Go over all entries in the second dict and add them to the
10034 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010035 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010038 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010039 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010040 --todo;
10041 di1 = dict_find(d1, hi2->hi_key, -1);
10042 if (di1 == NULL)
10043 {
10044 di1 = dictitem_copy(HI2DI(hi2));
10045 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10046 dictitem_free(di1);
10047 }
10048 else if (*action == 'e')
10049 {
10050 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10051 break;
10052 }
10053 else if (*action == 'f')
10054 {
10055 clear_tv(&di1->di_tv);
10056 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10057 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010058 }
10059 }
10060
Bram Moolenaare9a41262005-01-15 22:18:47 +000010061 copy_tv(&argvars[0], rettv);
10062 }
10063 }
10064 else
10065 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010066}
10067
10068/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010069 * "feedkeys()" function
10070 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010071 static void
10072f_feedkeys(argvars, rettv)
10073 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010074 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010075{
10076 int remap = TRUE;
10077 char_u *keys, *flags;
10078 char_u nbuf[NUMBUFLEN];
10079 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010080 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010081
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010082 /* This is not allowed in the sandbox. If the commands would still be
10083 * executed in the sandbox it would be OK, but it probably happens later,
10084 * when "sandbox" is no longer set. */
10085 if (check_secure())
10086 return;
10087
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010088 keys = get_tv_string(&argvars[0]);
10089 if (*keys != NUL)
10090 {
10091 if (argvars[1].v_type != VAR_UNKNOWN)
10092 {
10093 flags = get_tv_string_buf(&argvars[1], nbuf);
10094 for ( ; *flags != NUL; ++flags)
10095 {
10096 switch (*flags)
10097 {
10098 case 'n': remap = FALSE; break;
10099 case 'm': remap = TRUE; break;
10100 case 't': typed = TRUE; break;
10101 }
10102 }
10103 }
10104
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010105 /* Need to escape K_SPECIAL and CSI before putting the string in the
10106 * typeahead buffer. */
10107 keys_esc = vim_strsave_escape_csi(keys);
10108 if (keys_esc != NULL)
10109 {
10110 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010111 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010112 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010113 if (vgetc_busy)
10114 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010115 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010116 }
10117}
10118
10119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 * "filereadable()" function
10121 */
10122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 typval_T *argvars;
10125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010127 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 char_u *p;
10129 int n;
10130
Bram Moolenaarc236c162008-07-13 17:41:49 +000010131#ifndef O_NONBLOCK
10132# define O_NONBLOCK 0
10133#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010135 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10136 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 {
10138 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010139 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 }
10141 else
10142 n = FALSE;
10143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145}
10146
10147/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010148 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 * rights to write into.
10150 */
10151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010153 typval_T *argvars;
10154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010156 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157}
10158
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010159static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010160
10161 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010162findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010163 typval_T *argvars;
10164 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010165 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010166{
10167#ifdef FEAT_SEARCHPATH
10168 char_u *fname;
10169 char_u *fresult = NULL;
10170 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10171 char_u *p;
10172 char_u pathbuf[NUMBUFLEN];
10173 int count = 1;
10174 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010175 int error = FALSE;
10176#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010177
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010178 rettv->vval.v_string = NULL;
10179 rettv->v_type = VAR_STRING;
10180
10181#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010183
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010184 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10187 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010188 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 else
10190 {
10191 if (*p != NUL)
10192 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010195 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010197 }
10198
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010199 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10200 error = TRUE;
10201
10202 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010204 do
10205 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010206 if (rettv->v_type == VAR_STRING)
10207 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 fresult = find_file_in_path_option(first ? fname : NULL,
10209 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010210 0, first, path,
10211 find_what,
10212 curbuf->b_ffname,
10213 find_what == FINDFILE_DIR
10214 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010216
10217 if (fresult != NULL && rettv->v_type == VAR_LIST)
10218 list_append_string(rettv->vval.v_list, fresult, -1);
10219
10220 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010222
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010223 if (rettv->v_type == VAR_STRING)
10224 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010225#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010226}
10227
Bram Moolenaar33570922005-01-25 22:26:29 +000010228static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10229static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010230
10231/*
10232 * Implementation of map() and filter().
10233 */
10234 static void
10235filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010236 typval_T *argvars;
10237 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010238 int map;
10239{
10240 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010241 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 listitem_T *li, *nli;
10243 list_T *l = NULL;
10244 dictitem_T *di;
10245 hashtab_T *ht;
10246 hashitem_T *hi;
10247 dict_T *d = NULL;
10248 typval_T save_val;
10249 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010250 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010252 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10253 char *arg_errmsg = (map ? N_("map() argument")
10254 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010255 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010256 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010257
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010259 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010260 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010261 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 return;
10263 }
10264 else if (argvars[0].v_type == VAR_DICT)
10265 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010266 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010267 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 return;
10269 }
10270 else
10271 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010272 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 return;
10274 }
10275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 expr = get_tv_string_buf_chk(&argvars[1], buf);
10277 /* On type errors, the preceding call has already displayed an error
10278 * message. Avoid a misleading error message for an empty string that
10279 * was not passed as argument. */
10280 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010282 prepare_vimvar(VV_VAL, &save_val);
10283 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010284
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010285 /* We reset "did_emsg" to be able to detect whether an error
10286 * occurred during evaluation of the expression. */
10287 save_did_emsg = did_emsg;
10288 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010289
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010290 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 vimvars[VV_KEY].vv_type = VAR_STRING;
10294
10295 ht = &d->dv_hashtab;
10296 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010297 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010298 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 if (!HASHITEM_EMPTY(hi))
10301 {
10302 --todo;
10303 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010304 if (tv_check_lock(di->di_tv.v_lock,
10305 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 break;
10307 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010308 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010309 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 break;
10311 if (!map && rem)
10312 dictitem_remove(d, di);
10313 clear_tv(&vimvars[VV_KEY].vv_tv);
10314 }
10315 }
10316 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010317 }
10318 else
10319 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010320 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 for (li = l->lv_first; li != NULL; li = nli)
10323 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010324 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010325 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010327 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010328 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010329 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010330 break;
10331 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010332 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010333 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010334 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010335 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010336
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010337 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010338 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010339
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010340 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010341 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342
10343 copy_tv(&argvars[0], rettv);
10344}
10345
10346 static int
10347filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010348 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010349 char_u *expr;
10350 int map;
10351 int *remp;
10352{
Bram Moolenaar33570922005-01-25 22:26:29 +000010353 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010354 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010355 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356
Bram Moolenaar33570922005-01-25 22:26:29 +000010357 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010358 s = expr;
10359 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010360 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010361 if (*s != NUL) /* check for trailing chars after expr */
10362 {
10363 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010364 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365 }
10366 if (map)
10367 {
10368 /* map(): replace the list item value */
10369 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010370 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010371 *tv = rettv;
10372 }
10373 else
10374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010375 int error = FALSE;
10376
Bram Moolenaare9a41262005-01-15 22:18:47 +000010377 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010378 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010380 /* On type error, nothing has been removed; return FAIL to stop the
10381 * loop. The error message was given by get_tv_number_chk(). */
10382 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010383 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010385 retval = OK;
10386theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010387 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010388 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010389}
10390
10391/*
10392 * "filter()" function
10393 */
10394 static void
10395f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010396 typval_T *argvars;
10397 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010398{
10399 filter_map(argvars, rettv, FALSE);
10400}
10401
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010402/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010403 * "finddir({fname}[, {path}[, {count}]])" function
10404 */
10405 static void
10406f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010407 typval_T *argvars;
10408 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010409{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010410 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010411}
10412
10413/*
10414 * "findfile({fname}[, {path}[, {count}]])" function
10415 */
10416 static void
10417f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 typval_T *argvars;
10419 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010420{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010421 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010422}
10423
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010424#ifdef FEAT_FLOAT
10425/*
10426 * "float2nr({float})" function
10427 */
10428 static void
10429f_float2nr(argvars, rettv)
10430 typval_T *argvars;
10431 typval_T *rettv;
10432{
10433 float_T f;
10434
10435 if (get_float_arg(argvars, &f) == OK)
10436 {
10437 if (f < -0x7fffffff)
10438 rettv->vval.v_number = -0x7fffffff;
10439 else if (f > 0x7fffffff)
10440 rettv->vval.v_number = 0x7fffffff;
10441 else
10442 rettv->vval.v_number = (varnumber_T)f;
10443 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010444}
10445
10446/*
10447 * "floor({float})" function
10448 */
10449 static void
10450f_floor(argvars, rettv)
10451 typval_T *argvars;
10452 typval_T *rettv;
10453{
10454 float_T f;
10455
10456 rettv->v_type = VAR_FLOAT;
10457 if (get_float_arg(argvars, &f) == OK)
10458 rettv->vval.v_float = floor(f);
10459 else
10460 rettv->vval.v_float = 0.0;
10461}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010462
10463/*
10464 * "fmod()" function
10465 */
10466 static void
10467f_fmod(argvars, rettv)
10468 typval_T *argvars;
10469 typval_T *rettv;
10470{
10471 float_T fx, fy;
10472
10473 rettv->v_type = VAR_FLOAT;
10474 if (get_float_arg(argvars, &fx) == OK
10475 && get_float_arg(&argvars[1], &fy) == OK)
10476 rettv->vval.v_float = fmod(fx, fy);
10477 else
10478 rettv->vval.v_float = 0.0;
10479}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010480#endif
10481
Bram Moolenaar0d660222005-01-07 21:51:51 +000010482/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010483 * "fnameescape({string})" function
10484 */
10485 static void
10486f_fnameescape(argvars, rettv)
10487 typval_T *argvars;
10488 typval_T *rettv;
10489{
10490 rettv->vval.v_string = vim_strsave_fnameescape(
10491 get_tv_string(&argvars[0]), FALSE);
10492 rettv->v_type = VAR_STRING;
10493}
10494
10495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 * "fnamemodify({fname}, {mods})" function
10497 */
10498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 typval_T *argvars;
10501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503 char_u *fname;
10504 char_u *mods;
10505 int usedlen = 0;
10506 int len;
10507 char_u *fbuf = NULL;
10508 char_u buf[NUMBUFLEN];
10509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 fname = get_tv_string_chk(&argvars[0]);
10511 mods = get_tv_string_buf_chk(&argvars[1], buf);
10512 if (fname == NULL || mods == NULL)
10513 fname = NULL;
10514 else
10515 {
10516 len = (int)STRLEN(fname);
10517 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 vim_free(fbuf);
10526}
10527
Bram Moolenaar33570922005-01-25 22:26:29 +000010528static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529
10530/*
10531 * "foldclosed()" function
10532 */
10533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010535 typval_T *argvars;
10536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 int end;
10538{
10539#ifdef FEAT_FOLDING
10540 linenr_T lnum;
10541 linenr_T first, last;
10542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10545 {
10546 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10547 {
10548 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 return;
10553 }
10554 }
10555#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557}
10558
10559/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010560 * "foldclosed()" function
10561 */
10562 static void
10563f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010564 typval_T *argvars;
10565 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010566{
10567 foldclosed_both(argvars, rettv, FALSE);
10568}
10569
10570/*
10571 * "foldclosedend()" function
10572 */
10573 static void
10574f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 typval_T *argvars;
10576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010577{
10578 foldclosed_both(argvars, rettv, TRUE);
10579}
10580
10581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 * "foldlevel()" function
10583 */
10584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010585f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *argvars;
10587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
10589#ifdef FEAT_FOLDING
10590 linenr_T lnum;
10591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596}
10597
10598/*
10599 * "foldtext()" function
10600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010603 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
10606#ifdef FEAT_FOLDING
10607 linenr_T lnum;
10608 char_u *s;
10609 char_u *r;
10610 int len;
10611 char *txt;
10612#endif
10613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 rettv->v_type = VAR_STRING;
10615 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010617 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10618 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10619 <= curbuf->b_ml.ml_line_count
10620 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 {
10622 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010623 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10624 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
10626 if (!linewhite(lnum))
10627 break;
10628 ++lnum;
10629 }
10630
10631 /* Find interesting text in this line. */
10632 s = skipwhite(ml_get(lnum));
10633 /* skip C comment-start */
10634 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010637 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010639 {
10640 s = skipwhite(ml_get(lnum + 1));
10641 if (*s == '*')
10642 s = skipwhite(s + 1);
10643 }
10644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 txt = _("+-%s%3ld lines: ");
10646 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010647 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 + 20 /* for %3ld */
10649 + STRLEN(s))); /* concatenated */
10650 if (r != NULL)
10651 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010652 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10653 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10654 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 len = (int)STRLEN(r);
10656 STRCAT(r, s);
10657 /* remove 'foldmarker' and 'commentstring' */
10658 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 }
10661 }
10662#endif
10663}
10664
10665/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010666 * "foldtextresult(lnum)" function
10667 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010670 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010672{
10673#ifdef FEAT_FOLDING
10674 linenr_T lnum;
10675 char_u *text;
10676 char_u buf[51];
10677 foldinfo_T foldinfo;
10678 int fold_count;
10679#endif
10680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->v_type = VAR_STRING;
10682 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010683#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685 /* treat illegal types and illegal string values for {lnum} the same */
10686 if (lnum < 0)
10687 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010688 fold_count = foldedCount(curwin, lnum, &foldinfo);
10689 if (fold_count > 0)
10690 {
10691 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10692 &foldinfo, buf);
10693 if (text == buf)
10694 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010696 }
10697#endif
10698}
10699
10700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 * "foreground()" function
10702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010705 typval_T *argvars UNUSED;
10706 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708#ifdef FEAT_GUI
10709 if (gui.in_use)
10710 gui_mch_set_foreground();
10711#else
10712# ifdef WIN32
10713 win32_set_foreground();
10714# endif
10715#endif
10716}
10717
10718/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010719 * "function()" function
10720 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010723 typval_T *argvars;
10724 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010725{
10726 char_u *s;
10727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010729 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010730 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010731 /* Don't check an autoload name for existence here. */
10732 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010733 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010734 else
10735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->vval.v_string = vim_strsave(s);
10737 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010738 }
10739}
10740
10741/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010742 * "garbagecollect()" function
10743 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010744 static void
10745f_garbagecollect(argvars, rettv)
10746 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010747 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010748{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010749 /* This is postponed until we are back at the toplevel, because we may be
10750 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10751 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010752
10753 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10754 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010755}
10756
10757/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010758 * "get()" function
10759 */
10760 static void
10761f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 typval_T *argvars;
10763 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010764{
Bram Moolenaar33570922005-01-25 22:26:29 +000010765 listitem_T *li;
10766 list_T *l;
10767 dictitem_T *di;
10768 dict_T *d;
10769 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010770
Bram Moolenaare9a41262005-01-15 22:18:47 +000010771 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010772 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010773 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 int error = FALSE;
10776
10777 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10778 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010780 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 else if (argvars[0].v_type == VAR_DICT)
10783 {
10784 if ((d = argvars[0].vval.v_dict) != NULL)
10785 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010786 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 if (di != NULL)
10788 tv = &di->di_tv;
10789 }
10790 }
10791 else
10792 EMSG2(_(e_listdictarg), "get()");
10793
10794 if (tv == NULL)
10795 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010796 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 copy_tv(&argvars[2], rettv);
10798 }
10799 else
10800 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010801}
10802
Bram Moolenaar342337a2005-07-21 21:11:17 +000010803static 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 +000010804
10805/*
10806 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010807 * Return a range (from start to end) of lines in rettv from the specified
10808 * buffer.
10809 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010810 */
10811 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010812get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010813 buf_T *buf;
10814 linenr_T start;
10815 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010816 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010817 typval_T *rettv;
10818{
10819 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010820
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010821 if (retlist && rettv_list_alloc(rettv) == FAIL)
10822 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010823
10824 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10825 return;
10826
10827 if (!retlist)
10828 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010829 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10830 p = ml_get_buf(buf, start, FALSE);
10831 else
10832 p = (char_u *)"";
10833
10834 rettv->v_type = VAR_STRING;
10835 rettv->vval.v_string = vim_strsave(p);
10836 }
10837 else
10838 {
10839 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010840 return;
10841
10842 if (start < 1)
10843 start = 1;
10844 if (end > buf->b_ml.ml_line_count)
10845 end = buf->b_ml.ml_line_count;
10846 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010847 if (list_append_string(rettv->vval.v_list,
10848 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010849 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010850 }
10851}
10852
10853/*
10854 * "getbufline()" function
10855 */
10856 static void
10857f_getbufline(argvars, rettv)
10858 typval_T *argvars;
10859 typval_T *rettv;
10860{
10861 linenr_T lnum;
10862 linenr_T end;
10863 buf_T *buf;
10864
10865 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10866 ++emsg_off;
10867 buf = get_buf_tv(&argvars[0]);
10868 --emsg_off;
10869
Bram Moolenaar661b1822005-07-28 22:36:45 +000010870 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010871 if (argvars[2].v_type == VAR_UNKNOWN)
10872 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010873 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010874 end = get_tv_lnum_buf(&argvars[2], buf);
10875
Bram Moolenaar342337a2005-07-21 21:11:17 +000010876 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010877}
10878
Bram Moolenaar0d660222005-01-07 21:51:51 +000010879/*
10880 * "getbufvar()" function
10881 */
10882 static void
10883f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *argvars;
10885 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010886{
10887 buf_T *buf;
10888 buf_T *save_curbuf;
10889 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010890 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10893 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010894 ++emsg_off;
10895 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010896
10897 rettv->v_type = VAR_STRING;
10898 rettv->vval.v_string = NULL;
10899
10900 if (buf != NULL && varname != NULL)
10901 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010902 /* set curbuf to be our buf, temporarily */
10903 save_curbuf = curbuf;
10904 curbuf = buf;
10905
Bram Moolenaar0d660222005-01-07 21:51:51 +000010906 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010907 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010010908 else if (STRCMP(varname, "changedtick") == 0)
10909 {
10910 rettv->v_type = VAR_NUMBER;
10911 rettv->vval.v_number = curbuf->b_changedtick;
10912 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010913 else
10914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 if (*varname == NUL)
10916 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10917 * scope prefix before the NUL byte is required by
10918 * find_var_in_ht(). */
10919 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010920 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010921 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010922 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010923 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010924 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010925
10926 /* restore previous notion of curbuf */
10927 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010928 }
10929
10930 --emsg_off;
10931}
10932
10933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 * "getchar()" function
10935 */
10936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010938 typval_T *argvars;
10939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940{
10941 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010944 /* Position the cursor. Needed after a message that ends in a space. */
10945 windgoto(msg_row, msg_col);
10946
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 ++no_mapping;
10948 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010949 for (;;)
10950 {
10951 if (argvars[0].v_type == VAR_UNKNOWN)
10952 /* getchar(): blocking wait. */
10953 n = safe_vgetc();
10954 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10955 /* getchar(1): only check if char avail */
10956 n = vpeekc();
10957 else if (error || vpeekc() == NUL)
10958 /* illegal argument or getchar(0) and no char avail: return zero */
10959 n = 0;
10960 else
10961 /* getchar(0) and char avail: return char */
10962 n = safe_vgetc();
10963 if (n == K_IGNORE)
10964 continue;
10965 break;
10966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 --no_mapping;
10968 --allow_keys;
10969
Bram Moolenaar219b8702006-11-01 14:32:36 +000010970 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10971 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10972 vimvars[VV_MOUSE_COL].vv_nr = 0;
10973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 if (IS_SPECIAL(n) || mod_mask != 0)
10976 {
10977 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10978 int i = 0;
10979
10980 /* Turn a special key into three bytes, plus modifier. */
10981 if (mod_mask != 0)
10982 {
10983 temp[i++] = K_SPECIAL;
10984 temp[i++] = KS_MODIFIER;
10985 temp[i++] = mod_mask;
10986 }
10987 if (IS_SPECIAL(n))
10988 {
10989 temp[i++] = K_SPECIAL;
10990 temp[i++] = K_SECOND(n);
10991 temp[i++] = K_THIRD(n);
10992 }
10993#ifdef FEAT_MBYTE
10994 else if (has_mbyte)
10995 i += (*mb_char2bytes)(n, temp + i);
10996#endif
10997 else
10998 temp[i++] = n;
10999 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 rettv->v_type = VAR_STRING;
11001 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011002
11003#ifdef FEAT_MOUSE
11004 if (n == K_LEFTMOUSE
11005 || n == K_LEFTMOUSE_NM
11006 || n == K_LEFTDRAG
11007 || n == K_LEFTRELEASE
11008 || n == K_LEFTRELEASE_NM
11009 || n == K_MIDDLEMOUSE
11010 || n == K_MIDDLEDRAG
11011 || n == K_MIDDLERELEASE
11012 || n == K_RIGHTMOUSE
11013 || n == K_RIGHTDRAG
11014 || n == K_RIGHTRELEASE
11015 || n == K_X1MOUSE
11016 || n == K_X1DRAG
11017 || n == K_X1RELEASE
11018 || n == K_X2MOUSE
11019 || n == K_X2DRAG
11020 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011021 || n == K_MOUSELEFT
11022 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011023 || n == K_MOUSEDOWN
11024 || n == K_MOUSEUP)
11025 {
11026 int row = mouse_row;
11027 int col = mouse_col;
11028 win_T *win;
11029 linenr_T lnum;
11030# ifdef FEAT_WINDOWS
11031 win_T *wp;
11032# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011033 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011034
11035 if (row >= 0 && col >= 0)
11036 {
11037 /* Find the window at the mouse coordinates and compute the
11038 * text position. */
11039 win = mouse_find_win(&row, &col);
11040 (void)mouse_comp_pos(win, &row, &col, &lnum);
11041# ifdef FEAT_WINDOWS
11042 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011043 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011044# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011045 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011046 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11047 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11048 }
11049 }
11050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 }
11052}
11053
11054/*
11055 * "getcharmod()" function
11056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011059 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063}
11064
11065/*
11066 * "getcmdline()" function
11067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011070 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 rettv->v_type = VAR_STRING;
11074 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075}
11076
11077/*
11078 * "getcmdpos()" function
11079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011082 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086}
11087
11088/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011089 * "getcmdtype()" function
11090 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011091 static void
11092f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011093 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011094 typval_T *rettv;
11095{
11096 rettv->v_type = VAR_STRING;
11097 rettv->vval.v_string = alloc(2);
11098 if (rettv->vval.v_string != NULL)
11099 {
11100 rettv->vval.v_string[0] = get_cmdline_type();
11101 rettv->vval.v_string[1] = NUL;
11102 }
11103}
11104
11105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 * "getcwd()" function
11107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011110 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011113 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011116 rettv->vval.v_string = NULL;
11117 cwd = alloc(MAXPATHL);
11118 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011120 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11121 {
11122 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011124 if (rettv->vval.v_string != NULL)
11125 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011127 }
11128 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 }
11130}
11131
11132/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011133 * "getfontname()" function
11134 */
11135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011136f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011139{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->v_type = VAR_STRING;
11141 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011142#ifdef FEAT_GUI
11143 if (gui.in_use)
11144 {
11145 GuiFont font;
11146 char_u *name = NULL;
11147
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011148 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011149 {
11150 /* Get the "Normal" font. Either the name saved by
11151 * hl_set_font_name() or from the font ID. */
11152 font = gui.norm_font;
11153 name = hl_get_font_name();
11154 }
11155 else
11156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011158 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11159 return;
11160 font = gui_mch_get_font(name, FALSE);
11161 if (font == NOFONT)
11162 return; /* Invalid font name, return empty string. */
11163 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011165 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011166 gui_mch_free_font(font);
11167 }
11168#endif
11169}
11170
11171/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011172 * "getfperm({fname})" function
11173 */
11174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011176 typval_T *argvars;
11177 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011178{
11179 char_u *fname;
11180 struct stat st;
11181 char_u *perm = NULL;
11182 char_u flags[] = "rwx";
11183 int i;
11184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011188 if (mch_stat((char *)fname, &st) >= 0)
11189 {
11190 perm = vim_strsave((char_u *)"---------");
11191 if (perm != NULL)
11192 {
11193 for (i = 0; i < 9; i++)
11194 {
11195 if (st.st_mode & (1 << (8 - i)))
11196 perm[i] = flags[i % 3];
11197 }
11198 }
11199 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011201}
11202
11203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 * "getfsize({fname})" function
11205 */
11206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011208 typval_T *argvars;
11209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210{
11211 char_u *fname;
11212 struct stat st;
11213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011214 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011216 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217
11218 if (mch_stat((char *)fname, &st) >= 0)
11219 {
11220 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011225
11226 /* non-perfect check for overflow */
11227 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11228 rettv->vval.v_number = -2;
11229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 }
11231 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233}
11234
11235/*
11236 * "getftime({fname})" function
11237 */
11238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 typval_T *argvars;
11241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242{
11243 char_u *fname;
11244 struct stat st;
11245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247
11248 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
11254/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011255 * "getftype({fname})" function
11256 */
11257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011259 typval_T *argvars;
11260 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011261{
11262 char_u *fname;
11263 struct stat st;
11264 char_u *type = NULL;
11265 char *t;
11266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011270 if (mch_lstat((char *)fname, &st) >= 0)
11271 {
11272#ifdef S_ISREG
11273 if (S_ISREG(st.st_mode))
11274 t = "file";
11275 else if (S_ISDIR(st.st_mode))
11276 t = "dir";
11277# ifdef S_ISLNK
11278 else if (S_ISLNK(st.st_mode))
11279 t = "link";
11280# endif
11281# ifdef S_ISBLK
11282 else if (S_ISBLK(st.st_mode))
11283 t = "bdev";
11284# endif
11285# ifdef S_ISCHR
11286 else if (S_ISCHR(st.st_mode))
11287 t = "cdev";
11288# endif
11289# ifdef S_ISFIFO
11290 else if (S_ISFIFO(st.st_mode))
11291 t = "fifo";
11292# endif
11293# ifdef S_ISSOCK
11294 else if (S_ISSOCK(st.st_mode))
11295 t = "fifo";
11296# endif
11297 else
11298 t = "other";
11299#else
11300# ifdef S_IFMT
11301 switch (st.st_mode & S_IFMT)
11302 {
11303 case S_IFREG: t = "file"; break;
11304 case S_IFDIR: t = "dir"; break;
11305# ifdef S_IFLNK
11306 case S_IFLNK: t = "link"; break;
11307# endif
11308# ifdef S_IFBLK
11309 case S_IFBLK: t = "bdev"; break;
11310# endif
11311# ifdef S_IFCHR
11312 case S_IFCHR: t = "cdev"; break;
11313# endif
11314# ifdef S_IFIFO
11315 case S_IFIFO: t = "fifo"; break;
11316# endif
11317# ifdef S_IFSOCK
11318 case S_IFSOCK: t = "socket"; break;
11319# endif
11320 default: t = "other";
11321 }
11322# else
11323 if (mch_isdir(fname))
11324 t = "dir";
11325 else
11326 t = "file";
11327# endif
11328#endif
11329 type = vim_strsave((char_u *)t);
11330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011332}
11333
11334/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011335 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011336 */
11337 static void
11338f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011339 typval_T *argvars;
11340 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011341{
11342 linenr_T lnum;
11343 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011344 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345
11346 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011347 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011348 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011349 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011350 retlist = FALSE;
11351 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011352 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011353 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011354 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011355 retlist = TRUE;
11356 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011357
Bram Moolenaar342337a2005-07-21 21:11:17 +000011358 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011359}
11360
11361/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011362 * "getmatches()" function
11363 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011364 static void
11365f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011366 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011367 typval_T *rettv;
11368{
11369#ifdef FEAT_SEARCH_EXTRA
11370 dict_T *dict;
11371 matchitem_T *cur = curwin->w_match_head;
11372
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011373 if (rettv_list_alloc(rettv) == OK)
11374 {
11375 while (cur != NULL)
11376 {
11377 dict = dict_alloc();
11378 if (dict == NULL)
11379 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011380 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11381 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11382 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11383 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11384 list_append_dict(rettv->vval.v_list, dict);
11385 cur = cur->next;
11386 }
11387 }
11388#endif
11389}
11390
11391/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011392 * "getpid()" function
11393 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011394 static void
11395f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011397 typval_T *rettv;
11398{
11399 rettv->vval.v_number = mch_get_pid();
11400}
11401
11402/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011403 * "getpos(string)" function
11404 */
11405 static void
11406f_getpos(argvars, rettv)
11407 typval_T *argvars;
11408 typval_T *rettv;
11409{
11410 pos_T *fp;
11411 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011412 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011413
11414 if (rettv_list_alloc(rettv) == OK)
11415 {
11416 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011417 fp = var2fpos(&argvars[0], TRUE, &fnum);
11418 if (fnum != -1)
11419 list_append_number(l, (varnumber_T)fnum);
11420 else
11421 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011422 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11423 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011424 list_append_number(l, (fp != NULL)
11425 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011426 : (varnumber_T)0);
11427 list_append_number(l,
11428#ifdef FEAT_VIRTUALEDIT
11429 (fp != NULL) ? (varnumber_T)fp->coladd :
11430#endif
11431 (varnumber_T)0);
11432 }
11433 else
11434 rettv->vval.v_number = FALSE;
11435}
11436
11437/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011438 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011439 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011440 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011441f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011442 typval_T *argvars UNUSED;
11443 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011444{
11445#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011446 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011447#endif
11448
Bram Moolenaar2641f772005-03-25 21:58:17 +000011449#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011450 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011451 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011452 wp = NULL;
11453 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11454 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011455 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011456 if (wp == NULL)
11457 return;
11458 }
11459
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011460 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011461 }
11462#endif
11463}
11464
11465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 * "getreg()" function
11467 */
11468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
11473 char_u *strregname;
11474 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011475 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011476 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011478 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 strregname = get_tv_string_chk(&argvars[0]);
11481 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011482 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011486 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 regname = (strregname == NULL ? '"' : *strregname);
11488 if (regname == 0)
11489 regname = '"';
11490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492 rettv->vval.v_string = error ? NULL :
11493 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495
11496/*
11497 * "getregtype()" function
11498 */
11499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 typval_T *argvars;
11502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503{
11504 char_u *strregname;
11505 int regname;
11506 char_u buf[NUMBUFLEN + 2];
11507 long reglen = 0;
11508
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011509 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011510 {
11511 strregname = get_tv_string_chk(&argvars[0]);
11512 if (strregname == NULL) /* type error; errmsg already given */
11513 {
11514 rettv->v_type = VAR_STRING;
11515 rettv->vval.v_string = NULL;
11516 return;
11517 }
11518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 else
11520 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011521 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522
11523 regname = (strregname == NULL ? '"' : *strregname);
11524 if (regname == 0)
11525 regname = '"';
11526
11527 buf[0] = NUL;
11528 buf[1] = NUL;
11529 switch (get_reg_type(regname, &reglen))
11530 {
11531 case MLINE: buf[0] = 'V'; break;
11532 case MCHAR: buf[0] = 'v'; break;
11533#ifdef FEAT_VISUAL
11534 case MBLOCK:
11535 buf[0] = Ctrl_V;
11536 sprintf((char *)buf + 1, "%ld", reglen + 1);
11537 break;
11538#endif
11539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->v_type = VAR_STRING;
11541 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542}
11543
11544/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011545 * "gettabvar()" function
11546 */
11547 static void
11548f_gettabvar(argvars, rettv)
11549 typval_T *argvars;
11550 typval_T *rettv;
11551{
11552 tabpage_T *tp;
11553 dictitem_T *v;
11554 char_u *varname;
11555
11556 rettv->v_type = VAR_STRING;
11557 rettv->vval.v_string = NULL;
11558
11559 varname = get_tv_string_chk(&argvars[1]);
11560 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11561 if (tp != NULL && varname != NULL)
11562 {
11563 /* look up the variable */
11564 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11565 if (v != NULL)
11566 copy_tv(&v->di_tv, rettv);
11567 }
11568}
11569
11570/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011571 * "gettabwinvar()" function
11572 */
11573 static void
11574f_gettabwinvar(argvars, rettv)
11575 typval_T *argvars;
11576 typval_T *rettv;
11577{
11578 getwinvar(argvars, rettv, 1);
11579}
11580
11581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 * "getwinposx()" function
11583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011586 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590#ifdef FEAT_GUI
11591 if (gui.in_use)
11592 {
11593 int x, y;
11594
11595 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 }
11598#endif
11599}
11600
11601/*
11602 * "getwinposy()" function
11603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011606 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610#ifdef FEAT_GUI
11611 if (gui.in_use)
11612 {
11613 int x, y;
11614
11615 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 }
11618#endif
11619}
11620
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011621/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011622 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011623 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011624 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011625find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011626 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011627 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011628{
11629#ifdef FEAT_WINDOWS
11630 win_T *wp;
11631#endif
11632 int nr;
11633
11634 nr = get_tv_number_chk(vp, NULL);
11635
11636#ifdef FEAT_WINDOWS
11637 if (nr < 0)
11638 return NULL;
11639 if (nr == 0)
11640 return curwin;
11641
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011642 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11643 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011644 if (--nr <= 0)
11645 break;
11646 return wp;
11647#else
11648 if (nr == 0 || nr == 1)
11649 return curwin;
11650 return NULL;
11651#endif
11652}
11653
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654/*
11655 * "getwinvar()" function
11656 */
11657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011659 typval_T *argvars;
11660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011662 getwinvar(argvars, rettv, 0);
11663}
11664
11665/*
11666 * getwinvar() and gettabwinvar()
11667 */
11668 static void
11669getwinvar(argvars, rettv, off)
11670 typval_T *argvars;
11671 typval_T *rettv;
11672 int off; /* 1 for gettabwinvar() */
11673{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 win_T *win, *oldcurwin;
11675 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011676 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011677 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011679#ifdef FEAT_WINDOWS
11680 if (off == 1)
11681 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11682 else
11683 tp = curtab;
11684#endif
11685 win = find_win_by_nr(&argvars[off], tp);
11686 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 rettv->v_type = VAR_STRING;
11690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691
11692 if (win != NULL && varname != NULL)
11693 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011694 /* Set curwin to be our win, temporarily. Also set curbuf, so
11695 * that we can get buffer-local options. */
11696 oldcurwin = curwin;
11697 curwin = win;
11698 curbuf = win->w_buffer;
11699
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 else
11703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011704 if (*varname == NUL)
11705 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11706 * scope prefix before the NUL byte is required by
11707 * find_var_in_ht(). */
11708 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011710 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011712 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011714
11715 /* restore previous notion of curwin */
11716 curwin = oldcurwin;
11717 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 }
11719
11720 --emsg_off;
11721}
11722
11723/*
11724 * "glob()" function
11725 */
11726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011728 typval_T *argvars;
11729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011731 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011733 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011735 /* When the optional second argument is non-zero, don't remove matches
11736 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11737 if (argvars[1].v_type != VAR_UNKNOWN
11738 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011739 options |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011741 if (!error)
11742 {
11743 ExpandInit(&xpc);
11744 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011745 if (p_wic)
11746 options += WILD_ICASE;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011747 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011748 NULL, options, WILD_ALL);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011749 }
11750 else
11751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752}
11753
11754/*
11755 * "globpath()" function
11756 */
11757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011759 typval_T *argvars;
11760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011762 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011765 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011767 /* When the optional second argument is non-zero, don't remove matches
11768 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11769 if (argvars[2].v_type != VAR_UNKNOWN
11770 && get_tv_number_chk(&argvars[2], &error))
11771 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011773 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 rettv->vval.v_string = NULL;
11775 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011776 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11777 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778}
11779
11780/*
11781 * "has()" function
11782 */
11783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011785 typval_T *argvars;
11786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787{
11788 int i;
11789 char_u *name;
11790 int n = FALSE;
11791 static char *(has_list[]) =
11792 {
11793#ifdef AMIGA
11794 "amiga",
11795# ifdef FEAT_ARP
11796 "arp",
11797# endif
11798#endif
11799#ifdef __BEOS__
11800 "beos",
11801#endif
11802#ifdef MSDOS
11803# ifdef DJGPP
11804 "dos32",
11805# else
11806 "dos16",
11807# endif
11808#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011809#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 "mac",
11811#endif
11812#if defined(MACOS_X_UNIX)
11813 "macunix",
11814#endif
11815#ifdef OS2
11816 "os2",
11817#endif
11818#ifdef __QNX__
11819 "qnx",
11820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821#ifdef UNIX
11822 "unix",
11823#endif
11824#ifdef VMS
11825 "vms",
11826#endif
11827#ifdef WIN16
11828 "win16",
11829#endif
11830#ifdef WIN32
11831 "win32",
11832#endif
11833#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11834 "win32unix",
11835#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011836#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 "win64",
11838#endif
11839#ifdef EBCDIC
11840 "ebcdic",
11841#endif
11842#ifndef CASE_INSENSITIVE_FILENAME
11843 "fname_case",
11844#endif
11845#ifdef FEAT_ARABIC
11846 "arabic",
11847#endif
11848#ifdef FEAT_AUTOCMD
11849 "autocmd",
11850#endif
11851#ifdef FEAT_BEVAL
11852 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011853# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11854 "balloon_multiline",
11855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856#endif
11857#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11858 "builtin_terms",
11859# ifdef ALL_BUILTIN_TCAPS
11860 "all_builtin_terms",
11861# endif
11862#endif
11863#ifdef FEAT_BYTEOFF
11864 "byte_offset",
11865#endif
11866#ifdef FEAT_CINDENT
11867 "cindent",
11868#endif
11869#ifdef FEAT_CLIENTSERVER
11870 "clientserver",
11871#endif
11872#ifdef FEAT_CLIPBOARD
11873 "clipboard",
11874#endif
11875#ifdef FEAT_CMDL_COMPL
11876 "cmdline_compl",
11877#endif
11878#ifdef FEAT_CMDHIST
11879 "cmdline_hist",
11880#endif
11881#ifdef FEAT_COMMENTS
11882 "comments",
11883#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011884#ifdef FEAT_CONCEAL
11885 "conceal",
11886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887#ifdef FEAT_CRYPT
11888 "cryptv",
11889#endif
11890#ifdef FEAT_CSCOPE
11891 "cscope",
11892#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011893#ifdef FEAT_CURSORBIND
11894 "cursorbind",
11895#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011896#ifdef CURSOR_SHAPE
11897 "cursorshape",
11898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899#ifdef DEBUG
11900 "debug",
11901#endif
11902#ifdef FEAT_CON_DIALOG
11903 "dialog_con",
11904#endif
11905#ifdef FEAT_GUI_DIALOG
11906 "dialog_gui",
11907#endif
11908#ifdef FEAT_DIFF
11909 "diff",
11910#endif
11911#ifdef FEAT_DIGRAPHS
11912 "digraphs",
11913#endif
11914#ifdef FEAT_DND
11915 "dnd",
11916#endif
11917#ifdef FEAT_EMACS_TAGS
11918 "emacs_tags",
11919#endif
11920 "eval", /* always present, of course! */
11921#ifdef FEAT_EX_EXTRA
11922 "ex_extra",
11923#endif
11924#ifdef FEAT_SEARCH_EXTRA
11925 "extra_search",
11926#endif
11927#ifdef FEAT_FKMAP
11928 "farsi",
11929#endif
11930#ifdef FEAT_SEARCHPATH
11931 "file_in_path",
11932#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011933#if defined(UNIX) && !defined(USE_SYSTEM)
11934 "filterpipe",
11935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936#ifdef FEAT_FIND_ID
11937 "find_in_path",
11938#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011939#ifdef FEAT_FLOAT
11940 "float",
11941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942#ifdef FEAT_FOLDING
11943 "folding",
11944#endif
11945#ifdef FEAT_FOOTER
11946 "footer",
11947#endif
11948#if !defined(USE_SYSTEM) && defined(UNIX)
11949 "fork",
11950#endif
11951#ifdef FEAT_GETTEXT
11952 "gettext",
11953#endif
11954#ifdef FEAT_GUI
11955 "gui",
11956#endif
11957#ifdef FEAT_GUI_ATHENA
11958# ifdef FEAT_GUI_NEXTAW
11959 "gui_neXtaw",
11960# else
11961 "gui_athena",
11962# endif
11963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964#ifdef FEAT_GUI_GTK
11965 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011968#ifdef FEAT_GUI_GNOME
11969 "gui_gnome",
11970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971#ifdef FEAT_GUI_MAC
11972 "gui_mac",
11973#endif
11974#ifdef FEAT_GUI_MOTIF
11975 "gui_motif",
11976#endif
11977#ifdef FEAT_GUI_PHOTON
11978 "gui_photon",
11979#endif
11980#ifdef FEAT_GUI_W16
11981 "gui_win16",
11982#endif
11983#ifdef FEAT_GUI_W32
11984 "gui_win32",
11985#endif
11986#ifdef FEAT_HANGULIN
11987 "hangul_input",
11988#endif
11989#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11990 "iconv",
11991#endif
11992#ifdef FEAT_INS_EXPAND
11993 "insert_expand",
11994#endif
11995#ifdef FEAT_JUMPLIST
11996 "jumplist",
11997#endif
11998#ifdef FEAT_KEYMAP
11999 "keymap",
12000#endif
12001#ifdef FEAT_LANGMAP
12002 "langmap",
12003#endif
12004#ifdef FEAT_LIBCALL
12005 "libcall",
12006#endif
12007#ifdef FEAT_LINEBREAK
12008 "linebreak",
12009#endif
12010#ifdef FEAT_LISP
12011 "lispindent",
12012#endif
12013#ifdef FEAT_LISTCMDS
12014 "listcmds",
12015#endif
12016#ifdef FEAT_LOCALMAP
12017 "localmap",
12018#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012019#ifdef FEAT_LUA
12020# ifndef DYNAMIC_LUA
12021 "lua",
12022# endif
12023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024#ifdef FEAT_MENU
12025 "menu",
12026#endif
12027#ifdef FEAT_SESSION
12028 "mksession",
12029#endif
12030#ifdef FEAT_MODIFY_FNAME
12031 "modify_fname",
12032#endif
12033#ifdef FEAT_MOUSE
12034 "mouse",
12035#endif
12036#ifdef FEAT_MOUSESHAPE
12037 "mouseshape",
12038#endif
12039#if defined(UNIX) || defined(VMS)
12040# ifdef FEAT_MOUSE_DEC
12041 "mouse_dec",
12042# endif
12043# ifdef FEAT_MOUSE_GPM
12044 "mouse_gpm",
12045# endif
12046# ifdef FEAT_MOUSE_JSB
12047 "mouse_jsbterm",
12048# endif
12049# ifdef FEAT_MOUSE_NET
12050 "mouse_netterm",
12051# endif
12052# ifdef FEAT_MOUSE_PTERM
12053 "mouse_pterm",
12054# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012055# ifdef FEAT_SYSMOUSE
12056 "mouse_sysmouse",
12057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058# ifdef FEAT_MOUSE_XTERM
12059 "mouse_xterm",
12060# endif
12061#endif
12062#ifdef FEAT_MBYTE
12063 "multi_byte",
12064#endif
12065#ifdef FEAT_MBYTE_IME
12066 "multi_byte_ime",
12067#endif
12068#ifdef FEAT_MULTI_LANG
12069 "multi_lang",
12070#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012071#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012072#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012073 "mzscheme",
12074#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076#ifdef FEAT_OLE
12077 "ole",
12078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079#ifdef FEAT_PATH_EXTRA
12080 "path_extra",
12081#endif
12082#ifdef FEAT_PERL
12083#ifndef DYNAMIC_PERL
12084 "perl",
12085#endif
12086#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012087#ifdef FEAT_PERSISTENT_UNDO
12088 "persistent_undo",
12089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090#ifdef FEAT_PYTHON
12091#ifndef DYNAMIC_PYTHON
12092 "python",
12093#endif
12094#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012095#ifdef FEAT_PYTHON3
12096#ifndef DYNAMIC_PYTHON3
12097 "python3",
12098#endif
12099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100#ifdef FEAT_POSTSCRIPT
12101 "postscript",
12102#endif
12103#ifdef FEAT_PRINTER
12104 "printer",
12105#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012106#ifdef FEAT_PROFILE
12107 "profile",
12108#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012109#ifdef FEAT_RELTIME
12110 "reltime",
12111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112#ifdef FEAT_QUICKFIX
12113 "quickfix",
12114#endif
12115#ifdef FEAT_RIGHTLEFT
12116 "rightleft",
12117#endif
12118#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12119 "ruby",
12120#endif
12121#ifdef FEAT_SCROLLBIND
12122 "scrollbind",
12123#endif
12124#ifdef FEAT_CMDL_INFO
12125 "showcmd",
12126 "cmdline_info",
12127#endif
12128#ifdef FEAT_SIGNS
12129 "signs",
12130#endif
12131#ifdef FEAT_SMARTINDENT
12132 "smartindent",
12133#endif
12134#ifdef FEAT_SNIFF
12135 "sniff",
12136#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012137#ifdef STARTUPTIME
12138 "startuptime",
12139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140#ifdef FEAT_STL_OPT
12141 "statusline",
12142#endif
12143#ifdef FEAT_SUN_WORKSHOP
12144 "sun_workshop",
12145#endif
12146#ifdef FEAT_NETBEANS_INTG
12147 "netbeans_intg",
12148#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012149#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012150 "spell",
12151#endif
12152#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 "syntax",
12154#endif
12155#if defined(USE_SYSTEM) || !defined(UNIX)
12156 "system",
12157#endif
12158#ifdef FEAT_TAG_BINS
12159 "tag_binary",
12160#endif
12161#ifdef FEAT_TAG_OLDSTATIC
12162 "tag_old_static",
12163#endif
12164#ifdef FEAT_TAG_ANYWHITE
12165 "tag_any_white",
12166#endif
12167#ifdef FEAT_TCL
12168# ifndef DYNAMIC_TCL
12169 "tcl",
12170# endif
12171#endif
12172#ifdef TERMINFO
12173 "terminfo",
12174#endif
12175#ifdef FEAT_TERMRESPONSE
12176 "termresponse",
12177#endif
12178#ifdef FEAT_TEXTOBJ
12179 "textobjects",
12180#endif
12181#ifdef HAVE_TGETENT
12182 "tgetent",
12183#endif
12184#ifdef FEAT_TITLE
12185 "title",
12186#endif
12187#ifdef FEAT_TOOLBAR
12188 "toolbar",
12189#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012190#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12191 "unnamedplus",
12192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193#ifdef FEAT_USR_CMDS
12194 "user-commands", /* was accidentally included in 5.4 */
12195 "user_commands",
12196#endif
12197#ifdef FEAT_VIMINFO
12198 "viminfo",
12199#endif
12200#ifdef FEAT_VERTSPLIT
12201 "vertsplit",
12202#endif
12203#ifdef FEAT_VIRTUALEDIT
12204 "virtualedit",
12205#endif
12206#ifdef FEAT_VISUAL
12207 "visual",
12208#endif
12209#ifdef FEAT_VISUALEXTRA
12210 "visualextra",
12211#endif
12212#ifdef FEAT_VREPLACE
12213 "vreplace",
12214#endif
12215#ifdef FEAT_WILDIGN
12216 "wildignore",
12217#endif
12218#ifdef FEAT_WILDMENU
12219 "wildmenu",
12220#endif
12221#ifdef FEAT_WINDOWS
12222 "windows",
12223#endif
12224#ifdef FEAT_WAK
12225 "winaltkeys",
12226#endif
12227#ifdef FEAT_WRITEBACKUP
12228 "writebackup",
12229#endif
12230#ifdef FEAT_XIM
12231 "xim",
12232#endif
12233#ifdef FEAT_XFONTSET
12234 "xfontset",
12235#endif
12236#ifdef USE_XSMP
12237 "xsmp",
12238#endif
12239#ifdef USE_XSMP_INTERACT
12240 "xsmp_interact",
12241#endif
12242#ifdef FEAT_XCLIPBOARD
12243 "xterm_clipboard",
12244#endif
12245#ifdef FEAT_XTERM_SAVE
12246 "xterm_save",
12247#endif
12248#if defined(UNIX) && defined(FEAT_X11)
12249 "X11",
12250#endif
12251 NULL
12252 };
12253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 for (i = 0; has_list[i] != NULL; ++i)
12256 if (STRICMP(name, has_list[i]) == 0)
12257 {
12258 n = TRUE;
12259 break;
12260 }
12261
12262 if (n == FALSE)
12263 {
12264 if (STRNICMP(name, "patch", 5) == 0)
12265 n = has_patch(atoi((char *)name + 5));
12266 else if (STRICMP(name, "vim_starting") == 0)
12267 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012268#ifdef FEAT_MBYTE
12269 else if (STRICMP(name, "multi_byte_encoding") == 0)
12270 n = has_mbyte;
12271#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012272#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12273 else if (STRICMP(name, "balloon_multiline") == 0)
12274 n = multiline_balloon_available();
12275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276#ifdef DYNAMIC_TCL
12277 else if (STRICMP(name, "tcl") == 0)
12278 n = tcl_enabled(FALSE);
12279#endif
12280#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12281 else if (STRICMP(name, "iconv") == 0)
12282 n = iconv_enabled(FALSE);
12283#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012284#ifdef DYNAMIC_LUA
12285 else if (STRICMP(name, "lua") == 0)
12286 n = lua_enabled(FALSE);
12287#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012288#ifdef DYNAMIC_MZSCHEME
12289 else if (STRICMP(name, "mzscheme") == 0)
12290 n = mzscheme_enabled(FALSE);
12291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292#ifdef DYNAMIC_RUBY
12293 else if (STRICMP(name, "ruby") == 0)
12294 n = ruby_enabled(FALSE);
12295#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012296#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297#ifdef DYNAMIC_PYTHON
12298 else if (STRICMP(name, "python") == 0)
12299 n = python_enabled(FALSE);
12300#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012301#endif
12302#ifdef FEAT_PYTHON3
12303#ifdef DYNAMIC_PYTHON3
12304 else if (STRICMP(name, "python3") == 0)
12305 n = python3_enabled(FALSE);
12306#endif
12307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308#ifdef DYNAMIC_PERL
12309 else if (STRICMP(name, "perl") == 0)
12310 n = perl_enabled(FALSE);
12311#endif
12312#ifdef FEAT_GUI
12313 else if (STRICMP(name, "gui_running") == 0)
12314 n = (gui.in_use || gui.starting);
12315# ifdef FEAT_GUI_W32
12316 else if (STRICMP(name, "gui_win32s") == 0)
12317 n = gui_is_win32s();
12318# endif
12319# ifdef FEAT_BROWSE
12320 else if (STRICMP(name, "browse") == 0)
12321 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12322# endif
12323#endif
12324#ifdef FEAT_SYN_HL
12325 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012326 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327#endif
12328#if defined(WIN3264)
12329 else if (STRICMP(name, "win95") == 0)
12330 n = mch_windows95();
12331#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012332#ifdef FEAT_NETBEANS_INTG
12333 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012334 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 }
12337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339}
12340
12341/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012342 * "has_key()" function
12343 */
12344 static void
12345f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 typval_T *argvars;
12347 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012348{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012349 if (argvars[0].v_type != VAR_DICT)
12350 {
12351 EMSG(_(e_dictreq));
12352 return;
12353 }
12354 if (argvars[0].vval.v_dict == NULL)
12355 return;
12356
12357 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012358 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012359}
12360
12361/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012362 * "haslocaldir()" function
12363 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012364 static void
12365f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012366 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012367 typval_T *rettv;
12368{
12369 rettv->vval.v_number = (curwin->w_localdir != NULL);
12370}
12371
12372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 * "hasmapto()" function
12374 */
12375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012377 typval_T *argvars;
12378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379{
12380 char_u *name;
12381 char_u *mode;
12382 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012383 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012386 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 mode = (char_u *)"nvo";
12388 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012390 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012391 if (argvars[2].v_type != VAR_UNKNOWN)
12392 abbr = get_tv_number(&argvars[2]);
12393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394
Bram Moolenaar2c932302006-03-18 21:42:09 +000012395 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399}
12400
12401/*
12402 * "histadd()" function
12403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012405f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012406 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408{
12409#ifdef FEAT_CMDHIST
12410 int histype;
12411 char_u *str;
12412 char_u buf[NUMBUFLEN];
12413#endif
12414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416 if (check_restricted() || check_secure())
12417 return;
12418#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012419 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12420 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 if (histype >= 0)
12422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 if (*str != NUL)
12425 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012426 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 return;
12430 }
12431 }
12432#endif
12433}
12434
12435/*
12436 * "histdel()" function
12437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012440 typval_T *argvars UNUSED;
12441 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442{
12443#ifdef FEAT_CMDHIST
12444 int n;
12445 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012446 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012448 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12449 if (str == NULL)
12450 n = 0;
12451 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012453 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012454 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012456 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 else
12459 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012460 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012461 get_tv_string_buf(&argvars[1], buf));
12462 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463#endif
12464}
12465
12466/*
12467 * "histget()" function
12468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012470f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012471 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473{
12474#ifdef FEAT_CMDHIST
12475 int type;
12476 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012477 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012479 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12480 if (str == NULL)
12481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012483 {
12484 type = get_histtype(str);
12485 if (argvars[1].v_type == VAR_UNKNOWN)
12486 idx = get_history_idx(type);
12487 else
12488 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12489 /* -1 on type error */
12490 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496}
12497
12498/*
12499 * "histnr()" function
12500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012503 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505{
12506 int i;
12507
12508#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 char_u *history = get_tv_string_chk(&argvars[0]);
12510
12511 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 if (i >= HIST_CMD && i < HIST_COUNT)
12513 i = get_history_idx(i);
12514 else
12515#endif
12516 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518}
12519
12520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 * "highlightID(name)" function
12522 */
12523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 typval_T *argvars;
12526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529}
12530
12531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532 * "highlight_exists()" function
12533 */
12534 static void
12535f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012536 typval_T *argvars;
12537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538{
12539 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12540}
12541
12542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 * "hostname()" function
12544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012546f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012547 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549{
12550 char_u hostname[256];
12551
12552 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553 rettv->v_type = VAR_STRING;
12554 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555}
12556
12557/*
12558 * iconv() function
12559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012562 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564{
12565#ifdef FEAT_MBYTE
12566 char_u buf1[NUMBUFLEN];
12567 char_u buf2[NUMBUFLEN];
12568 char_u *from, *to, *str;
12569 vimconv_T vimconv;
12570#endif
12571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->v_type = VAR_STRING;
12573 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574
12575#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012576 str = get_tv_string(&argvars[0]);
12577 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12578 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579 vimconv.vc_type = CONV_NONE;
12580 convert_setup(&vimconv, from, to);
12581
12582 /* If the encodings are equal, no conversion needed. */
12583 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012586 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587
12588 convert_setup(&vimconv, NULL, NULL);
12589 vim_free(from);
12590 vim_free(to);
12591#endif
12592}
12593
12594/*
12595 * "indent()" function
12596 */
12597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012598f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012599 typval_T *argvars;
12600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601{
12602 linenr_T lnum;
12603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012606 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012608 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609}
12610
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012611/*
12612 * "index()" function
12613 */
12614 static void
12615f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012616 typval_T *argvars;
12617 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012618{
Bram Moolenaar33570922005-01-25 22:26:29 +000012619 list_T *l;
12620 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012621 long idx = 0;
12622 int ic = FALSE;
12623
12624 rettv->vval.v_number = -1;
12625 if (argvars[0].v_type != VAR_LIST)
12626 {
12627 EMSG(_(e_listreq));
12628 return;
12629 }
12630 l = argvars[0].vval.v_list;
12631 if (l != NULL)
12632 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012633 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012634 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636 int error = FALSE;
12637
Bram Moolenaar758711c2005-02-02 23:11:38 +000012638 /* Start at specified item. Use the cached index that list_find()
12639 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012641 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012642 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643 ic = get_tv_number_chk(&argvars[3], &error);
12644 if (error)
12645 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012646 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012647
Bram Moolenaar758711c2005-02-02 23:11:38 +000012648 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012649 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012650 {
12651 rettv->vval.v_number = idx;
12652 break;
12653 }
12654 }
12655}
12656
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657static int inputsecret_flag = 0;
12658
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012659static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12660
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012662 * This function is used by f_input() and f_inputdialog() functions. The third
12663 * argument to f_input() specifies the type of completion to use at the
12664 * prompt. The third argument to f_inputdialog() specifies the value to return
12665 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666 */
12667 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012668get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 typval_T *argvars;
12670 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012671 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 char_u *p = NULL;
12675 int c;
12676 char_u buf[NUMBUFLEN];
12677 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012678 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012679 int xp_type = EXPAND_NOTHING;
12680 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012683 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684
12685#ifdef NO_CONSOLE_INPUT
12686 /* While starting up, there is no place to enter text. */
12687 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689#endif
12690
12691 cmd_silent = FALSE; /* Want to see the prompt. */
12692 if (prompt != NULL)
12693 {
12694 /* Only the part of the message after the last NL is considered as
12695 * prompt for the command line */
12696 p = vim_strrchr(prompt, '\n');
12697 if (p == NULL)
12698 p = prompt;
12699 else
12700 {
12701 ++p;
12702 c = *p;
12703 *p = NUL;
12704 msg_start();
12705 msg_clr_eos();
12706 msg_puts_attr(prompt, echo_attr);
12707 msg_didout = FALSE;
12708 msg_starthere();
12709 *p = c;
12710 }
12711 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012713 if (argvars[1].v_type != VAR_UNKNOWN)
12714 {
12715 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12716 if (defstr != NULL)
12717 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012719 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012720 {
12721 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012722 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012723 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012724
Bram Moolenaar4463f292005-09-25 22:20:24 +000012725 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012726
Bram Moolenaar4463f292005-09-25 22:20:24 +000012727 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12728 if (xp_name == NULL)
12729 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012730
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012731 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012732
Bram Moolenaar4463f292005-09-25 22:20:24 +000012733 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12734 &xp_arg) == FAIL)
12735 return;
12736 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012737 }
12738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012739 if (defstr != NULL)
12740 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012741 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12742 xp_type, xp_arg);
12743
12744 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 /* since the user typed this, no need to wait for return */
12747 need_wait_return = FALSE;
12748 msg_didout = FALSE;
12749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 cmd_silent = cmd_silent_save;
12751}
12752
12753/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012754 * "input()" function
12755 * Also handles inputsecret() when inputsecret is set.
12756 */
12757 static void
12758f_input(argvars, rettv)
12759 typval_T *argvars;
12760 typval_T *rettv;
12761{
12762 get_user_input(argvars, rettv, FALSE);
12763}
12764
12765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 * "inputdialog()" function
12767 */
12768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012770 typval_T *argvars;
12771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772{
12773#if defined(FEAT_GUI_TEXTDIALOG)
12774 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12775 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12776 {
12777 char_u *message;
12778 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012779 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012781 message = get_tv_string_chk(&argvars[0]);
12782 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012783 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012784 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 else
12786 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012787 if (message != NULL && defstr != NULL
12788 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010012789 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 else
12792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 if (message != NULL && defstr != NULL
12794 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012795 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 rettv->vval.v_string = vim_strsave(
12797 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 }
12803 else
12804#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012805 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806}
12807
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012808/*
12809 * "inputlist()" function
12810 */
12811 static void
12812f_inputlist(argvars, rettv)
12813 typval_T *argvars;
12814 typval_T *rettv;
12815{
12816 listitem_T *li;
12817 int selected;
12818 int mouse_used;
12819
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012820#ifdef NO_CONSOLE_INPUT
12821 /* While starting up, there is no place to enter text. */
12822 if (no_console_input())
12823 return;
12824#endif
12825 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12826 {
12827 EMSG2(_(e_listarg), "inputlist()");
12828 return;
12829 }
12830
12831 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012832 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012833 lines_left = Rows; /* avoid more prompt */
12834 msg_scroll = TRUE;
12835 msg_clr_eos();
12836
12837 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12838 {
12839 msg_puts(get_tv_string(&li->li_tv));
12840 msg_putchar('\n');
12841 }
12842
12843 /* Ask for choice. */
12844 selected = prompt_for_number(&mouse_used);
12845 if (mouse_used)
12846 selected -= lines_left;
12847
12848 rettv->vval.v_number = selected;
12849}
12850
12851
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12853
12854/*
12855 * "inputrestore()" function
12856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012859 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861{
12862 if (ga_userinput.ga_len > 0)
12863 {
12864 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12866 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012867 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 }
12869 else if (p_verbose > 1)
12870 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012871 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 }
12874}
12875
12876/*
12877 * "inputsave()" function
12878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012880f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012881 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012884 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885 if (ga_grow(&ga_userinput, 1) == OK)
12886 {
12887 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12888 + ga_userinput.ga_len);
12889 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012890 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 }
12892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894}
12895
12896/*
12897 * "inputsecret()" function
12898 */
12899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012901 typval_T *argvars;
12902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903{
12904 ++cmdline_star;
12905 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 --cmdline_star;
12908 --inputsecret_flag;
12909}
12910
12911/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012912 * "insert()" function
12913 */
12914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *argvars;
12917 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012918{
12919 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 listitem_T *item;
12921 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012922 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012923
12924 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012925 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012926 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012927 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012928 {
12929 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012930 before = get_tv_number_chk(&argvars[2], &error);
12931 if (error)
12932 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012933
Bram Moolenaar758711c2005-02-02 23:11:38 +000012934 if (before == l->lv_len)
12935 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012936 else
12937 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012938 item = list_find(l, before);
12939 if (item == NULL)
12940 {
12941 EMSGN(_(e_listidx), before);
12942 l = NULL;
12943 }
12944 }
12945 if (l != NULL)
12946 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012947 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012948 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012949 }
12950 }
12951}
12952
12953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 * "isdirectory()" function
12955 */
12956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012958 typval_T *argvars;
12959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962}
12963
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012964/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012965 * "islocked()" function
12966 */
12967 static void
12968f_islocked(argvars, rettv)
12969 typval_T *argvars;
12970 typval_T *rettv;
12971{
12972 lval_T lv;
12973 char_u *end;
12974 dictitem_T *di;
12975
12976 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012977 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12978 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012979 if (end != NULL && lv.ll_name != NULL)
12980 {
12981 if (*end != NUL)
12982 EMSG(_(e_trailing));
12983 else
12984 {
12985 if (lv.ll_tv == NULL)
12986 {
12987 if (check_changedtick(lv.ll_name))
12988 rettv->vval.v_number = 1; /* always locked */
12989 else
12990 {
12991 di = find_var(lv.ll_name, NULL);
12992 if (di != NULL)
12993 {
12994 /* Consider a variable locked when:
12995 * 1. the variable itself is locked
12996 * 2. the value of the variable is locked.
12997 * 3. the List or Dict value is locked.
12998 */
12999 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13000 || tv_islocked(&di->di_tv));
13001 }
13002 }
13003 }
13004 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013005 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013006 else if (lv.ll_newkey != NULL)
13007 EMSG2(_(e_dictkey), lv.ll_newkey);
13008 else if (lv.ll_list != NULL)
13009 /* List item. */
13010 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13011 else
13012 /* Dictionary item. */
13013 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13014 }
13015 }
13016
13017 clear_lval(&lv);
13018}
13019
Bram Moolenaar33570922005-01-25 22:26:29 +000013020static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013021
13022/*
13023 * Turn a dict into a list:
13024 * "what" == 0: list of keys
13025 * "what" == 1: list of values
13026 * "what" == 2: list of items
13027 */
13028 static void
13029dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013030 typval_T *argvars;
13031 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013032 int what;
13033{
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 list_T *l2;
13035 dictitem_T *di;
13036 hashitem_T *hi;
13037 listitem_T *li;
13038 listitem_T *li2;
13039 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013040 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013041
Bram Moolenaar8c711452005-01-14 21:53:12 +000013042 if (argvars[0].v_type != VAR_DICT)
13043 {
13044 EMSG(_(e_dictreq));
13045 return;
13046 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013047 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013048 return;
13049
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013050 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013051 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013053 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013054 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013055 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013056 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013057 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013058 --todo;
13059 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013060
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013061 li = listitem_alloc();
13062 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013063 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013064 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013065
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013066 if (what == 0)
13067 {
13068 /* keys() */
13069 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013070 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013071 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13072 }
13073 else if (what == 1)
13074 {
13075 /* values() */
13076 copy_tv(&di->di_tv, &li->li_tv);
13077 }
13078 else
13079 {
13080 /* items() */
13081 l2 = list_alloc();
13082 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013083 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013084 li->li_tv.vval.v_list = l2;
13085 if (l2 == NULL)
13086 break;
13087 ++l2->lv_refcount;
13088
13089 li2 = listitem_alloc();
13090 if (li2 == NULL)
13091 break;
13092 list_append(l2, li2);
13093 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013094 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013095 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13096
13097 li2 = listitem_alloc();
13098 if (li2 == NULL)
13099 break;
13100 list_append(l2, li2);
13101 copy_tv(&di->di_tv, &li2->li_tv);
13102 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013103 }
13104 }
13105}
13106
13107/*
13108 * "items(dict)" function
13109 */
13110 static void
13111f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013112 typval_T *argvars;
13113 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013114{
13115 dict_list(argvars, rettv, 2);
13116}
13117
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013119 * "join()" function
13120 */
13121 static void
13122f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013123 typval_T *argvars;
13124 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013125{
13126 garray_T ga;
13127 char_u *sep;
13128
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013129 if (argvars[0].v_type != VAR_LIST)
13130 {
13131 EMSG(_(e_listreq));
13132 return;
13133 }
13134 if (argvars[0].vval.v_list == NULL)
13135 return;
13136 if (argvars[1].v_type == VAR_UNKNOWN)
13137 sep = (char_u *)" ";
13138 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013139 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013140
13141 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013142
13143 if (sep != NULL)
13144 {
13145 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013146 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013147 ga_append(&ga, NUL);
13148 rettv->vval.v_string = (char_u *)ga.ga_data;
13149 }
13150 else
13151 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013152}
13153
13154/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013155 * "keys()" function
13156 */
13157 static void
13158f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013159 typval_T *argvars;
13160 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013161{
13162 dict_list(argvars, rettv, 0);
13163}
13164
13165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 * "last_buffer_nr()" function.
13167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172{
13173 int n = 0;
13174 buf_T *buf;
13175
13176 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13177 if (n < buf->b_fnum)
13178 n = buf->b_fnum;
13179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013181}
13182
13183/*
13184 * "len()" function
13185 */
13186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013188 typval_T *argvars;
13189 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013190{
13191 switch (argvars[0].v_type)
13192 {
13193 case VAR_STRING:
13194 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 rettv->vval.v_number = (varnumber_T)STRLEN(
13196 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013197 break;
13198 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013200 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013201 case VAR_DICT:
13202 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13203 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013204 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013205 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013206 break;
13207 }
13208}
13209
Bram Moolenaar33570922005-01-25 22:26:29 +000013210static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013211
13212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013214 typval_T *argvars;
13215 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013216 int type;
13217{
13218#ifdef FEAT_LIBCALL
13219 char_u *string_in;
13220 char_u **string_result;
13221 int nr_result;
13222#endif
13223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013224 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013225 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013226 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013227
13228 if (check_restricted() || check_secure())
13229 return;
13230
13231#ifdef FEAT_LIBCALL
13232 /* The first two args must be strings, otherwise its meaningless */
13233 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13234 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013235 string_in = NULL;
13236 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013237 string_in = argvars[2].vval.v_string;
13238 if (type == VAR_NUMBER)
13239 string_result = NULL;
13240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013242 if (mch_libcall(argvars[0].vval.v_string,
13243 argvars[1].vval.v_string,
13244 string_in,
13245 argvars[2].vval.v_number,
13246 string_result,
13247 &nr_result) == OK
13248 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013250 }
13251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252}
13253
13254/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013255 * "libcall()" function
13256 */
13257 static void
13258f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013259 typval_T *argvars;
13260 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013261{
13262 libcall_common(argvars, rettv, VAR_STRING);
13263}
13264
13265/*
13266 * "libcallnr()" function
13267 */
13268 static void
13269f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013270 typval_T *argvars;
13271 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013272{
13273 libcall_common(argvars, rettv, VAR_NUMBER);
13274}
13275
13276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 * "line(string)" function
13278 */
13279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013280f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013281 typval_T *argvars;
13282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283{
13284 linenr_T lnum = 0;
13285 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013286 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013288 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 if (fp != NULL)
13290 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292}
13293
13294/*
13295 * "line2byte(lnum)" function
13296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013298f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013299 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301{
13302#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304#else
13305 linenr_T lnum;
13306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013311 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13312 if (rettv->vval.v_number >= 0)
13313 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314#endif
13315}
13316
13317/*
13318 * "lispindent(lnum)" function
13319 */
13320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 typval_T *argvars;
13323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324{
13325#ifdef FEAT_LISP
13326 pos_T pos;
13327 linenr_T lnum;
13328
13329 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13332 {
13333 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 curwin->w_cursor = pos;
13336 }
13337 else
13338#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340}
13341
13342/*
13343 * "localtime()" function
13344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013347 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351}
13352
Bram Moolenaar33570922005-01-25 22:26:29 +000013353static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354
13355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013356get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013357 typval_T *argvars;
13358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 int exact;
13360{
13361 char_u *keys;
13362 char_u *which;
13363 char_u buf[NUMBUFLEN];
13364 char_u *keys_buf = NULL;
13365 char_u *rhs;
13366 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013367 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013368 int get_dict = FALSE;
13369 mapblock_T *mp;
13370 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371
13372 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013373 rettv->v_type = VAR_STRING;
13374 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 if (*keys == NUL)
13378 return;
13379
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013380 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013383 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013384 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013385 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013386 if (argvars[3].v_type != VAR_UNKNOWN)
13387 get_dict = get_tv_number(&argvars[3]);
13388 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 else
13391 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 if (which == NULL)
13393 return;
13394
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 mode = get_map_mode(&which, 0);
13396
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013397 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013398 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013400
13401 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013403 /* Return a string. */
13404 if (rhs != NULL)
13405 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406
Bram Moolenaarbd743252010-10-20 21:23:33 +020013407 }
13408 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13409 {
13410 /* Return a dictionary. */
13411 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13412 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13413 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414
Bram Moolenaarbd743252010-10-20 21:23:33 +020013415 dict_add_nr_str(dict, "lhs", 0L, lhs);
13416 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13417 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13418 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13419 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13420 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13421 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13422 dict_add_nr_str(dict, "mode", 0L, mapmode);
13423
13424 vim_free(lhs);
13425 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426 }
13427}
13428
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013429#ifdef FEAT_FLOAT
13430/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013431 * "log()" function
13432 */
13433 static void
13434f_log(argvars, rettv)
13435 typval_T *argvars;
13436 typval_T *rettv;
13437{
13438 float_T f;
13439
13440 rettv->v_type = VAR_FLOAT;
13441 if (get_float_arg(argvars, &f) == OK)
13442 rettv->vval.v_float = log(f);
13443 else
13444 rettv->vval.v_float = 0.0;
13445}
13446
13447/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013448 * "log10()" function
13449 */
13450 static void
13451f_log10(argvars, rettv)
13452 typval_T *argvars;
13453 typval_T *rettv;
13454{
13455 float_T f;
13456
13457 rettv->v_type = VAR_FLOAT;
13458 if (get_float_arg(argvars, &f) == OK)
13459 rettv->vval.v_float = log10(f);
13460 else
13461 rettv->vval.v_float = 0.0;
13462}
13463#endif
13464
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013466 * "map()" function
13467 */
13468 static void
13469f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013470 typval_T *argvars;
13471 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013472{
13473 filter_map(argvars, rettv, TRUE);
13474}
13475
13476/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013477 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 */
13479 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013480f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013481 typval_T *argvars;
13482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013484 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485}
13486
13487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013488 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 */
13490 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013492 typval_T *argvars;
13493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013495 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496}
13497
Bram Moolenaar33570922005-01-25 22:26:29 +000013498static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499
13500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013502 typval_T *argvars;
13503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 int type;
13505{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013506 char_u *str = NULL;
13507 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 char_u *pat;
13509 regmatch_T regmatch;
13510 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013511 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 char_u *save_cpo;
13513 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013514 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013515 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013516 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013517 list_T *l = NULL;
13518 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013519 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013520 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521
13522 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13523 save_cpo = p_cpo;
13524 p_cpo = (char_u *)"";
13525
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013526 rettv->vval.v_number = -1;
13527 if (type == 3)
13528 {
13529 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013530 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013531 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013532 }
13533 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 rettv->v_type = VAR_STRING;
13536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013539 if (argvars[0].v_type == VAR_LIST)
13540 {
13541 if ((l = argvars[0].vval.v_list) == NULL)
13542 goto theend;
13543 li = l->lv_first;
13544 }
13545 else
13546 expr = str = get_tv_string(&argvars[0]);
13547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13549 if (pat == NULL)
13550 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013551
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013552 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013554 int error = FALSE;
13555
13556 start = get_tv_number_chk(&argvars[2], &error);
13557 if (error)
13558 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013559 if (l != NULL)
13560 {
13561 li = list_find(l, start);
13562 if (li == NULL)
13563 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013564 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013565 }
13566 else
13567 {
13568 if (start < 0)
13569 start = 0;
13570 if (start > (long)STRLEN(str))
13571 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013572 /* When "count" argument is there ignore matches before "start",
13573 * otherwise skip part of the string. Differs when pattern is "^"
13574 * or "\<". */
13575 if (argvars[3].v_type != VAR_UNKNOWN)
13576 startcol = start;
13577 else
13578 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013579 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013580
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013581 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013582 nth = get_tv_number_chk(&argvars[3], &error);
13583 if (error)
13584 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 }
13586
13587 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13588 if (regmatch.regprog != NULL)
13589 {
13590 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013591
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013592 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013593 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013594 if (l != NULL)
13595 {
13596 if (li == NULL)
13597 {
13598 match = FALSE;
13599 break;
13600 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013601 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013602 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013603 if (str == NULL)
13604 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013605 }
13606
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013607 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013608
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013609 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013610 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013611 if (l == NULL && !match)
13612 break;
13613
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013614 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013615 if (l != NULL)
13616 {
13617 li = li->li_next;
13618 ++idx;
13619 }
13620 else
13621 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013622#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013623 startcol = (colnr_T)(regmatch.startp[0]
13624 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013625#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013626 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013627#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013628 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013629 }
13630
13631 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013633 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013634 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013635 int i;
13636
13637 /* return list with matched string and submatches */
13638 for (i = 0; i < NSUBEXP; ++i)
13639 {
13640 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013641 {
13642 if (list_append_string(rettv->vval.v_list,
13643 (char_u *)"", 0) == FAIL)
13644 break;
13645 }
13646 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013647 regmatch.startp[i],
13648 (int)(regmatch.endp[i] - regmatch.startp[i]))
13649 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013650 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013651 }
13652 }
13653 else if (type == 2)
13654 {
13655 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013656 if (l != NULL)
13657 copy_tv(&li->li_tv, rettv);
13658 else
13659 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013661 }
13662 else if (l != NULL)
13663 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 else
13665 {
13666 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 (varnumber_T)(regmatch.startp[0] - str);
13669 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013672 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 }
13674 }
13675 vim_free(regmatch.regprog);
13676 }
13677
13678theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013679 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 p_cpo = save_cpo;
13681}
13682
13683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013684 * "match()" function
13685 */
13686 static void
13687f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *argvars;
13689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013690{
13691 find_some_match(argvars, rettv, 1);
13692}
13693
13694/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013695 * "matchadd()" function
13696 */
13697 static void
13698f_matchadd(argvars, rettv)
13699 typval_T *argvars;
13700 typval_T *rettv;
13701{
13702#ifdef FEAT_SEARCH_EXTRA
13703 char_u buf[NUMBUFLEN];
13704 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13705 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13706 int prio = 10; /* default priority */
13707 int id = -1;
13708 int error = FALSE;
13709
13710 rettv->vval.v_number = -1;
13711
13712 if (grp == NULL || pat == NULL)
13713 return;
13714 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013715 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013716 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013717 if (argvars[3].v_type != VAR_UNKNOWN)
13718 id = get_tv_number_chk(&argvars[3], &error);
13719 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013720 if (error == TRUE)
13721 return;
13722 if (id >= 1 && id <= 3)
13723 {
13724 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13725 return;
13726 }
13727
13728 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13729#endif
13730}
13731
13732/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013733 * "matcharg()" function
13734 */
13735 static void
13736f_matcharg(argvars, rettv)
13737 typval_T *argvars;
13738 typval_T *rettv;
13739{
13740 if (rettv_list_alloc(rettv) == OK)
13741 {
13742#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013743 int id = get_tv_number(&argvars[0]);
13744 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013745
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013746 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013747 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013748 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13749 {
13750 list_append_string(rettv->vval.v_list,
13751 syn_id2name(m->hlg_id), -1);
13752 list_append_string(rettv->vval.v_list, m->pattern, -1);
13753 }
13754 else
13755 {
13756 list_append_string(rettv->vval.v_list, NUL, -1);
13757 list_append_string(rettv->vval.v_list, NUL, -1);
13758 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013759 }
13760#endif
13761 }
13762}
13763
13764/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013765 * "matchdelete()" function
13766 */
13767 static void
13768f_matchdelete(argvars, rettv)
13769 typval_T *argvars;
13770 typval_T *rettv;
13771{
13772#ifdef FEAT_SEARCH_EXTRA
13773 rettv->vval.v_number = match_delete(curwin,
13774 (int)get_tv_number(&argvars[0]), TRUE);
13775#endif
13776}
13777
13778/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013779 * "matchend()" function
13780 */
13781 static void
13782f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013783 typval_T *argvars;
13784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013785{
13786 find_some_match(argvars, rettv, 0);
13787}
13788
13789/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013790 * "matchlist()" function
13791 */
13792 static void
13793f_matchlist(argvars, rettv)
13794 typval_T *argvars;
13795 typval_T *rettv;
13796{
13797 find_some_match(argvars, rettv, 3);
13798}
13799
13800/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013801 * "matchstr()" function
13802 */
13803 static void
13804f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013805 typval_T *argvars;
13806 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013807{
13808 find_some_match(argvars, rettv, 2);
13809}
13810
Bram Moolenaar33570922005-01-25 22:26:29 +000013811static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013812
13813 static void
13814max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013815 typval_T *argvars;
13816 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013817 int domax;
13818{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013819 long n = 0;
13820 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013822
13823 if (argvars[0].v_type == VAR_LIST)
13824 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013825 list_T *l;
13826 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013827
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013828 l = argvars[0].vval.v_list;
13829 if (l != NULL)
13830 {
13831 li = l->lv_first;
13832 if (li != NULL)
13833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013834 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013835 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013836 {
13837 li = li->li_next;
13838 if (li == NULL)
13839 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013841 if (domax ? i > n : i < n)
13842 n = i;
13843 }
13844 }
13845 }
13846 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013847 else if (argvars[0].v_type == VAR_DICT)
13848 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013849 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013850 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013851 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013852 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013853
13854 d = argvars[0].vval.v_dict;
13855 if (d != NULL)
13856 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013857 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013858 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013859 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013860 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013861 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013862 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013864 if (first)
13865 {
13866 n = i;
13867 first = FALSE;
13868 }
13869 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013870 n = i;
13871 }
13872 }
13873 }
13874 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013875 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013876 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013878}
13879
13880/*
13881 * "max()" function
13882 */
13883 static void
13884f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013885 typval_T *argvars;
13886 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013887{
13888 max_min(argvars, rettv, TRUE);
13889}
13890
13891/*
13892 * "min()" function
13893 */
13894 static void
13895f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013896 typval_T *argvars;
13897 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013898{
13899 max_min(argvars, rettv, FALSE);
13900}
13901
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013902static int mkdir_recurse __ARGS((char_u *dir, int prot));
13903
13904/*
13905 * Create the directory in which "dir" is located, and higher levels when
13906 * needed.
13907 */
13908 static int
13909mkdir_recurse(dir, prot)
13910 char_u *dir;
13911 int prot;
13912{
13913 char_u *p;
13914 char_u *updir;
13915 int r = FAIL;
13916
13917 /* Get end of directory name in "dir".
13918 * We're done when it's "/" or "c:/". */
13919 p = gettail_sep(dir);
13920 if (p <= get_past_head(dir))
13921 return OK;
13922
13923 /* If the directory exists we're done. Otherwise: create it.*/
13924 updir = vim_strnsave(dir, (int)(p - dir));
13925 if (updir == NULL)
13926 return FAIL;
13927 if (mch_isdir(updir))
13928 r = OK;
13929 else if (mkdir_recurse(updir, prot) == OK)
13930 r = vim_mkdir_emsg(updir, prot);
13931 vim_free(updir);
13932 return r;
13933}
13934
13935#ifdef vim_mkdir
13936/*
13937 * "mkdir()" function
13938 */
13939 static void
13940f_mkdir(argvars, rettv)
13941 typval_T *argvars;
13942 typval_T *rettv;
13943{
13944 char_u *dir;
13945 char_u buf[NUMBUFLEN];
13946 int prot = 0755;
13947
13948 rettv->vval.v_number = FAIL;
13949 if (check_restricted() || check_secure())
13950 return;
13951
13952 dir = get_tv_string_buf(&argvars[0], buf);
13953 if (argvars[1].v_type != VAR_UNKNOWN)
13954 {
13955 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 prot = get_tv_number_chk(&argvars[2], NULL);
13957 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013958 mkdir_recurse(dir, prot);
13959 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013961}
13962#endif
13963
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 * "mode()" function
13966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013968f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013972 char_u buf[3];
13973
13974 buf[1] = NUL;
13975 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976
13977#ifdef FEAT_VISUAL
13978 if (VIsual_active)
13979 {
13980 if (VIsual_select)
13981 buf[0] = VIsual_mode + 's' - 'v';
13982 else
13983 buf[0] = VIsual_mode;
13984 }
13985 else
13986#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013987 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13988 || State == CONFIRM)
13989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013991 if (State == ASKMORE)
13992 buf[1] = 'm';
13993 else if (State == CONFIRM)
13994 buf[1] = '?';
13995 }
13996 else if (State == EXTERNCMD)
13997 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 else if (State & INSERT)
13999 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014000#ifdef FEAT_VREPLACE
14001 if (State & VREPLACE_FLAG)
14002 {
14003 buf[0] = 'R';
14004 buf[1] = 'v';
14005 }
14006 else
14007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 if (State & REPLACE_FLAG)
14009 buf[0] = 'R';
14010 else
14011 buf[0] = 'i';
14012 }
14013 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014016 if (exmode_active)
14017 buf[1] = 'v';
14018 }
14019 else if (exmode_active)
14020 {
14021 buf[0] = 'c';
14022 buf[1] = 'e';
14023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014025 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014027 if (finish_op)
14028 buf[1] = 'o';
14029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014031 /* Clear out the minor mode when the argument is not a non-zero number or
14032 * non-empty string. */
14033 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014034 buf[1] = NUL;
14035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036 rettv->vval.v_string = vim_strsave(buf);
14037 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038}
14039
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014040#ifdef FEAT_MZSCHEME
14041/*
14042 * "mzeval()" function
14043 */
14044 static void
14045f_mzeval(argvars, rettv)
14046 typval_T *argvars;
14047 typval_T *rettv;
14048{
14049 char_u *str;
14050 char_u buf[NUMBUFLEN];
14051
14052 str = get_tv_string_buf(&argvars[0], buf);
14053 do_mzeval(str, rettv);
14054}
14055#endif
14056
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058 * "nextnonblank()" function
14059 */
14060 static void
14061f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014062 typval_T *argvars;
14063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014064{
14065 linenr_T lnum;
14066
14067 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014069 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014070 {
14071 lnum = 0;
14072 break;
14073 }
14074 if (*skipwhite(ml_get(lnum)) != NUL)
14075 break;
14076 }
14077 rettv->vval.v_number = lnum;
14078}
14079
14080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 * "nr2char()" function
14082 */
14083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014084f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014085 typval_T *argvars;
14086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087{
14088 char_u buf[NUMBUFLEN];
14089
14090#ifdef FEAT_MBYTE
14091 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 else
14094#endif
14095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014096 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 buf[1] = NUL;
14098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 rettv->v_type = VAR_STRING;
14100 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014101}
14102
14103/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014104 * "pathshorten()" function
14105 */
14106 static void
14107f_pathshorten(argvars, rettv)
14108 typval_T *argvars;
14109 typval_T *rettv;
14110{
14111 char_u *p;
14112
14113 rettv->v_type = VAR_STRING;
14114 p = get_tv_string_chk(&argvars[0]);
14115 if (p == NULL)
14116 rettv->vval.v_string = NULL;
14117 else
14118 {
14119 p = vim_strsave(p);
14120 rettv->vval.v_string = p;
14121 if (p != NULL)
14122 shorten_dir(p);
14123 }
14124}
14125
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014126#ifdef FEAT_FLOAT
14127/*
14128 * "pow()" function
14129 */
14130 static void
14131f_pow(argvars, rettv)
14132 typval_T *argvars;
14133 typval_T *rettv;
14134{
14135 float_T fx, fy;
14136
14137 rettv->v_type = VAR_FLOAT;
14138 if (get_float_arg(argvars, &fx) == OK
14139 && get_float_arg(&argvars[1], &fy) == OK)
14140 rettv->vval.v_float = pow(fx, fy);
14141 else
14142 rettv->vval.v_float = 0.0;
14143}
14144#endif
14145
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014146/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147 * "prevnonblank()" function
14148 */
14149 static void
14150f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014151 typval_T *argvars;
14152 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014153{
14154 linenr_T lnum;
14155
14156 lnum = get_tv_lnum(argvars);
14157 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14158 lnum = 0;
14159 else
14160 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14161 --lnum;
14162 rettv->vval.v_number = lnum;
14163}
14164
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014165#ifdef HAVE_STDARG_H
14166/* This dummy va_list is here because:
14167 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14168 * - locally in the function results in a "used before set" warning
14169 * - using va_start() to initialize it gives "function with fixed args" error */
14170static va_list ap;
14171#endif
14172
Bram Moolenaar8c711452005-01-14 21:53:12 +000014173/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014174 * "printf()" function
14175 */
14176 static void
14177f_printf(argvars, rettv)
14178 typval_T *argvars;
14179 typval_T *rettv;
14180{
14181 rettv->v_type = VAR_STRING;
14182 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014183#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014184 {
14185 char_u buf[NUMBUFLEN];
14186 int len;
14187 char_u *s;
14188 int saved_did_emsg = did_emsg;
14189 char *fmt;
14190
14191 /* Get the required length, allocate the buffer and do it for real. */
14192 did_emsg = FALSE;
14193 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014194 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014195 if (!did_emsg)
14196 {
14197 s = alloc(len + 1);
14198 if (s != NULL)
14199 {
14200 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014201 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014202 }
14203 }
14204 did_emsg |= saved_did_emsg;
14205 }
14206#endif
14207}
14208
14209/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014210 * "pumvisible()" function
14211 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014212 static void
14213f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014214 typval_T *argvars UNUSED;
14215 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014216{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014217#ifdef FEAT_INS_EXPAND
14218 if (pum_visible())
14219 rettv->vval.v_number = 1;
14220#endif
14221}
14222
14223/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014224 * "range()" function
14225 */
14226 static void
14227f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014228 typval_T *argvars;
14229 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014230{
14231 long start;
14232 long end;
14233 long stride = 1;
14234 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014238 if (argvars[1].v_type == VAR_UNKNOWN)
14239 {
14240 end = start - 1;
14241 start = 0;
14242 }
14243 else
14244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014245 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014246 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014248 }
14249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 if (error)
14251 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014252 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014253 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014254 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014255 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014256 else
14257 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014258 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014259 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014260 if (list_append_number(rettv->vval.v_list,
14261 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014262 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014263 }
14264}
14265
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014266/*
14267 * "readfile()" function
14268 */
14269 static void
14270f_readfile(argvars, rettv)
14271 typval_T *argvars;
14272 typval_T *rettv;
14273{
14274 int binary = FALSE;
14275 char_u *fname;
14276 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014277 listitem_T *li;
14278#define FREAD_SIZE 200 /* optimized for text lines */
14279 char_u buf[FREAD_SIZE];
14280 int readlen; /* size of last fread() */
14281 int buflen; /* nr of valid chars in buf[] */
14282 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14283 int tolist; /* first byte in buf[] still to be put in list */
14284 int chop; /* how many CR to chop off */
14285 char_u *prev = NULL; /* previously read bytes, if any */
14286 int prevlen = 0; /* length of "prev" if not NULL */
14287 char_u *s;
14288 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014289 long maxline = MAXLNUM;
14290 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014291
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014292 if (argvars[1].v_type != VAR_UNKNOWN)
14293 {
14294 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14295 binary = TRUE;
14296 if (argvars[2].v_type != VAR_UNKNOWN)
14297 maxline = get_tv_number(&argvars[2]);
14298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014300 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014301 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014302
14303 /* Always open the file in binary mode, library functions have a mind of
14304 * their own about CR-LF conversion. */
14305 fname = get_tv_string(&argvars[0]);
14306 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14307 {
14308 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14309 return;
14310 }
14311
14312 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014313 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014314 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014315 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014316 buflen = filtd + readlen;
14317 tolist = 0;
14318 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14319 {
14320 if (buf[filtd] == '\n' || readlen <= 0)
14321 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014322 /* In binary mode add an empty list item when the last
14323 * non-empty line ends in a '\n'. */
14324 if (!binary && readlen == 0 && filtd == 0 && prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014325 break;
14326
14327 /* Found end-of-line or end-of-file: add a text line to the
14328 * list. */
14329 chop = 0;
14330 if (!binary)
14331 while (filtd - chop - 1 >= tolist
14332 && buf[filtd - chop - 1] == '\r')
14333 ++chop;
14334 len = filtd - tolist - chop;
14335 if (prev == NULL)
14336 s = vim_strnsave(buf + tolist, len);
14337 else
14338 {
14339 s = alloc((unsigned)(prevlen + len + 1));
14340 if (s != NULL)
14341 {
14342 mch_memmove(s, prev, prevlen);
14343 vim_free(prev);
14344 prev = NULL;
14345 mch_memmove(s + prevlen, buf + tolist, len);
14346 s[prevlen + len] = NUL;
14347 }
14348 }
14349 tolist = filtd + 1;
14350
14351 li = listitem_alloc();
14352 if (li == NULL)
14353 {
14354 vim_free(s);
14355 break;
14356 }
14357 li->li_tv.v_type = VAR_STRING;
14358 li->li_tv.v_lock = 0;
14359 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014360 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014361
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014362 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014363 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014364 if (readlen <= 0)
14365 break;
14366 }
14367 else if (buf[filtd] == NUL)
14368 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014369#ifdef FEAT_MBYTE
14370 else if (buf[filtd] == 0xef
14371 && enc_utf8
14372 && filtd + 2 < buflen
14373 && !binary
14374 && buf[filtd + 1] == 0xbb
14375 && buf[filtd + 2] == 0xbf)
14376 {
14377 /* remove utf-8 byte order mark */
14378 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14379 --filtd;
14380 buflen -= 3;
14381 }
14382#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014383 }
14384 if (readlen <= 0)
14385 break;
14386
14387 if (tolist == 0)
14388 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014389 if (buflen >= FREAD_SIZE / 2)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014390 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014391 /* "buf" is full, need to move text to an allocated buffer */
14392 if (prev == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014393 {
Bram Moolenaar27b60562011-04-01 16:07:46 +020014394 prev = vim_strnsave(buf, buflen);
14395 prevlen = buflen;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014396 }
Bram Moolenaar27b60562011-04-01 16:07:46 +020014397 else
14398 {
14399 s = alloc((unsigned)(prevlen + buflen));
14400 if (s != NULL)
14401 {
14402 mch_memmove(s, prev, prevlen);
14403 mch_memmove(s + prevlen, buf, buflen);
14404 vim_free(prev);
14405 prev = s;
14406 prevlen += buflen;
14407 }
14408 }
14409 filtd = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014410 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014411 }
14412 else
14413 {
14414 mch_memmove(buf, buf + tolist, buflen - tolist);
14415 filtd -= tolist;
14416 }
14417 }
14418
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014419 /*
14420 * For a negative line count use only the lines at the end of the file,
14421 * free the rest.
14422 */
14423 if (maxline < 0)
14424 while (cnt > -maxline)
14425 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014426 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014427 --cnt;
14428 }
14429
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014430 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014431 fclose(fd);
14432}
14433
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014434#if defined(FEAT_RELTIME)
14435static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14436
14437/*
14438 * Convert a List to proftime_T.
14439 * Return FAIL when there is something wrong.
14440 */
14441 static int
14442list2proftime(arg, tm)
14443 typval_T *arg;
14444 proftime_T *tm;
14445{
14446 long n1, n2;
14447 int error = FALSE;
14448
14449 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14450 || arg->vval.v_list->lv_len != 2)
14451 return FAIL;
14452 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14453 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14454# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014455 tm->HighPart = n1;
14456 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014457# else
14458 tm->tv_sec = n1;
14459 tm->tv_usec = n2;
14460# endif
14461 return error ? FAIL : OK;
14462}
14463#endif /* FEAT_RELTIME */
14464
14465/*
14466 * "reltime()" function
14467 */
14468 static void
14469f_reltime(argvars, rettv)
14470 typval_T *argvars;
14471 typval_T *rettv;
14472{
14473#ifdef FEAT_RELTIME
14474 proftime_T res;
14475 proftime_T start;
14476
14477 if (argvars[0].v_type == VAR_UNKNOWN)
14478 {
14479 /* No arguments: get current time. */
14480 profile_start(&res);
14481 }
14482 else if (argvars[1].v_type == VAR_UNKNOWN)
14483 {
14484 if (list2proftime(&argvars[0], &res) == FAIL)
14485 return;
14486 profile_end(&res);
14487 }
14488 else
14489 {
14490 /* Two arguments: compute the difference. */
14491 if (list2proftime(&argvars[0], &start) == FAIL
14492 || list2proftime(&argvars[1], &res) == FAIL)
14493 return;
14494 profile_sub(&res, &start);
14495 }
14496
14497 if (rettv_list_alloc(rettv) == OK)
14498 {
14499 long n1, n2;
14500
14501# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014502 n1 = res.HighPart;
14503 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014504# else
14505 n1 = res.tv_sec;
14506 n2 = res.tv_usec;
14507# endif
14508 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14509 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14510 }
14511#endif
14512}
14513
14514/*
14515 * "reltimestr()" function
14516 */
14517 static void
14518f_reltimestr(argvars, rettv)
14519 typval_T *argvars;
14520 typval_T *rettv;
14521{
14522#ifdef FEAT_RELTIME
14523 proftime_T tm;
14524#endif
14525
14526 rettv->v_type = VAR_STRING;
14527 rettv->vval.v_string = NULL;
14528#ifdef FEAT_RELTIME
14529 if (list2proftime(&argvars[0], &tm) == OK)
14530 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14531#endif
14532}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014533
Bram Moolenaar0d660222005-01-07 21:51:51 +000014534#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14535static void make_connection __ARGS((void));
14536static int check_connection __ARGS((void));
14537
14538 static void
14539make_connection()
14540{
14541 if (X_DISPLAY == NULL
14542# ifdef FEAT_GUI
14543 && !gui.in_use
14544# endif
14545 )
14546 {
14547 x_force_connect = TRUE;
14548 setup_term_clip();
14549 x_force_connect = FALSE;
14550 }
14551}
14552
14553 static int
14554check_connection()
14555{
14556 make_connection();
14557 if (X_DISPLAY == NULL)
14558 {
14559 EMSG(_("E240: No connection to Vim server"));
14560 return FAIL;
14561 }
14562 return OK;
14563}
14564#endif
14565
14566#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014567static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568
14569 static void
14570remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014571 typval_T *argvars;
14572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573 int expr;
14574{
14575 char_u *server_name;
14576 char_u *keys;
14577 char_u *r = NULL;
14578 char_u buf[NUMBUFLEN];
14579# ifdef WIN32
14580 HWND w;
14581# else
14582 Window w;
14583# endif
14584
14585 if (check_restricted() || check_secure())
14586 return;
14587
14588# ifdef FEAT_X11
14589 if (check_connection() == FAIL)
14590 return;
14591# endif
14592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 server_name = get_tv_string_chk(&argvars[0]);
14594 if (server_name == NULL)
14595 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014596 keys = get_tv_string_buf(&argvars[1], buf);
14597# ifdef WIN32
14598 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14599# else
14600 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14601 < 0)
14602# endif
14603 {
14604 if (r != NULL)
14605 EMSG(r); /* sending worked but evaluation failed */
14606 else
14607 EMSG2(_("E241: Unable to send to %s"), server_name);
14608 return;
14609 }
14610
14611 rettv->vval.v_string = r;
14612
14613 if (argvars[2].v_type != VAR_UNKNOWN)
14614 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014615 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014616 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014617 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014618
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014619 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014620 v.di_tv.v_type = VAR_STRING;
14621 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014622 idvar = get_tv_string_chk(&argvars[2]);
14623 if (idvar != NULL)
14624 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014626 }
14627}
14628#endif
14629
14630/*
14631 * "remote_expr()" function
14632 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 static void
14634f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014636 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637{
14638 rettv->v_type = VAR_STRING;
14639 rettv->vval.v_string = NULL;
14640#ifdef FEAT_CLIENTSERVER
14641 remote_common(argvars, rettv, TRUE);
14642#endif
14643}
14644
14645/*
14646 * "remote_foreground()" function
14647 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648 static void
14649f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014650 typval_T *argvars UNUSED;
14651 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014653#ifdef FEAT_CLIENTSERVER
14654# ifdef WIN32
14655 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 {
14657 char_u *server_name = get_tv_string_chk(&argvars[0]);
14658
14659 if (server_name != NULL)
14660 serverForeground(server_name);
14661 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662# else
14663 /* Send a foreground() expression to the server. */
14664 argvars[1].v_type = VAR_STRING;
14665 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14666 argvars[2].v_type = VAR_UNKNOWN;
14667 remote_common(argvars, rettv, TRUE);
14668 vim_free(argvars[1].vval.v_string);
14669# endif
14670#endif
14671}
14672
Bram Moolenaar0d660222005-01-07 21:51:51 +000014673 static void
14674f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014675 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014676 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014677{
14678#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014679 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014680 char_u *s = NULL;
14681# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014682 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014684 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014685
14686 if (check_restricted() || check_secure())
14687 {
14688 rettv->vval.v_number = -1;
14689 return;
14690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014691 serverid = get_tv_string_chk(&argvars[0]);
14692 if (serverid == NULL)
14693 {
14694 rettv->vval.v_number = -1;
14695 return; /* type error; errmsg already given */
14696 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014697# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014698 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699 if (n == 0)
14700 rettv->vval.v_number = -1;
14701 else
14702 {
14703 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14704 rettv->vval.v_number = (s != NULL);
14705 }
14706# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707 if (check_connection() == FAIL)
14708 return;
14709
14710 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014711 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014712# endif
14713
14714 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014716 char_u *retvar;
14717
Bram Moolenaar33570922005-01-25 22:26:29 +000014718 v.di_tv.v_type = VAR_STRING;
14719 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014720 retvar = get_tv_string_chk(&argvars[1]);
14721 if (retvar != NULL)
14722 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014723 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014724 }
14725#else
14726 rettv->vval.v_number = -1;
14727#endif
14728}
14729
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730 static void
14731f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014733 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014734{
14735 char_u *r = NULL;
14736
14737#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014738 char_u *serverid = get_tv_string_chk(&argvars[0]);
14739
14740 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014741 {
14742# ifdef WIN32
14743 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014744 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014745
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014746 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014747 if (n != 0)
14748 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14749 if (r == NULL)
14750# else
14751 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014752 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753# endif
14754 EMSG(_("E277: Unable to read a server reply"));
14755 }
14756#endif
14757 rettv->v_type = VAR_STRING;
14758 rettv->vval.v_string = r;
14759}
14760
14761/*
14762 * "remote_send()" function
14763 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014764 static void
14765f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014766 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014767 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014768{
14769 rettv->v_type = VAR_STRING;
14770 rettv->vval.v_string = NULL;
14771#ifdef FEAT_CLIENTSERVER
14772 remote_common(argvars, rettv, FALSE);
14773#endif
14774}
14775
14776/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014777 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014778 */
14779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014780f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014781 typval_T *argvars;
14782 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014783{
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 list_T *l;
14785 listitem_T *item, *item2;
14786 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014787 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014788 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014789 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014790 dict_T *d;
14791 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014792 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014793
Bram Moolenaar8c711452005-01-14 21:53:12 +000014794 if (argvars[0].v_type == VAR_DICT)
14795 {
14796 if (argvars[2].v_type != VAR_UNKNOWN)
14797 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014798 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014799 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014801 key = get_tv_string_chk(&argvars[1]);
14802 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014804 di = dict_find(d, key, -1);
14805 if (di == NULL)
14806 EMSG2(_(e_dictkey), key);
14807 else
14808 {
14809 *rettv = di->di_tv;
14810 init_tv(&di->di_tv);
14811 dictitem_remove(d, di);
14812 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014814 }
14815 }
14816 else if (argvars[0].v_type != VAR_LIST)
14817 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014818 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020014819 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014821 int error = FALSE;
14822
14823 idx = get_tv_number_chk(&argvars[1], &error);
14824 if (error)
14825 ; /* type error: do nothing, errmsg already given */
14826 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827 EMSGN(_(e_listidx), idx);
14828 else
14829 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014830 if (argvars[2].v_type == VAR_UNKNOWN)
14831 {
14832 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014833 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014834 *rettv = item->li_tv;
14835 vim_free(item);
14836 }
14837 else
14838 {
14839 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014840 end = get_tv_number_chk(&argvars[2], &error);
14841 if (error)
14842 ; /* type error: do nothing */
14843 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014844 EMSGN(_(e_listidx), end);
14845 else
14846 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014847 int cnt = 0;
14848
14849 for (li = item; li != NULL; li = li->li_next)
14850 {
14851 ++cnt;
14852 if (li == item2)
14853 break;
14854 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014855 if (li == NULL) /* didn't find "item2" after "item" */
14856 EMSG(_(e_invrange));
14857 else
14858 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014859 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014860 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014861 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014862 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014863 l->lv_first = item;
14864 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014865 item->li_prev = NULL;
14866 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014867 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014868 }
14869 }
14870 }
14871 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014872 }
14873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874}
14875
14876/*
14877 * "rename({from}, {to})" function
14878 */
14879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014880f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014881 typval_T *argvars;
14882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883{
14884 char_u buf[NUMBUFLEN];
14885
14886 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14890 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891}
14892
14893/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014894 * "repeat()" function
14895 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014896 static void
14897f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014898 typval_T *argvars;
14899 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014900{
14901 char_u *p;
14902 int n;
14903 int slen;
14904 int len;
14905 char_u *r;
14906 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014907
14908 n = get_tv_number(&argvars[1]);
14909 if (argvars[0].v_type == VAR_LIST)
14910 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014911 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014912 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014913 if (list_extend(rettv->vval.v_list,
14914 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014915 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014916 }
14917 else
14918 {
14919 p = get_tv_string(&argvars[0]);
14920 rettv->v_type = VAR_STRING;
14921 rettv->vval.v_string = NULL;
14922
14923 slen = (int)STRLEN(p);
14924 len = slen * n;
14925 if (len <= 0)
14926 return;
14927
14928 r = alloc(len + 1);
14929 if (r != NULL)
14930 {
14931 for (i = 0; i < n; i++)
14932 mch_memmove(r + i * slen, p, (size_t)slen);
14933 r[len] = NUL;
14934 }
14935
14936 rettv->vval.v_string = r;
14937 }
14938}
14939
14940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 * "resolve()" function
14942 */
14943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014944f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 typval_T *argvars;
14946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947{
14948 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020014949#ifdef HAVE_READLINK
14950 char_u *buf = NULL;
14951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954#ifdef FEAT_SHORTCUT
14955 {
14956 char_u *v = NULL;
14957
14958 v = mch_resolve_shortcut(p);
14959 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014960 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014962 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 }
14964#else
14965# ifdef HAVE_READLINK
14966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 char_u *cpy;
14968 int len;
14969 char_u *remain = NULL;
14970 char_u *q;
14971 int is_relative_to_current = FALSE;
14972 int has_trailing_pathsep = FALSE;
14973 int limit = 100;
14974
14975 p = vim_strsave(p);
14976
14977 if (p[0] == '.' && (vim_ispathsep(p[1])
14978 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14979 is_relative_to_current = TRUE;
14980
14981 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014982 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020014985 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
14986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987
14988 q = getnextcomp(p);
14989 if (*q != NUL)
14990 {
14991 /* Separate the first path component in "p", and keep the
14992 * remainder (beginning with the path separator). */
14993 remain = vim_strsave(q - 1);
14994 q[-1] = NUL;
14995 }
14996
Bram Moolenaard9462e32011-04-11 21:35:11 +020014997 buf = alloc(MAXPATHL + 1);
14998 if (buf == NULL)
14999 goto fail;
15000
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 for (;;)
15002 {
15003 for (;;)
15004 {
15005 len = readlink((char *)p, (char *)buf, MAXPATHL);
15006 if (len <= 0)
15007 break;
15008 buf[len] = NUL;
15009
15010 if (limit-- == 0)
15011 {
15012 vim_free(p);
15013 vim_free(remain);
15014 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 goto fail;
15017 }
15018
15019 /* Ensure that the result will have a trailing path separator
15020 * if the argument has one. */
15021 if (remain == NULL && has_trailing_pathsep)
15022 add_pathsep(buf);
15023
15024 /* Separate the first path component in the link value and
15025 * concatenate the remainders. */
15026 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15027 if (*q != NUL)
15028 {
15029 if (remain == NULL)
15030 remain = vim_strsave(q - 1);
15031 else
15032 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015033 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 if (cpy != NULL)
15035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036 vim_free(remain);
15037 remain = cpy;
15038 }
15039 }
15040 q[-1] = NUL;
15041 }
15042
15043 q = gettail(p);
15044 if (q > p && *q == NUL)
15045 {
15046 /* Ignore trailing path separator. */
15047 q[-1] = NUL;
15048 q = gettail(p);
15049 }
15050 if (q > p && !mch_isFullName(buf))
15051 {
15052 /* symlink is relative to directory of argument */
15053 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15054 if (cpy != NULL)
15055 {
15056 STRCPY(cpy, p);
15057 STRCPY(gettail(cpy), buf);
15058 vim_free(p);
15059 p = cpy;
15060 }
15061 }
15062 else
15063 {
15064 vim_free(p);
15065 p = vim_strsave(buf);
15066 }
15067 }
15068
15069 if (remain == NULL)
15070 break;
15071
15072 /* Append the first path component of "remain" to "p". */
15073 q = getnextcomp(remain + 1);
15074 len = q - remain - (*q != NUL);
15075 cpy = vim_strnsave(p, STRLEN(p) + len);
15076 if (cpy != NULL)
15077 {
15078 STRNCAT(cpy, remain, len);
15079 vim_free(p);
15080 p = cpy;
15081 }
15082 /* Shorten "remain". */
15083 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015084 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 else
15086 {
15087 vim_free(remain);
15088 remain = NULL;
15089 }
15090 }
15091
15092 /* If the result is a relative path name, make it explicitly relative to
15093 * the current directory if and only if the argument had this form. */
15094 if (!vim_ispathsep(*p))
15095 {
15096 if (is_relative_to_current
15097 && *p != NUL
15098 && !(p[0] == '.'
15099 && (p[1] == NUL
15100 || vim_ispathsep(p[1])
15101 || (p[1] == '.'
15102 && (p[2] == NUL
15103 || vim_ispathsep(p[2]))))))
15104 {
15105 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015106 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 if (cpy != NULL)
15108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 vim_free(p);
15110 p = cpy;
15111 }
15112 }
15113 else if (!is_relative_to_current)
15114 {
15115 /* Strip leading "./". */
15116 q = p;
15117 while (q[0] == '.' && vim_ispathsep(q[1]))
15118 q += 2;
15119 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015120 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 }
15122 }
15123
15124 /* Ensure that the result will have no trailing path separator
15125 * if the argument had none. But keep "/" or "//". */
15126 if (!has_trailing_pathsep)
15127 {
15128 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015129 if (after_pathsep(p, q))
15130 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 }
15132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015133 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134 }
15135# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015136 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137# endif
15138#endif
15139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141
15142#ifdef HAVE_READLINK
15143fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015144 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015146 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147}
15148
15149/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 */
15152 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015154 typval_T *argvars;
15155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156{
Bram Moolenaar33570922005-01-25 22:26:29 +000015157 list_T *l;
15158 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 if (argvars[0].v_type != VAR_LIST)
15161 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015162 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015163 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015164 {
15165 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015166 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015167 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168 while (li != NULL)
15169 {
15170 ni = li->li_prev;
15171 list_append(l, li);
15172 li = ni;
15173 }
15174 rettv->vval.v_list = l;
15175 rettv->v_type = VAR_LIST;
15176 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015177 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179}
15180
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015181#define SP_NOMOVE 0x01 /* don't move cursor */
15182#define SP_REPEAT 0x02 /* repeat to find outer pair */
15183#define SP_RETCOUNT 0x04 /* return matchcount */
15184#define SP_SETPCMARK 0x08 /* set previous context mark */
15185#define SP_START 0x10 /* accept match at start position */
15186#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15187#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015188
Bram Moolenaar33570922005-01-25 22:26:29 +000015189static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015190
15191/*
15192 * Get flags for a search function.
15193 * Possibly sets "p_ws".
15194 * Returns BACKWARD, FORWARD or zero (for an error).
15195 */
15196 static int
15197get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015198 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015199 int *flagsp;
15200{
15201 int dir = FORWARD;
15202 char_u *flags;
15203 char_u nbuf[NUMBUFLEN];
15204 int mask;
15205
15206 if (varp->v_type != VAR_UNKNOWN)
15207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015208 flags = get_tv_string_buf_chk(varp, nbuf);
15209 if (flags == NULL)
15210 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211 while (*flags != NUL)
15212 {
15213 switch (*flags)
15214 {
15215 case 'b': dir = BACKWARD; break;
15216 case 'w': p_ws = TRUE; break;
15217 case 'W': p_ws = FALSE; break;
15218 default: mask = 0;
15219 if (flagsp != NULL)
15220 switch (*flags)
15221 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015222 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015223 case 'e': mask = SP_END; break;
15224 case 'm': mask = SP_RETCOUNT; break;
15225 case 'n': mask = SP_NOMOVE; break;
15226 case 'p': mask = SP_SUBPAT; break;
15227 case 'r': mask = SP_REPEAT; break;
15228 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015229 }
15230 if (mask == 0)
15231 {
15232 EMSG2(_(e_invarg2), flags);
15233 dir = 0;
15234 }
15235 else
15236 *flagsp |= mask;
15237 }
15238 if (dir == 0)
15239 break;
15240 ++flags;
15241 }
15242 }
15243 return dir;
15244}
15245
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015247 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015249 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015250search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015252 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015253 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015255 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 char_u *pat;
15257 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015258 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 int save_p_ws = p_ws;
15260 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015261 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015262 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015263 proftime_T tm;
15264#ifdef FEAT_RELTIME
15265 long time_limit = 0;
15266#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015267 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015268 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015270 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015271 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015272 if (dir == 0)
15273 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015274 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015275 if (flags & SP_START)
15276 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015277 if (flags & SP_END)
15278 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015279
Bram Moolenaar76929292008-01-06 19:07:36 +000015280 /* Optional arguments: line number to stop searching and timeout. */
15281 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015282 {
15283 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15284 if (lnum_stop < 0)
15285 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015286#ifdef FEAT_RELTIME
15287 if (argvars[3].v_type != VAR_UNKNOWN)
15288 {
15289 time_limit = get_tv_number_chk(&argvars[3], NULL);
15290 if (time_limit < 0)
15291 goto theend;
15292 }
15293#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015294 }
15295
Bram Moolenaar76929292008-01-06 19:07:36 +000015296#ifdef FEAT_RELTIME
15297 /* Set the time limit, if there is one. */
15298 profile_setlimit(time_limit, &tm);
15299#endif
15300
Bram Moolenaar231334e2005-07-25 20:46:57 +000015301 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015302 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015303 * Check to make sure only those flags are set.
15304 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15305 * flags cannot be set. Check for that condition also.
15306 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015307 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015308 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015310 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015311 goto theend;
15312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015314 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015315 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015316 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015317 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015319 if (flags & SP_SUBPAT)
15320 retval = subpatnum;
15321 else
15322 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015323 if (flags & SP_SETPCMARK)
15324 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015326 if (match_pos != NULL)
15327 {
15328 /* Store the match cursor position */
15329 match_pos->lnum = pos.lnum;
15330 match_pos->col = pos.col + 1;
15331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 /* "/$" will put the cursor after the end of the line, may need to
15333 * correct that here */
15334 check_cursor();
15335 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015336
15337 /* If 'n' flag is used: restore cursor position. */
15338 if (flags & SP_NOMOVE)
15339 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015340 else
15341 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015342theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015344
15345 return retval;
15346}
15347
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015348#ifdef FEAT_FLOAT
15349/*
15350 * "round({float})" function
15351 */
15352 static void
15353f_round(argvars, rettv)
15354 typval_T *argvars;
15355 typval_T *rettv;
15356{
15357 float_T f;
15358
15359 rettv->v_type = VAR_FLOAT;
15360 if (get_float_arg(argvars, &f) == OK)
15361 /* round() is not in C90, use ceil() or floor() instead. */
15362 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15363 else
15364 rettv->vval.v_float = 0.0;
15365}
15366#endif
15367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015368/*
15369 * "search()" function
15370 */
15371 static void
15372f_search(argvars, rettv)
15373 typval_T *argvars;
15374 typval_T *rettv;
15375{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015376 int flags = 0;
15377
15378 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379}
15380
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015382 * "searchdecl()" function
15383 */
15384 static void
15385f_searchdecl(argvars, rettv)
15386 typval_T *argvars;
15387 typval_T *rettv;
15388{
15389 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015390 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015391 int error = FALSE;
15392 char_u *name;
15393
15394 rettv->vval.v_number = 1; /* default: FAIL */
15395
15396 name = get_tv_string_chk(&argvars[0]);
15397 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015398 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015399 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015400 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15401 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15402 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015403 if (!error && name != NULL)
15404 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015405 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015406}
15407
15408/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015409 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015411 static int
15412searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015413 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015414 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415{
15416 char_u *spat, *mpat, *epat;
15417 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419 int dir;
15420 int flags = 0;
15421 char_u nbuf1[NUMBUFLEN];
15422 char_u nbuf2[NUMBUFLEN];
15423 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015424 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015425 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015426 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015429 spat = get_tv_string_chk(&argvars[0]);
15430 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15431 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15432 if (spat == NULL || mpat == NULL || epat == NULL)
15433 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 /* Handle the optional fourth argument: flags */
15436 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015437 if (dir == 0)
15438 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015439
15440 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015441 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15442 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015443 if ((flags & (SP_END | SP_SUBPAT)) != 0
15444 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015445 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015446 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015447 goto theend;
15448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015450 /* Using 'r' implies 'W', otherwise it doesn't work. */
15451 if (flags & SP_REPEAT)
15452 p_ws = FALSE;
15453
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015454 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015455 if (argvars[3].v_type == VAR_UNKNOWN
15456 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 skip = (char_u *)"";
15458 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015460 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015461 if (argvars[5].v_type != VAR_UNKNOWN)
15462 {
15463 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15464 if (lnum_stop < 0)
15465 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015466#ifdef FEAT_RELTIME
15467 if (argvars[6].v_type != VAR_UNKNOWN)
15468 {
15469 time_limit = get_tv_number_chk(&argvars[6], NULL);
15470 if (time_limit < 0)
15471 goto theend;
15472 }
15473#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015474 }
15475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015476 if (skip == NULL)
15477 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015479 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015480 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015481
15482theend:
15483 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015484
15485 return retval;
15486}
15487
15488/*
15489 * "searchpair()" function
15490 */
15491 static void
15492f_searchpair(argvars, rettv)
15493 typval_T *argvars;
15494 typval_T *rettv;
15495{
15496 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15497}
15498
15499/*
15500 * "searchpairpos()" function
15501 */
15502 static void
15503f_searchpairpos(argvars, rettv)
15504 typval_T *argvars;
15505 typval_T *rettv;
15506{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015507 pos_T match_pos;
15508 int lnum = 0;
15509 int col = 0;
15510
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015511 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015512 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015513
15514 if (searchpair_cmn(argvars, &match_pos) > 0)
15515 {
15516 lnum = match_pos.lnum;
15517 col = match_pos.col;
15518 }
15519
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015520 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15521 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015522}
15523
15524/*
15525 * Search for a start/middle/end thing.
15526 * Used by searchpair(), see its documentation for the details.
15527 * Returns 0 or -1 for no match,
15528 */
15529 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015530do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15531 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015532 char_u *spat; /* start pattern */
15533 char_u *mpat; /* middle pattern */
15534 char_u *epat; /* end pattern */
15535 int dir; /* BACKWARD or FORWARD */
15536 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015537 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015538 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015539 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015540 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015541{
15542 char_u *save_cpo;
15543 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15544 long retval = 0;
15545 pos_T pos;
15546 pos_T firstpos;
15547 pos_T foundpos;
15548 pos_T save_cursor;
15549 pos_T save_pos;
15550 int n;
15551 int r;
15552 int nest = 1;
15553 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015554 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015555 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015556
15557 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15558 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015559 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015560
Bram Moolenaar76929292008-01-06 19:07:36 +000015561#ifdef FEAT_RELTIME
15562 /* Set the time limit, if there is one. */
15563 profile_setlimit(time_limit, &tm);
15564#endif
15565
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015566 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15567 * start/middle/end (pat3, for the top pair). */
15568 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15569 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15570 if (pat2 == NULL || pat3 == NULL)
15571 goto theend;
15572 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15573 if (*mpat == NUL)
15574 STRCPY(pat3, pat2);
15575 else
15576 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15577 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015578 if (flags & SP_START)
15579 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015580
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 save_cursor = curwin->w_cursor;
15582 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015583 clearpos(&firstpos);
15584 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 pat = pat3;
15586 for (;;)
15587 {
15588 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015589 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15591 /* didn't find it or found the first match again: FAIL */
15592 break;
15593
15594 if (firstpos.lnum == 0)
15595 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015596 if (equalpos(pos, foundpos))
15597 {
15598 /* Found the same position again. Can happen with a pattern that
15599 * has "\zs" at the end and searching backwards. Advance one
15600 * character and try again. */
15601 if (dir == BACKWARD)
15602 decl(&pos);
15603 else
15604 incl(&pos);
15605 }
15606 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015608 /* clear the start flag to avoid getting stuck here */
15609 options &= ~SEARCH_START;
15610
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 /* If the skip pattern matches, ignore this match. */
15612 if (*skip != NUL)
15613 {
15614 save_pos = curwin->w_cursor;
15615 curwin->w_cursor = pos;
15616 r = eval_to_bool(skip, &err, NULL, FALSE);
15617 curwin->w_cursor = save_pos;
15618 if (err)
15619 {
15620 /* Evaluating {skip} caused an error, break here. */
15621 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015622 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 break;
15624 }
15625 if (r)
15626 continue;
15627 }
15628
15629 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15630 {
15631 /* Found end when searching backwards or start when searching
15632 * forward: nested pair. */
15633 ++nest;
15634 pat = pat2; /* nested, don't search for middle */
15635 }
15636 else
15637 {
15638 /* Found end when searching forward or start when searching
15639 * backward: end of (nested) pair; or found middle in outer pair. */
15640 if (--nest == 1)
15641 pat = pat3; /* outer level, search for middle */
15642 }
15643
15644 if (nest == 0)
15645 {
15646 /* Found the match: return matchcount or line number. */
15647 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015648 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015650 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015651 if (flags & SP_SETPCMARK)
15652 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 curwin->w_cursor = pos;
15654 if (!(flags & SP_REPEAT))
15655 break;
15656 nest = 1; /* search for next unmatched */
15657 }
15658 }
15659
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015660 if (match_pos != NULL)
15661 {
15662 /* Store the match cursor position */
15663 match_pos->lnum = curwin->w_cursor.lnum;
15664 match_pos->col = curwin->w_cursor.col + 1;
15665 }
15666
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015668 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 curwin->w_cursor = save_cursor;
15670
15671theend:
15672 vim_free(pat2);
15673 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015674 if (p_cpo == empty_option)
15675 p_cpo = save_cpo;
15676 else
15677 /* Darn, evaluating the {skip} expression changed the value. */
15678 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015679
15680 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681}
15682
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015683/*
15684 * "searchpos()" function
15685 */
15686 static void
15687f_searchpos(argvars, rettv)
15688 typval_T *argvars;
15689 typval_T *rettv;
15690{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015691 pos_T match_pos;
15692 int lnum = 0;
15693 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015694 int n;
15695 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015696
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015697 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015698 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015699
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015700 n = search_cmn(argvars, &match_pos, &flags);
15701 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015702 {
15703 lnum = match_pos.lnum;
15704 col = match_pos.col;
15705 }
15706
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015707 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15708 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015709 if (flags & SP_SUBPAT)
15710 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015711}
15712
15713
Bram Moolenaar0d660222005-01-07 21:51:51 +000015714 static void
15715f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015716 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015719#ifdef FEAT_CLIENTSERVER
15720 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015721 char_u *server = get_tv_string_chk(&argvars[0]);
15722 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015725 if (server == NULL || reply == NULL)
15726 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015727 if (check_restricted() || check_secure())
15728 return;
15729# ifdef FEAT_X11
15730 if (check_connection() == FAIL)
15731 return;
15732# endif
15733
15734 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015736 EMSG(_("E258: Unable to send to client"));
15737 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 rettv->vval.v_number = 0;
15740#else
15741 rettv->vval.v_number = -1;
15742#endif
15743}
15744
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745 static void
15746f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015748 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749{
15750 char_u *r = NULL;
15751
15752#ifdef FEAT_CLIENTSERVER
15753# ifdef WIN32
15754 r = serverGetVimNames();
15755# else
15756 make_connection();
15757 if (X_DISPLAY != NULL)
15758 r = serverGetVimNames(X_DISPLAY);
15759# endif
15760#endif
15761 rettv->v_type = VAR_STRING;
15762 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763}
15764
15765/*
15766 * "setbufvar()" function
15767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015769f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015770 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015771 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772{
15773 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015776 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 char_u nbuf[NUMBUFLEN];
15778
15779 if (check_restricted() || check_secure())
15780 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015781 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15782 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015783 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 varp = &argvars[2];
15785
15786 if (buf != NULL && varname != NULL && varp != NULL)
15787 {
15788 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790
15791 if (*varname == '&')
15792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015793 long numval;
15794 char_u *strval;
15795 int error = FALSE;
15796
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015798 numval = get_tv_number_chk(varp, &error);
15799 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800 if (!error && strval != NULL)
15801 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 }
15803 else
15804 {
15805 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15806 if (bufvarname != NULL)
15807 {
15808 STRCPY(bufvarname, "b:");
15809 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015810 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811 vim_free(bufvarname);
15812 }
15813 }
15814
15815 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818}
15819
15820/*
15821 * "setcmdpos()" function
15822 */
15823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015824f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 typval_T *argvars;
15826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015828 int pos = (int)get_tv_number(&argvars[0]) - 1;
15829
15830 if (pos >= 0)
15831 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832}
15833
15834/*
15835 * "setline()" function
15836 */
15837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015838f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015839 typval_T *argvars;
15840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841{
15842 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015843 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015844 list_T *l = NULL;
15845 listitem_T *li = NULL;
15846 long added = 0;
15847 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015849 lnum = get_tv_lnum(&argvars[0]);
15850 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015852 l = argvars[1].vval.v_list;
15853 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015855 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015856 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015857
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015858 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015859 for (;;)
15860 {
15861 if (l != NULL)
15862 {
15863 /* list argument, get next string */
15864 if (li == NULL)
15865 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015866 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015867 li = li->li_next;
15868 }
15869
15870 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015871 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015872 break;
15873 if (lnum <= curbuf->b_ml.ml_line_count)
15874 {
15875 /* existing line, replace it */
15876 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15877 {
15878 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015879 if (lnum == curwin->w_cursor.lnum)
15880 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015881 rettv->vval.v_number = 0; /* OK */
15882 }
15883 }
15884 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15885 {
15886 /* lnum is one past the last line, append the line */
15887 ++added;
15888 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15889 rettv->vval.v_number = 0; /* OK */
15890 }
15891
15892 if (l == NULL) /* only one string argument */
15893 break;
15894 ++lnum;
15895 }
15896
15897 if (added > 0)
15898 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899}
15900
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015901static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15902
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015904 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015905 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015906 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015907set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015908 win_T *wp UNUSED;
15909 typval_T *list_arg UNUSED;
15910 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015911 typval_T *rettv;
15912{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015913#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015914 char_u *act;
15915 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015916#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015917
Bram Moolenaar2641f772005-03-25 21:58:17 +000015918 rettv->vval.v_number = -1;
15919
15920#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015921 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015922 EMSG(_(e_listreq));
15923 else
15924 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015925 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015926
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015927 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015928 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015929 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015930 if (act == NULL)
15931 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015932 if (*act == 'a' || *act == 'r')
15933 action = *act;
15934 }
15935
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015936 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015937 rettv->vval.v_number = 0;
15938 }
15939#endif
15940}
15941
15942/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015943 * "setloclist()" function
15944 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015945 static void
15946f_setloclist(argvars, rettv)
15947 typval_T *argvars;
15948 typval_T *rettv;
15949{
15950 win_T *win;
15951
15952 rettv->vval.v_number = -1;
15953
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015954 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015955 if (win != NULL)
15956 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15957}
15958
15959/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015960 * "setmatches()" function
15961 */
15962 static void
15963f_setmatches(argvars, rettv)
15964 typval_T *argvars;
15965 typval_T *rettv;
15966{
15967#ifdef FEAT_SEARCH_EXTRA
15968 list_T *l;
15969 listitem_T *li;
15970 dict_T *d;
15971
15972 rettv->vval.v_number = -1;
15973 if (argvars[0].v_type != VAR_LIST)
15974 {
15975 EMSG(_(e_listreq));
15976 return;
15977 }
15978 if ((l = argvars[0].vval.v_list) != NULL)
15979 {
15980
15981 /* To some extent make sure that we are dealing with a list from
15982 * "getmatches()". */
15983 li = l->lv_first;
15984 while (li != NULL)
15985 {
15986 if (li->li_tv.v_type != VAR_DICT
15987 || (d = li->li_tv.vval.v_dict) == NULL)
15988 {
15989 EMSG(_(e_invarg));
15990 return;
15991 }
15992 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15993 && dict_find(d, (char_u *)"pattern", -1) != NULL
15994 && dict_find(d, (char_u *)"priority", -1) != NULL
15995 && dict_find(d, (char_u *)"id", -1) != NULL))
15996 {
15997 EMSG(_(e_invarg));
15998 return;
15999 }
16000 li = li->li_next;
16001 }
16002
16003 clear_matches(curwin);
16004 li = l->lv_first;
16005 while (li != NULL)
16006 {
16007 d = li->li_tv.vval.v_dict;
16008 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16009 get_dict_string(d, (char_u *)"pattern", FALSE),
16010 (int)get_dict_number(d, (char_u *)"priority"),
16011 (int)get_dict_number(d, (char_u *)"id"));
16012 li = li->li_next;
16013 }
16014 rettv->vval.v_number = 0;
16015 }
16016#endif
16017}
16018
16019/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016020 * "setpos()" function
16021 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016022 static void
16023f_setpos(argvars, rettv)
16024 typval_T *argvars;
16025 typval_T *rettv;
16026{
16027 pos_T pos;
16028 int fnum;
16029 char_u *name;
16030
Bram Moolenaar08250432008-02-13 11:42:46 +000016031 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016032 name = get_tv_string_chk(argvars);
16033 if (name != NULL)
16034 {
16035 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16036 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016037 if (--pos.col < 0)
16038 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016039 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016040 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016041 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016042 if (fnum == curbuf->b_fnum)
16043 {
16044 curwin->w_cursor = pos;
16045 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016046 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016047 }
16048 else
16049 EMSG(_(e_invarg));
16050 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016051 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16052 {
16053 /* set mark */
16054 if (setmark_pos(name[1], &pos, fnum) == OK)
16055 rettv->vval.v_number = 0;
16056 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016057 else
16058 EMSG(_(e_invarg));
16059 }
16060 }
16061}
16062
16063/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016064 * "setqflist()" function
16065 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016066 static void
16067f_setqflist(argvars, rettv)
16068 typval_T *argvars;
16069 typval_T *rettv;
16070{
16071 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16072}
16073
16074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 * "setreg()" function
16076 */
16077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016078f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016079 typval_T *argvars;
16080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081{
16082 int regname;
16083 char_u *strregname;
16084 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016085 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 int append;
16087 char_u yank_type;
16088 long block_len;
16089
16090 block_len = -1;
16091 yank_type = MAUTO;
16092 append = FALSE;
16093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016094 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016095 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016097 if (strregname == NULL)
16098 return; /* type error; errmsg already given */
16099 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 if (regname == 0 || regname == '@')
16101 regname = '"';
16102 else if (regname == '=')
16103 return;
16104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016105 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016107 stropt = get_tv_string_chk(&argvars[2]);
16108 if (stropt == NULL)
16109 return; /* type error */
16110 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 switch (*stropt)
16112 {
16113 case 'a': case 'A': /* append */
16114 append = TRUE;
16115 break;
16116 case 'v': case 'c': /* character-wise selection */
16117 yank_type = MCHAR;
16118 break;
16119 case 'V': case 'l': /* line-wise selection */
16120 yank_type = MLINE;
16121 break;
16122#ifdef FEAT_VISUAL
16123 case 'b': case Ctrl_V: /* block-wise selection */
16124 yank_type = MBLOCK;
16125 if (VIM_ISDIGIT(stropt[1]))
16126 {
16127 ++stropt;
16128 block_len = getdigits(&stropt) - 1;
16129 --stropt;
16130 }
16131 break;
16132#endif
16133 }
16134 }
16135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016136 strval = get_tv_string_chk(&argvars[1]);
16137 if (strval != NULL)
16138 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016140 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141}
16142
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016143/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016144 * "settabvar()" function
16145 */
16146 static void
16147f_settabvar(argvars, rettv)
16148 typval_T *argvars;
16149 typval_T *rettv;
16150{
16151 tabpage_T *save_curtab;
16152 char_u *varname, *tabvarname;
16153 typval_T *varp;
16154 tabpage_T *tp;
16155
16156 rettv->vval.v_number = 0;
16157
16158 if (check_restricted() || check_secure())
16159 return;
16160
16161 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16162 varname = get_tv_string_chk(&argvars[1]);
16163 varp = &argvars[2];
16164
16165 if (tp != NULL && varname != NULL && varp != NULL)
16166 {
16167 save_curtab = curtab;
16168 goto_tabpage_tp(tp);
16169
16170 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16171 if (tabvarname != NULL)
16172 {
16173 STRCPY(tabvarname, "t:");
16174 STRCPY(tabvarname + 2, varname);
16175 set_var(tabvarname, varp, TRUE);
16176 vim_free(tabvarname);
16177 }
16178
16179 /* Restore current tabpage */
16180 if (valid_tabpage(save_curtab))
16181 goto_tabpage_tp(save_curtab);
16182 }
16183}
16184
16185/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016186 * "settabwinvar()" function
16187 */
16188 static void
16189f_settabwinvar(argvars, rettv)
16190 typval_T *argvars;
16191 typval_T *rettv;
16192{
16193 setwinvar(argvars, rettv, 1);
16194}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195
16196/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016197 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016200f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016201 typval_T *argvars;
16202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016204 setwinvar(argvars, rettv, 0);
16205}
16206
16207/*
16208 * "setwinvar()" and "settabwinvar()" functions
16209 */
16210 static void
16211setwinvar(argvars, rettv, off)
16212 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016213 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016214 int off;
16215{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 win_T *win;
16217#ifdef FEAT_WINDOWS
16218 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016219 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220#endif
16221 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016224 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225
16226 if (check_restricted() || check_secure())
16227 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016228
16229#ifdef FEAT_WINDOWS
16230 if (off == 1)
16231 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16232 else
16233 tp = curtab;
16234#endif
16235 win = find_win_by_nr(&argvars[off], tp);
16236 varname = get_tv_string_chk(&argvars[off + 1]);
16237 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238
16239 if (win != NULL && varname != NULL && varp != NULL)
16240 {
16241#ifdef FEAT_WINDOWS
16242 /* set curwin to be our win, temporarily */
16243 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016244 save_curtab = curtab;
16245 goto_tabpage_tp(tp);
16246 if (!win_valid(win))
16247 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 curwin = win;
16249 curbuf = curwin->w_buffer;
16250#endif
16251
16252 if (*varname == '&')
16253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016254 long numval;
16255 char_u *strval;
16256 int error = FALSE;
16257
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 numval = get_tv_number_chk(varp, &error);
16260 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 if (!error && strval != NULL)
16262 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 }
16264 else
16265 {
16266 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16267 if (winvarname != NULL)
16268 {
16269 STRCPY(winvarname, "w:");
16270 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016271 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 vim_free(winvarname);
16273 }
16274 }
16275
16276#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016277 /* Restore current tabpage and window, if still valid (autocomands can
16278 * make them invalid). */
16279 if (valid_tabpage(save_curtab))
16280 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 if (win_valid(save_curwin))
16282 {
16283 curwin = save_curwin;
16284 curbuf = curwin->w_buffer;
16285 }
16286#endif
16287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288}
16289
16290/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016291 * "shellescape({string})" function
16292 */
16293 static void
16294f_shellescape(argvars, rettv)
16295 typval_T *argvars;
16296 typval_T *rettv;
16297{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016298 rettv->vval.v_string = vim_strsave_shellescape(
16299 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016300 rettv->v_type = VAR_STRING;
16301}
16302
16303/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016304 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 */
16306 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 typval_T *argvars;
16309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016311 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313 p = get_tv_string(&argvars[0]);
16314 rettv->vval.v_string = vim_strsave(p);
16315 simplify_filename(rettv->vval.v_string); /* simplify in place */
16316 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317}
16318
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016319#ifdef FEAT_FLOAT
16320/*
16321 * "sin()" function
16322 */
16323 static void
16324f_sin(argvars, rettv)
16325 typval_T *argvars;
16326 typval_T *rettv;
16327{
16328 float_T f;
16329
16330 rettv->v_type = VAR_FLOAT;
16331 if (get_float_arg(argvars, &f) == OK)
16332 rettv->vval.v_float = sin(f);
16333 else
16334 rettv->vval.v_float = 0.0;
16335}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016336
16337/*
16338 * "sinh()" function
16339 */
16340 static void
16341f_sinh(argvars, rettv)
16342 typval_T *argvars;
16343 typval_T *rettv;
16344{
16345 float_T f;
16346
16347 rettv->v_type = VAR_FLOAT;
16348 if (get_float_arg(argvars, &f) == OK)
16349 rettv->vval.v_float = sinh(f);
16350 else
16351 rettv->vval.v_float = 0.0;
16352}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016353#endif
16354
Bram Moolenaar0d660222005-01-07 21:51:51 +000016355static int
16356#ifdef __BORLANDC__
16357 _RTLENTRYF
16358#endif
16359 item_compare __ARGS((const void *s1, const void *s2));
16360static int
16361#ifdef __BORLANDC__
16362 _RTLENTRYF
16363#endif
16364 item_compare2 __ARGS((const void *s1, const void *s2));
16365
16366static int item_compare_ic;
16367static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369#define ITEM_COMPARE_FAIL 999
16370
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016374 static int
16375#ifdef __BORLANDC__
16376_RTLENTRYF
16377#endif
16378item_compare(s1, s2)
16379 const void *s1;
16380 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382 char_u *p1, *p2;
16383 char_u *tofree1, *tofree2;
16384 int res;
16385 char_u numbuf1[NUMBUFLEN];
16386 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016388 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16389 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016390 if (p1 == NULL)
16391 p1 = (char_u *)"";
16392 if (p2 == NULL)
16393 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016394 if (item_compare_ic)
16395 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397 res = STRCMP(p1, p2);
16398 vim_free(tofree1);
16399 vim_free(tofree2);
16400 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401}
16402
16403 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404#ifdef __BORLANDC__
16405_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016407item_compare2(s1, s2)
16408 const void *s1;
16409 const void *s2;
16410{
16411 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016412 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016413 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016416 /* shortcut after failure in previous call; compare all items equal */
16417 if (item_compare_func_err)
16418 return 0;
16419
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016420 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16421 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016422 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16423 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424
16425 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016426 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016427 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428 clear_tv(&argv[0]);
16429 clear_tv(&argv[1]);
16430
16431 if (res == FAIL)
16432 res = ITEM_COMPARE_FAIL;
16433 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016434 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16435 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016436 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437 clear_tv(&rettv);
16438 return res;
16439}
16440
16441/*
16442 * "sort({list})" function
16443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016446 typval_T *argvars;
16447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448{
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 list_T *l;
16450 listitem_T *li;
16451 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016452 long len;
16453 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455 if (argvars[0].v_type != VAR_LIST)
16456 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 else
16458 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016460 if (l == NULL || tv_check_lock(l->lv_lock,
16461 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462 return;
16463 rettv->vval.v_list = l;
16464 rettv->v_type = VAR_LIST;
16465 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466
Bram Moolenaar0d660222005-01-07 21:51:51 +000016467 len = list_len(l);
16468 if (len <= 1)
16469 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470
Bram Moolenaar0d660222005-01-07 21:51:51 +000016471 item_compare_ic = FALSE;
16472 item_compare_func = NULL;
16473 if (argvars[1].v_type != VAR_UNKNOWN)
16474 {
16475 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016476 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477 else
16478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016479 int error = FALSE;
16480
16481 i = get_tv_number_chk(&argvars[1], &error);
16482 if (error)
16483 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484 if (i == 1)
16485 item_compare_ic = TRUE;
16486 else
16487 item_compare_func = get_tv_string(&argvars[1]);
16488 }
16489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490
Bram Moolenaar0d660222005-01-07 21:51:51 +000016491 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016492 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016493 if (ptrs == NULL)
16494 return;
16495 i = 0;
16496 for (li = l->lv_first; li != NULL; li = li->li_next)
16497 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016499 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500 /* test the compare function */
16501 if (item_compare_func != NULL
16502 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16503 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016504 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016506 {
16507 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016508 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016509 item_compare_func == NULL ? item_compare : item_compare2);
16510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016511 if (!item_compare_func_err)
16512 {
16513 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016514 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016515 l->lv_len = 0;
16516 for (i = 0; i < len; ++i)
16517 list_append(l, ptrs[i]);
16518 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016519 }
16520
16521 vim_free(ptrs);
16522 }
16523}
16524
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016525/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016526 * "soundfold({word})" function
16527 */
16528 static void
16529f_soundfold(argvars, rettv)
16530 typval_T *argvars;
16531 typval_T *rettv;
16532{
16533 char_u *s;
16534
16535 rettv->v_type = VAR_STRING;
16536 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016537#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016538 rettv->vval.v_string = eval_soundfold(s);
16539#else
16540 rettv->vval.v_string = vim_strsave(s);
16541#endif
16542}
16543
16544/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016545 * "spellbadword()" function
16546 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016547 static void
16548f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016549 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016550 typval_T *rettv;
16551{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016552 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016553 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016554 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016555
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016556 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016557 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016558
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016559#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016560 if (argvars[0].v_type == VAR_UNKNOWN)
16561 {
16562 /* Find the start and length of the badly spelled word. */
16563 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16564 if (len != 0)
16565 word = ml_get_cursor();
16566 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016567 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016568 {
16569 char_u *str = get_tv_string_chk(&argvars[0]);
16570 int capcol = -1;
16571
16572 if (str != NULL)
16573 {
16574 /* Check the argument for spelling. */
16575 while (*str != NUL)
16576 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016577 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016578 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016579 {
16580 word = str;
16581 break;
16582 }
16583 str += len;
16584 }
16585 }
16586 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016587#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016588
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016589 list_append_string(rettv->vval.v_list, word, len);
16590 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016591 attr == HLF_SPB ? "bad" :
16592 attr == HLF_SPR ? "rare" :
16593 attr == HLF_SPL ? "local" :
16594 attr == HLF_SPC ? "caps" :
16595 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016596}
16597
16598/*
16599 * "spellsuggest()" function
16600 */
16601 static void
16602f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016603 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016604 typval_T *rettv;
16605{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016606#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016607 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016608 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016609 int maxcount;
16610 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016611 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016612 listitem_T *li;
16613 int need_capital = FALSE;
16614#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016616 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016617 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016618
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016619#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016620 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016621 {
16622 str = get_tv_string(&argvars[0]);
16623 if (argvars[1].v_type != VAR_UNKNOWN)
16624 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016625 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016626 if (maxcount <= 0)
16627 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016628 if (argvars[2].v_type != VAR_UNKNOWN)
16629 {
16630 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16631 if (typeerr)
16632 return;
16633 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016634 }
16635 else
16636 maxcount = 25;
16637
Bram Moolenaar4770d092006-01-12 23:22:24 +000016638 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016639
16640 for (i = 0; i < ga.ga_len; ++i)
16641 {
16642 str = ((char_u **)ga.ga_data)[i];
16643
16644 li = listitem_alloc();
16645 if (li == NULL)
16646 vim_free(str);
16647 else
16648 {
16649 li->li_tv.v_type = VAR_STRING;
16650 li->li_tv.v_lock = 0;
16651 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016652 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016653 }
16654 }
16655 ga_clear(&ga);
16656 }
16657#endif
16658}
16659
Bram Moolenaar0d660222005-01-07 21:51:51 +000016660 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016661f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 typval_T *argvars;
16663 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016664{
16665 char_u *str;
16666 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016667 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016668 regmatch_T regmatch;
16669 char_u patbuf[NUMBUFLEN];
16670 char_u *save_cpo;
16671 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016672 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016673 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016674 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016675
16676 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16677 save_cpo = p_cpo;
16678 p_cpo = (char_u *)"";
16679
16680 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016681 if (argvars[1].v_type != VAR_UNKNOWN)
16682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016683 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16684 if (pat == NULL)
16685 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016686 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016687 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016688 }
16689 if (pat == NULL || *pat == NUL)
16690 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016692 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016694 if (typeerr)
16695 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696
Bram Moolenaar0d660222005-01-07 21:51:51 +000016697 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16698 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016700 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016701 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016702 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016703 if (*str == NUL)
16704 match = FALSE; /* empty item at the end */
16705 else
16706 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016707 if (match)
16708 end = regmatch.startp[0];
16709 else
16710 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016711 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16712 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016713 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016714 if (list_append_string(rettv->vval.v_list, str,
16715 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016716 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016717 }
16718 if (!match)
16719 break;
16720 /* Advance to just after the match. */
16721 if (regmatch.endp[0] > str)
16722 col = 0;
16723 else
16724 {
16725 /* Don't get stuck at the same match. */
16726#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016727 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016728#else
16729 col = 1;
16730#endif
16731 }
16732 str = regmatch.endp[0];
16733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734
Bram Moolenaar0d660222005-01-07 21:51:51 +000016735 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737
Bram Moolenaar0d660222005-01-07 21:51:51 +000016738 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739}
16740
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016741#ifdef FEAT_FLOAT
16742/*
16743 * "sqrt()" function
16744 */
16745 static void
16746f_sqrt(argvars, rettv)
16747 typval_T *argvars;
16748 typval_T *rettv;
16749{
16750 float_T f;
16751
16752 rettv->v_type = VAR_FLOAT;
16753 if (get_float_arg(argvars, &f) == OK)
16754 rettv->vval.v_float = sqrt(f);
16755 else
16756 rettv->vval.v_float = 0.0;
16757}
16758
16759/*
16760 * "str2float()" function
16761 */
16762 static void
16763f_str2float(argvars, rettv)
16764 typval_T *argvars;
16765 typval_T *rettv;
16766{
16767 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16768
16769 if (*p == '+')
16770 p = skipwhite(p + 1);
16771 (void)string2float(p, &rettv->vval.v_float);
16772 rettv->v_type = VAR_FLOAT;
16773}
16774#endif
16775
Bram Moolenaar2c932302006-03-18 21:42:09 +000016776/*
16777 * "str2nr()" function
16778 */
16779 static void
16780f_str2nr(argvars, rettv)
16781 typval_T *argvars;
16782 typval_T *rettv;
16783{
16784 int base = 10;
16785 char_u *p;
16786 long n;
16787
16788 if (argvars[1].v_type != VAR_UNKNOWN)
16789 {
16790 base = get_tv_number(&argvars[1]);
16791 if (base != 8 && base != 10 && base != 16)
16792 {
16793 EMSG(_(e_invarg));
16794 return;
16795 }
16796 }
16797
16798 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016799 if (*p == '+')
16800 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016801 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16802 rettv->vval.v_number = n;
16803}
16804
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805#ifdef HAVE_STRFTIME
16806/*
16807 * "strftime({format}[, {time}])" function
16808 */
16809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016810f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016811 typval_T *argvars;
16812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813{
16814 char_u result_buf[256];
16815 struct tm *curtime;
16816 time_t seconds;
16817 char_u *p;
16818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016819 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016821 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016822 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823 seconds = time(NULL);
16824 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016825 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 curtime = localtime(&seconds);
16827 /* MSVC returns NULL for an invalid value of seconds. */
16828 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016829 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 else
16831 {
16832# ifdef FEAT_MBYTE
16833 vimconv_T conv;
16834 char_u *enc;
16835
16836 conv.vc_type = CONV_NONE;
16837 enc = enc_locale();
16838 convert_setup(&conv, p_enc, enc);
16839 if (conv.vc_type != CONV_NONE)
16840 p = string_convert(&conv, p, NULL);
16841# endif
16842 if (p != NULL)
16843 (void)strftime((char *)result_buf, sizeof(result_buf),
16844 (char *)p, curtime);
16845 else
16846 result_buf[0] = NUL;
16847
16848# ifdef FEAT_MBYTE
16849 if (conv.vc_type != CONV_NONE)
16850 vim_free(p);
16851 convert_setup(&conv, enc, p_enc);
16852 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016853 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 else
16855# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016856 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857
16858# ifdef FEAT_MBYTE
16859 /* Release conversion descriptors */
16860 convert_setup(&conv, NULL, NULL);
16861 vim_free(enc);
16862# endif
16863 }
16864}
16865#endif
16866
16867/*
16868 * "stridx()" function
16869 */
16870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016871f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016872 typval_T *argvars;
16873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874{
16875 char_u buf[NUMBUFLEN];
16876 char_u *needle;
16877 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016878 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016880 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016882 needle = get_tv_string_chk(&argvars[1]);
16883 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016884 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016885 if (needle == NULL || haystack == NULL)
16886 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887
Bram Moolenaar33570922005-01-25 22:26:29 +000016888 if (argvars[2].v_type != VAR_UNKNOWN)
16889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016890 int error = FALSE;
16891
16892 start_idx = get_tv_number_chk(&argvars[2], &error);
16893 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016894 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016895 if (start_idx >= 0)
16896 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 }
16898
16899 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16900 if (pos != NULL)
16901 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902}
16903
16904/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016905 * "string()" function
16906 */
16907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016908f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016909 typval_T *argvars;
16910 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016911{
16912 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016913 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016915 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016916 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016917 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016918 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016919 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920}
16921
16922/*
16923 * "strlen()" function
16924 */
16925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016926f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 typval_T *argvars;
16928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016930 rettv->vval.v_number = (varnumber_T)(STRLEN(
16931 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932}
16933
16934/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016935 * "strchars()" function
16936 */
16937 static void
16938f_strchars(argvars, rettv)
16939 typval_T *argvars;
16940 typval_T *rettv;
16941{
16942 char_u *s = get_tv_string(&argvars[0]);
16943#ifdef FEAT_MBYTE
16944 varnumber_T len = 0;
16945
16946 while (*s != NUL)
16947 {
16948 mb_cptr2char_adv(&s);
16949 ++len;
16950 }
16951 rettv->vval.v_number = len;
16952#else
16953 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16954#endif
16955}
16956
16957/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016958 * "strdisplaywidth()" function
16959 */
16960 static void
16961f_strdisplaywidth(argvars, rettv)
16962 typval_T *argvars;
16963 typval_T *rettv;
16964{
16965 char_u *s = get_tv_string(&argvars[0]);
16966 int col = 0;
16967
16968 if (argvars[1].v_type != VAR_UNKNOWN)
16969 col = get_tv_number(&argvars[1]);
16970
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016971 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016972}
16973
16974/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016975 * "strwidth()" function
16976 */
16977 static void
16978f_strwidth(argvars, rettv)
16979 typval_T *argvars;
16980 typval_T *rettv;
16981{
16982 char_u *s = get_tv_string(&argvars[0]);
16983
16984 rettv->vval.v_number = (varnumber_T)(
16985#ifdef FEAT_MBYTE
16986 mb_string2cells(s, -1)
16987#else
16988 STRLEN(s)
16989#endif
16990 );
16991}
16992
16993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 * "strpart()" function
16995 */
16996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016997f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016998 typval_T *argvars;
16999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000{
17001 char_u *p;
17002 int n;
17003 int len;
17004 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017007 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 slen = (int)STRLEN(p);
17009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017010 n = get_tv_number_chk(&argvars[1], &error);
17011 if (error)
17012 len = 0;
17013 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017014 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 else
17016 len = slen - n; /* default len: all bytes that are available. */
17017
17018 /*
17019 * Only return the overlap between the specified part and the actual
17020 * string.
17021 */
17022 if (n < 0)
17023 {
17024 len += n;
17025 n = 0;
17026 }
17027 else if (n > slen)
17028 n = slen;
17029 if (len < 0)
17030 len = 0;
17031 else if (n + len > slen)
17032 len = slen - n;
17033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 rettv->v_type = VAR_STRING;
17035 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036}
17037
17038/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039 * "strridx()" function
17040 */
17041 static void
17042f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 typval_T *argvars;
17044 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017045{
17046 char_u buf[NUMBUFLEN];
17047 char_u *needle;
17048 char_u *haystack;
17049 char_u *rest;
17050 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017051 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 needle = get_tv_string_chk(&argvars[1]);
17054 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017055
17056 rettv->vval.v_number = -1;
17057 if (needle == NULL || haystack == NULL)
17058 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017059
17060 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017061 if (argvars[2].v_type != VAR_UNKNOWN)
17062 {
17063 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017064 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017065 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017066 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017067 }
17068 else
17069 end_idx = haystack_len;
17070
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017072 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017074 lastmatch = haystack + end_idx;
17075 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017077 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078 for (rest = haystack; *rest != '\0'; ++rest)
17079 {
17080 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017081 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082 break;
17083 lastmatch = rest;
17084 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017085 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086
17087 if (lastmatch == NULL)
17088 rettv->vval.v_number = -1;
17089 else
17090 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17091}
17092
17093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 * "strtrans()" function
17095 */
17096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017097f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017098 typval_T *argvars;
17099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017101 rettv->v_type = VAR_STRING;
17102 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103}
17104
17105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106 * "submatch()" function
17107 */
17108 static void
17109f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017110 typval_T *argvars;
17111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112{
17113 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017114 rettv->vval.v_string =
17115 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116}
17117
17118/*
17119 * "substitute()" function
17120 */
17121 static void
17122f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017123 typval_T *argvars;
17124 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125{
17126 char_u patbuf[NUMBUFLEN];
17127 char_u subbuf[NUMBUFLEN];
17128 char_u flagsbuf[NUMBUFLEN];
17129
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017130 char_u *str = get_tv_string_chk(&argvars[0]);
17131 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17132 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17133 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17134
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17137 rettv->vval.v_string = NULL;
17138 else
17139 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140}
17141
17142/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017143 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017146f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149{
17150 int id = 0;
17151#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017152 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 long col;
17154 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017155 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017157 lnum = get_tv_lnum(argvars); /* -1 on type error */
17158 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17159 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017161 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017162 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017163 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164#endif
17165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017166 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167}
17168
17169/*
17170 * "synIDattr(id, what [, mode])" function
17171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017174 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176{
17177 char_u *p = NULL;
17178#ifdef FEAT_SYN_HL
17179 int id;
17180 char_u *what;
17181 char_u *mode;
17182 char_u modebuf[NUMBUFLEN];
17183 int modec;
17184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017185 id = get_tv_number(&argvars[0]);
17186 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017187 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017189 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017191 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 modec = 0; /* replace invalid with current */
17193 }
17194 else
17195 {
17196#ifdef FEAT_GUI
17197 if (gui.in_use)
17198 modec = 'g';
17199 else
17200#endif
17201 if (t_colors > 1)
17202 modec = 'c';
17203 else
17204 modec = 't';
17205 }
17206
17207
17208 switch (TOLOWER_ASC(what[0]))
17209 {
17210 case 'b':
17211 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17212 p = highlight_color(id, what, modec);
17213 else /* bold */
17214 p = highlight_has_attr(id, HL_BOLD, modec);
17215 break;
17216
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017217 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 p = highlight_color(id, what, modec);
17219 break;
17220
17221 case 'i':
17222 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17223 p = highlight_has_attr(id, HL_INVERSE, modec);
17224 else /* italic */
17225 p = highlight_has_attr(id, HL_ITALIC, modec);
17226 break;
17227
17228 case 'n': /* name */
17229 p = get_highlight_name(NULL, id - 1);
17230 break;
17231
17232 case 'r': /* reverse */
17233 p = highlight_has_attr(id, HL_INVERSE, modec);
17234 break;
17235
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017236 case 's':
17237 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17238 p = highlight_color(id, what, modec);
17239 else /* standout */
17240 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 break;
17242
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017243 case 'u':
17244 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17245 /* underline */
17246 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17247 else
17248 /* undercurl */
17249 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 break;
17251 }
17252
17253 if (p != NULL)
17254 p = vim_strsave(p);
17255#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017256 rettv->v_type = VAR_STRING;
17257 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258}
17259
17260/*
17261 * "synIDtrans(id)" function
17262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017264f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017265 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267{
17268 int id;
17269
17270#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017271 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272
17273 if (id > 0)
17274 id = syn_get_final_id(id);
17275 else
17276#endif
17277 id = 0;
17278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280}
17281
17282/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017283 * "synconcealed(lnum, col)" function
17284 */
17285 static void
17286f_synconcealed(argvars, rettv)
17287 typval_T *argvars UNUSED;
17288 typval_T *rettv;
17289{
17290#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17291 long lnum;
17292 long col;
17293 int syntax_flags = 0;
17294 int cchar;
17295 int matchid = 0;
17296 char_u str[NUMBUFLEN];
17297#endif
17298
17299 rettv->v_type = VAR_LIST;
17300 rettv->vval.v_list = NULL;
17301
17302#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17303 lnum = get_tv_lnum(argvars); /* -1 on type error */
17304 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17305
17306 vim_memset(str, NUL, sizeof(str));
17307
17308 if (rettv_list_alloc(rettv) != FAIL)
17309 {
17310 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17311 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17312 && curwin->w_p_cole > 0)
17313 {
17314 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17315 syntax_flags = get_syntax_info(&matchid);
17316
17317 /* get the conceal character */
17318 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17319 {
17320 cchar = syn_get_sub_char();
17321 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17322 cchar = lcs_conceal;
17323 if (cchar != NUL)
17324 {
17325# ifdef FEAT_MBYTE
17326 if (has_mbyte)
17327 (*mb_char2bytes)(cchar, str);
17328 else
17329# endif
17330 str[0] = cchar;
17331 }
17332 }
17333 }
17334
17335 list_append_number(rettv->vval.v_list,
17336 (syntax_flags & HL_CONCEAL) != 0);
17337 /* -1 to auto-determine strlen */
17338 list_append_string(rettv->vval.v_list, str, -1);
17339 list_append_number(rettv->vval.v_list, matchid);
17340 }
17341#endif
17342}
17343
17344/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017345 * "synstack(lnum, col)" function
17346 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017347 static void
17348f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017349 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017350 typval_T *rettv;
17351{
17352#ifdef FEAT_SYN_HL
17353 long lnum;
17354 long col;
17355 int i;
17356 int id;
17357#endif
17358
17359 rettv->v_type = VAR_LIST;
17360 rettv->vval.v_list = NULL;
17361
17362#ifdef FEAT_SYN_HL
17363 lnum = get_tv_lnum(argvars); /* -1 on type error */
17364 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17365
17366 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017367 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017368 && rettv_list_alloc(rettv) != FAIL)
17369 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017370 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017371 for (i = 0; ; ++i)
17372 {
17373 id = syn_get_stack_item(i);
17374 if (id < 0)
17375 break;
17376 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17377 break;
17378 }
17379 }
17380#endif
17381}
17382
17383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 * "system()" function
17385 */
17386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017387f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 typval_T *argvars;
17389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017391 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017393 char_u *infile = NULL;
17394 char_u buf[NUMBUFLEN];
17395 int err = FALSE;
17396 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017398 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017399 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017400
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017401 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017402 {
17403 /*
17404 * Write the string to a temp file, to be used for input of the shell
17405 * command.
17406 */
17407 if ((infile = vim_tempname('i')) == NULL)
17408 {
17409 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017410 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017411 }
17412
17413 fd = mch_fopen((char *)infile, WRITEBIN);
17414 if (fd == NULL)
17415 {
17416 EMSG2(_(e_notopen), infile);
17417 goto done;
17418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017419 p = get_tv_string_buf_chk(&argvars[1], buf);
17420 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017421 {
17422 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017423 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017424 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017425 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17426 err = TRUE;
17427 if (fclose(fd) != 0)
17428 err = TRUE;
17429 if (err)
17430 {
17431 EMSG(_("E677: Error writing temp file"));
17432 goto done;
17433 }
17434 }
17435
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017436 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17437 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017438
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439#ifdef USE_CR
17440 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017441 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 {
17443 char_u *s;
17444
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017445 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446 {
17447 if (*s == CAR)
17448 *s = NL;
17449 }
17450 }
17451#else
17452# ifdef USE_CRNL
17453 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017454 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 {
17456 char_u *s, *d;
17457
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017458 d = res;
17459 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 {
17461 if (s[0] == CAR && s[1] == NL)
17462 ++s;
17463 *d++ = *s;
17464 }
17465 *d = NUL;
17466 }
17467# endif
17468#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017469
17470done:
17471 if (infile != NULL)
17472 {
17473 mch_remove(infile);
17474 vim_free(infile);
17475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017476 rettv->v_type = VAR_STRING;
17477 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478}
17479
17480/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017481 * "tabpagebuflist()" function
17482 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017483 static void
17484f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017485 typval_T *argvars UNUSED;
17486 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017487{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017488#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017489 tabpage_T *tp;
17490 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017491
17492 if (argvars[0].v_type == VAR_UNKNOWN)
17493 wp = firstwin;
17494 else
17495 {
17496 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17497 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017498 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017499 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017500 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017501 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017502 for (; wp != NULL; wp = wp->w_next)
17503 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017504 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017505 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017506 }
17507#endif
17508}
17509
17510
17511/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017512 * "tabpagenr()" function
17513 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017514 static void
17515f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017516 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017517 typval_T *rettv;
17518{
17519 int nr = 1;
17520#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017521 char_u *arg;
17522
17523 if (argvars[0].v_type != VAR_UNKNOWN)
17524 {
17525 arg = get_tv_string_chk(&argvars[0]);
17526 nr = 0;
17527 if (arg != NULL)
17528 {
17529 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017530 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017531 else
17532 EMSG2(_(e_invexpr2), arg);
17533 }
17534 }
17535 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017536 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017537#endif
17538 rettv->vval.v_number = nr;
17539}
17540
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017541
17542#ifdef FEAT_WINDOWS
17543static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17544
17545/*
17546 * Common code for tabpagewinnr() and winnr().
17547 */
17548 static int
17549get_winnr(tp, argvar)
17550 tabpage_T *tp;
17551 typval_T *argvar;
17552{
17553 win_T *twin;
17554 int nr = 1;
17555 win_T *wp;
17556 char_u *arg;
17557
17558 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17559 if (argvar->v_type != VAR_UNKNOWN)
17560 {
17561 arg = get_tv_string_chk(argvar);
17562 if (arg == NULL)
17563 nr = 0; /* type error; errmsg already given */
17564 else if (STRCMP(arg, "$") == 0)
17565 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17566 else if (STRCMP(arg, "#") == 0)
17567 {
17568 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17569 if (twin == NULL)
17570 nr = 0;
17571 }
17572 else
17573 {
17574 EMSG2(_(e_invexpr2), arg);
17575 nr = 0;
17576 }
17577 }
17578
17579 if (nr > 0)
17580 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17581 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017582 {
17583 if (wp == NULL)
17584 {
17585 /* didn't find it in this tabpage */
17586 nr = 0;
17587 break;
17588 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017589 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017590 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017591 return nr;
17592}
17593#endif
17594
17595/*
17596 * "tabpagewinnr()" function
17597 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017598 static void
17599f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017600 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017601 typval_T *rettv;
17602{
17603 int nr = 1;
17604#ifdef FEAT_WINDOWS
17605 tabpage_T *tp;
17606
17607 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17608 if (tp == NULL)
17609 nr = 0;
17610 else
17611 nr = get_winnr(tp, &argvars[1]);
17612#endif
17613 rettv->vval.v_number = nr;
17614}
17615
17616
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017617/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017618 * "tagfiles()" function
17619 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017620 static void
17621f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017622 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017623 typval_T *rettv;
17624{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017625 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017626 tagname_T tn;
17627 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017628
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017629 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017630 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017631 fname = alloc(MAXPATHL);
17632 if (fname == NULL)
17633 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017635 for (first = TRUE; ; first = FALSE)
17636 if (get_tagfname(&tn, first, fname) == FAIL
17637 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017638 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017639 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017640 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017641}
17642
17643/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017644 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017645 */
17646 static void
17647f_taglist(argvars, rettv)
17648 typval_T *argvars;
17649 typval_T *rettv;
17650{
17651 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017652
17653 tag_pattern = get_tv_string(&argvars[0]);
17654
17655 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017656 if (*tag_pattern == NUL)
17657 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017658
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017659 if (rettv_list_alloc(rettv) == OK)
17660 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017661}
17662
17663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 * "tempname()" function
17665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017667f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017668 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670{
17671 static int x = 'A';
17672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017673 rettv->v_type = VAR_STRING;
17674 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675
17676 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17677 * names. Skip 'I' and 'O', they are used for shell redirection. */
17678 do
17679 {
17680 if (x == 'Z')
17681 x = '0';
17682 else if (x == '9')
17683 x = 'A';
17684 else
17685 {
17686#ifdef EBCDIC
17687 if (x == 'I')
17688 x = 'J';
17689 else if (x == 'R')
17690 x = 'S';
17691 else
17692#endif
17693 ++x;
17694 }
17695 } while (x == 'I' || x == 'O');
17696}
17697
17698/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017699 * "test(list)" function: Just checking the walls...
17700 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017701 static void
17702f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017703 typval_T *argvars UNUSED;
17704 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017705{
17706 /* Used for unit testing. Change the code below to your liking. */
17707#if 0
17708 listitem_T *li;
17709 list_T *l;
17710 char_u *bad, *good;
17711
17712 if (argvars[0].v_type != VAR_LIST)
17713 return;
17714 l = argvars[0].vval.v_list;
17715 if (l == NULL)
17716 return;
17717 li = l->lv_first;
17718 if (li == NULL)
17719 return;
17720 bad = get_tv_string(&li->li_tv);
17721 li = li->li_next;
17722 if (li == NULL)
17723 return;
17724 good = get_tv_string(&li->li_tv);
17725 rettv->vval.v_number = test_edit_score(bad, good);
17726#endif
17727}
17728
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017729#ifdef FEAT_FLOAT
17730/*
17731 * "tan()" function
17732 */
17733 static void
17734f_tan(argvars, rettv)
17735 typval_T *argvars;
17736 typval_T *rettv;
17737{
17738 float_T f;
17739
17740 rettv->v_type = VAR_FLOAT;
17741 if (get_float_arg(argvars, &f) == OK)
17742 rettv->vval.v_float = tan(f);
17743 else
17744 rettv->vval.v_float = 0.0;
17745}
17746
17747/*
17748 * "tanh()" function
17749 */
17750 static void
17751f_tanh(argvars, rettv)
17752 typval_T *argvars;
17753 typval_T *rettv;
17754{
17755 float_T f;
17756
17757 rettv->v_type = VAR_FLOAT;
17758 if (get_float_arg(argvars, &f) == OK)
17759 rettv->vval.v_float = tanh(f);
17760 else
17761 rettv->vval.v_float = 0.0;
17762}
17763#endif
17764
Bram Moolenaard52d9742005-08-21 22:20:28 +000017765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 * "tolower(string)" function
17767 */
17768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017769f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017770 typval_T *argvars;
17771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772{
17773 char_u *p;
17774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017775 p = vim_strsave(get_tv_string(&argvars[0]));
17776 rettv->v_type = VAR_STRING;
17777 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778
17779 if (p != NULL)
17780 while (*p != NUL)
17781 {
17782#ifdef FEAT_MBYTE
17783 int l;
17784
17785 if (enc_utf8)
17786 {
17787 int c, lc;
17788
17789 c = utf_ptr2char(p);
17790 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017791 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 /* TODO: reallocate string when byte count changes. */
17793 if (utf_char2len(lc) == l)
17794 utf_char2bytes(lc, p);
17795 p += l;
17796 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017797 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 p += l; /* skip multi-byte character */
17799 else
17800#endif
17801 {
17802 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17803 ++p;
17804 }
17805 }
17806}
17807
17808/*
17809 * "toupper(string)" function
17810 */
17811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017812f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017813 typval_T *argvars;
17814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017816 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017817 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818}
17819
17820/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017821 * "tr(string, fromstr, tostr)" function
17822 */
17823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017824f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017825 typval_T *argvars;
17826 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017827{
17828 char_u *instr;
17829 char_u *fromstr;
17830 char_u *tostr;
17831 char_u *p;
17832#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017833 int inlen;
17834 int fromlen;
17835 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017836 int idx;
17837 char_u *cpstr;
17838 int cplen;
17839 int first = TRUE;
17840#endif
17841 char_u buf[NUMBUFLEN];
17842 char_u buf2[NUMBUFLEN];
17843 garray_T ga;
17844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017845 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017846 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17847 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017848
17849 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017850 rettv->v_type = VAR_STRING;
17851 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017852 if (fromstr == NULL || tostr == NULL)
17853 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017854 ga_init2(&ga, (int)sizeof(char), 80);
17855
17856#ifdef FEAT_MBYTE
17857 if (!has_mbyte)
17858#endif
17859 /* not multi-byte: fromstr and tostr must be the same length */
17860 if (STRLEN(fromstr) != STRLEN(tostr))
17861 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017862#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017863error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017864#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017865 EMSG2(_(e_invarg2), fromstr);
17866 ga_clear(&ga);
17867 return;
17868 }
17869
17870 /* fromstr and tostr have to contain the same number of chars */
17871 while (*instr != NUL)
17872 {
17873#ifdef FEAT_MBYTE
17874 if (has_mbyte)
17875 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017876 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017877 cpstr = instr;
17878 cplen = inlen;
17879 idx = 0;
17880 for (p = fromstr; *p != NUL; p += fromlen)
17881 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017882 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017883 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17884 {
17885 for (p = tostr; *p != NUL; p += tolen)
17886 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017887 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017888 if (idx-- == 0)
17889 {
17890 cplen = tolen;
17891 cpstr = p;
17892 break;
17893 }
17894 }
17895 if (*p == NUL) /* tostr is shorter than fromstr */
17896 goto error;
17897 break;
17898 }
17899 ++idx;
17900 }
17901
17902 if (first && cpstr == instr)
17903 {
17904 /* Check that fromstr and tostr have the same number of
17905 * (multi-byte) characters. Done only once when a character
17906 * of instr doesn't appear in fromstr. */
17907 first = FALSE;
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 --idx;
17912 }
17913 if (idx != 0)
17914 goto error;
17915 }
17916
17917 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017918 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017919 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017920
17921 instr += inlen;
17922 }
17923 else
17924#endif
17925 {
17926 /* When not using multi-byte chars we can do it faster. */
17927 p = vim_strchr(fromstr, *instr);
17928 if (p != NULL)
17929 ga_append(&ga, tostr[p - fromstr]);
17930 else
17931 ga_append(&ga, *instr);
17932 ++instr;
17933 }
17934 }
17935
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017936 /* add a terminating NUL */
17937 ga_grow(&ga, 1);
17938 ga_append(&ga, NUL);
17939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017940 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017941}
17942
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017943#ifdef FEAT_FLOAT
17944/*
17945 * "trunc({float})" function
17946 */
17947 static void
17948f_trunc(argvars, rettv)
17949 typval_T *argvars;
17950 typval_T *rettv;
17951{
17952 float_T f;
17953
17954 rettv->v_type = VAR_FLOAT;
17955 if (get_float_arg(argvars, &f) == OK)
17956 /* trunc() is not in C90, use floor() or ceil() instead. */
17957 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17958 else
17959 rettv->vval.v_float = 0.0;
17960}
17961#endif
17962
Bram Moolenaar8299df92004-07-10 09:47:34 +000017963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 * "type(expr)" function
17965 */
17966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017967f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017968 typval_T *argvars;
17969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017971 int n;
17972
17973 switch (argvars[0].v_type)
17974 {
17975 case VAR_NUMBER: n = 0; break;
17976 case VAR_STRING: n = 1; break;
17977 case VAR_FUNC: n = 2; break;
17978 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017979 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017980#ifdef FEAT_FLOAT
17981 case VAR_FLOAT: n = 5; break;
17982#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017983 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17984 }
17985 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986}
17987
17988/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017989 * "undofile(name)" function
17990 */
17991 static void
17992f_undofile(argvars, rettv)
17993 typval_T *argvars;
17994 typval_T *rettv;
17995{
17996 rettv->v_type = VAR_STRING;
17997#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017998 {
17999 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
18000
18001 if (ffname != NULL)
18002 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18003 vim_free(ffname);
18004 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018005#else
18006 rettv->vval.v_string = NULL;
18007#endif
18008}
18009
18010/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018011 * "undotree()" function
18012 */
18013 static void
18014f_undotree(argvars, rettv)
18015 typval_T *argvars UNUSED;
18016 typval_T *rettv;
18017{
18018 if (rettv_dict_alloc(rettv) == OK)
18019 {
18020 dict_T *dict = rettv->vval.v_dict;
18021 list_T *list;
18022
Bram Moolenaar730cde92010-06-27 05:18:54 +020018023 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018024 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018025 dict_add_nr_str(dict, "save_last",
18026 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018027 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18028 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018029 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018030
18031 list = list_alloc();
18032 if (list != NULL)
18033 {
18034 u_eval_tree(curbuf->b_u_oldhead, list);
18035 dict_add_list(dict, "entries", list);
18036 }
18037 }
18038}
18039
18040/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018041 * "values(dict)" function
18042 */
18043 static void
18044f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 typval_T *argvars;
18046 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018047{
18048 dict_list(argvars, rettv, 1);
18049}
18050
18051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 * "virtcol(string)" function
18053 */
18054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018055f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018056 typval_T *argvars;
18057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058{
18059 colnr_T vcol = 0;
18060 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018061 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018063 fp = var2fpos(&argvars[0], FALSE, &fnum);
18064 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18065 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 {
18067 getvvcol(curwin, fp, NULL, NULL, &vcol);
18068 ++vcol;
18069 }
18070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018071 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072}
18073
18074/*
18075 * "visualmode()" function
18076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018078f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018079 typval_T *argvars UNUSED;
18080 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081{
18082#ifdef FEAT_VISUAL
18083 char_u str[2];
18084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 str[0] = curbuf->b_visual_mode_eval;
18087 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
18090 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018091 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093#endif
18094}
18095
18096/*
18097 * "winbufnr(nr)" function
18098 */
18099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018101 typval_T *argvars;
18102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103{
18104 win_T *wp;
18105
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018106 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018108 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018110 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111}
18112
18113/*
18114 * "wincol()" function
18115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018117f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018118 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120{
18121 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018122 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123}
18124
18125/*
18126 * "winheight(nr)" function
18127 */
18128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018130 typval_T *argvars;
18131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132{
18133 win_T *wp;
18134
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018135 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018139 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140}
18141
18142/*
18143 * "winline()" function
18144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149{
18150 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018151 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152}
18153
18154/*
18155 * "winnr()" function
18156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018158f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018159 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161{
18162 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018163
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018165 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018167 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168}
18169
18170/*
18171 * "winrestcmd()" function
18172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018174f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018175 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177{
18178#ifdef FEAT_WINDOWS
18179 win_T *wp;
18180 int winnr = 1;
18181 garray_T ga;
18182 char_u buf[50];
18183
18184 ga_init2(&ga, (int)sizeof(char), 70);
18185 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18186 {
18187 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18188 ga_concat(&ga, buf);
18189# ifdef FEAT_VERTSPLIT
18190 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18191 ga_concat(&ga, buf);
18192# endif
18193 ++winnr;
18194 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018195 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018197 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018199 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018201 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202}
18203
18204/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018205 * "winrestview()" function
18206 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018207 static void
18208f_winrestview(argvars, rettv)
18209 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018210 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018211{
18212 dict_T *dict;
18213
18214 if (argvars[0].v_type != VAR_DICT
18215 || (dict = argvars[0].vval.v_dict) == NULL)
18216 EMSG(_(e_invarg));
18217 else
18218 {
18219 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18220 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18221#ifdef FEAT_VIRTUALEDIT
18222 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18223#endif
18224 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018225 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018226
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018227 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018228#ifdef FEAT_DIFF
18229 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18230#endif
18231 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18232 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18233
18234 check_cursor();
18235 changed_cline_bef_curs();
18236 invalidate_botline();
18237 redraw_later(VALID);
18238
18239 if (curwin->w_topline == 0)
18240 curwin->w_topline = 1;
18241 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18242 curwin->w_topline = curbuf->b_ml.ml_line_count;
18243#ifdef FEAT_DIFF
18244 check_topfill(curwin, TRUE);
18245#endif
18246 }
18247}
18248
18249/*
18250 * "winsaveview()" function
18251 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018252 static void
18253f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018254 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018255 typval_T *rettv;
18256{
18257 dict_T *dict;
18258
Bram Moolenaara800b422010-06-27 01:15:55 +020018259 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018260 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018261 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018262
18263 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18264 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18265#ifdef FEAT_VIRTUALEDIT
18266 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18267#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018268 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018269 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18270
18271 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18272#ifdef FEAT_DIFF
18273 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18274#endif
18275 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18276 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18277}
18278
18279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 * "winwidth(nr)" function
18281 */
18282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018283f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018284 typval_T *argvars;
18285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286{
18287 win_T *wp;
18288
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018289 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018291 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 else
18293#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018294 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018296 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297#endif
18298}
18299
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018301 * "writefile()" function
18302 */
18303 static void
18304f_writefile(argvars, rettv)
18305 typval_T *argvars;
18306 typval_T *rettv;
18307{
18308 int binary = FALSE;
18309 char_u *fname;
18310 FILE *fd;
18311 listitem_T *li;
18312 char_u *s;
18313 int ret = 0;
18314 int c;
18315
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018316 if (check_restricted() || check_secure())
18317 return;
18318
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018319 if (argvars[0].v_type != VAR_LIST)
18320 {
18321 EMSG2(_(e_listarg), "writefile()");
18322 return;
18323 }
18324 if (argvars[0].vval.v_list == NULL)
18325 return;
18326
18327 if (argvars[2].v_type != VAR_UNKNOWN
18328 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18329 binary = TRUE;
18330
18331 /* Always open the file in binary mode, library functions have a mind of
18332 * their own about CR-LF conversion. */
18333 fname = get_tv_string(&argvars[1]);
18334 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18335 {
18336 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18337 ret = -1;
18338 }
18339 else
18340 {
18341 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18342 li = li->li_next)
18343 {
18344 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18345 {
18346 if (*s == '\n')
18347 c = putc(NUL, fd);
18348 else
18349 c = putc(*s, fd);
18350 if (c == EOF)
18351 {
18352 ret = -1;
18353 break;
18354 }
18355 }
18356 if (!binary || li->li_next != NULL)
18357 if (putc('\n', fd) == EOF)
18358 {
18359 ret = -1;
18360 break;
18361 }
18362 if (ret < 0)
18363 {
18364 EMSG(_(e_write));
18365 break;
18366 }
18367 }
18368 fclose(fd);
18369 }
18370
18371 rettv->vval.v_number = ret;
18372}
18373
18374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018376 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 */
18378 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018379var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018380 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018381 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018382 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018384 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018386 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387
Bram Moolenaara5525202006-03-02 22:52:09 +000018388 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018389 if (varp->v_type == VAR_LIST)
18390 {
18391 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018392 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018393 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018394 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018395
18396 l = varp->vval.v_list;
18397 if (l == NULL)
18398 return NULL;
18399
18400 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018401 pos.lnum = list_find_nr(l, 0L, &error);
18402 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018403 return NULL; /* invalid line number */
18404
18405 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018406 pos.col = list_find_nr(l, 1L, &error);
18407 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018408 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018409 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018410
18411 /* We accept "$" for the column number: last column. */
18412 li = list_find(l, 1L);
18413 if (li != NULL && li->li_tv.v_type == VAR_STRING
18414 && li->li_tv.vval.v_string != NULL
18415 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18416 pos.col = len + 1;
18417
Bram Moolenaara5525202006-03-02 22:52:09 +000018418 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018419 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018420 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018421 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018422
Bram Moolenaara5525202006-03-02 22:52:09 +000018423#ifdef FEAT_VIRTUALEDIT
18424 /* Get the virtual offset. Defaults to zero. */
18425 pos.coladd = list_find_nr(l, 2L, &error);
18426 if (error)
18427 pos.coladd = 0;
18428#endif
18429
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018430 return &pos;
18431 }
18432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018433 name = get_tv_string_chk(varp);
18434 if (name == NULL)
18435 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018436 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018438#ifdef FEAT_VISUAL
18439 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18440 {
18441 if (VIsual_active)
18442 return &VIsual;
18443 return &curwin->w_cursor;
18444 }
18445#endif
18446 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018448 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18450 return NULL;
18451 return pp;
18452 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018453
18454#ifdef FEAT_VIRTUALEDIT
18455 pos.coladd = 0;
18456#endif
18457
Bram Moolenaar477933c2007-07-17 14:32:23 +000018458 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018459 {
18460 pos.col = 0;
18461 if (name[1] == '0') /* "w0": first visible line */
18462 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018463 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018464 pos.lnum = curwin->w_topline;
18465 return &pos;
18466 }
18467 else if (name[1] == '$') /* "w$": last visible line */
18468 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018469 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018470 pos.lnum = curwin->w_botline - 1;
18471 return &pos;
18472 }
18473 }
18474 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018476 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 {
18478 pos.lnum = curbuf->b_ml.ml_line_count;
18479 pos.col = 0;
18480 }
18481 else
18482 {
18483 pos.lnum = curwin->w_cursor.lnum;
18484 pos.col = (colnr_T)STRLEN(ml_get_curline());
18485 }
18486 return &pos;
18487 }
18488 return NULL;
18489}
18490
18491/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018492 * Convert list in "arg" into a position and optional file number.
18493 * When "fnump" is NULL there is no file number, only 3 items.
18494 * Note that the column is passed on as-is, the caller may want to decrement
18495 * it to use 1 for the first column.
18496 * Return FAIL when conversion is not possible, doesn't check the position for
18497 * validity.
18498 */
18499 static int
18500list2fpos(arg, posp, fnump)
18501 typval_T *arg;
18502 pos_T *posp;
18503 int *fnump;
18504{
18505 list_T *l = arg->vval.v_list;
18506 long i = 0;
18507 long n;
18508
Bram Moolenaarbde35262006-07-23 20:12:24 +000018509 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18510 * when "fnump" isn't NULL and "coladd" is optional. */
18511 if (arg->v_type != VAR_LIST
18512 || l == NULL
18513 || l->lv_len < (fnump == NULL ? 2 : 3)
18514 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018515 return FAIL;
18516
18517 if (fnump != NULL)
18518 {
18519 n = list_find_nr(l, i++, NULL); /* fnum */
18520 if (n < 0)
18521 return FAIL;
18522 if (n == 0)
18523 n = curbuf->b_fnum; /* current buffer */
18524 *fnump = n;
18525 }
18526
18527 n = list_find_nr(l, i++, NULL); /* lnum */
18528 if (n < 0)
18529 return FAIL;
18530 posp->lnum = n;
18531
18532 n = list_find_nr(l, i++, NULL); /* col */
18533 if (n < 0)
18534 return FAIL;
18535 posp->col = n;
18536
18537#ifdef FEAT_VIRTUALEDIT
18538 n = list_find_nr(l, i, NULL);
18539 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018540 posp->coladd = 0;
18541 else
18542 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018543#endif
18544
18545 return OK;
18546}
18547
18548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 * Get the length of an environment variable name.
18550 * Advance "arg" to the first character after the name.
18551 * Return 0 for error.
18552 */
18553 static int
18554get_env_len(arg)
18555 char_u **arg;
18556{
18557 char_u *p;
18558 int len;
18559
18560 for (p = *arg; vim_isIDc(*p); ++p)
18561 ;
18562 if (p == *arg) /* no name found */
18563 return 0;
18564
18565 len = (int)(p - *arg);
18566 *arg = p;
18567 return len;
18568}
18569
18570/*
18571 * Get the length of the name of a function or internal variable.
18572 * "arg" is advanced to the first non-white character after the name.
18573 * Return 0 if something is wrong.
18574 */
18575 static int
18576get_id_len(arg)
18577 char_u **arg;
18578{
18579 char_u *p;
18580 int len;
18581
18582 /* Find the end of the name. */
18583 for (p = *arg; eval_isnamec(*p); ++p)
18584 ;
18585 if (p == *arg) /* no name found */
18586 return 0;
18587
18588 len = (int)(p - *arg);
18589 *arg = skipwhite(p);
18590
18591 return len;
18592}
18593
18594/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018595 * Get the length of the name of a variable or function.
18596 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018598 * Return -1 if curly braces expansion failed.
18599 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 * If the name contains 'magic' {}'s, expand them and return the
18601 * expanded name in an allocated string via 'alias' - caller must free.
18602 */
18603 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018604get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 char_u **arg;
18606 char_u **alias;
18607 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018608 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609{
18610 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 char_u *p;
18612 char_u *expr_start;
18613 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614
18615 *alias = NULL; /* default to no alias */
18616
18617 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18618 && (*arg)[2] == (int)KE_SNR)
18619 {
18620 /* hard coded <SNR>, already translated */
18621 *arg += 3;
18622 return get_id_len(arg) + 3;
18623 }
18624 len = eval_fname_script(*arg);
18625 if (len > 0)
18626 {
18627 /* literal "<SID>", "s:" or "<SNR>" */
18628 *arg += len;
18629 }
18630
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018632 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018634 p = find_name_end(*arg, &expr_start, &expr_end,
18635 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 if (expr_start != NULL)
18637 {
18638 char_u *temp_string;
18639
18640 if (!evaluate)
18641 {
18642 len += (int)(p - *arg);
18643 *arg = skipwhite(p);
18644 return len;
18645 }
18646
18647 /*
18648 * Include any <SID> etc in the expanded string:
18649 * Thus the -len here.
18650 */
18651 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18652 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018653 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 *alias = temp_string;
18655 *arg = skipwhite(p);
18656 return (int)STRLEN(temp_string);
18657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658
18659 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018660 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 EMSG2(_(e_invexpr2), *arg);
18662
18663 return len;
18664}
18665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666/*
18667 * Find the end of a variable or function name, taking care of magic braces.
18668 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18669 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018670 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018671 * Return a pointer to just after the name. Equal to "arg" if there is no
18672 * valid name.
18673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018675find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 char_u *arg;
18677 char_u **expr_start;
18678 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018679 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018681 int mb_nest = 0;
18682 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 char_u *p;
18684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018685 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018687 *expr_start = NULL;
18688 *expr_end = NULL;
18689 }
18690
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018691 /* Quick check for valid starting character. */
18692 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18693 return arg;
18694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018695 for (p = arg; *p != NUL
18696 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018697 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018698 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018699 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018700 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018701 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018702 if (*p == '\'')
18703 {
18704 /* skip over 'string' to avoid counting [ and ] inside it. */
18705 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18706 ;
18707 if (*p == NUL)
18708 break;
18709 }
18710 else if (*p == '"')
18711 {
18712 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18713 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18714 if (*p == '\\' && p[1] != NUL)
18715 ++p;
18716 if (*p == NUL)
18717 break;
18718 }
18719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018720 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722 if (*p == '[')
18723 ++br_nest;
18724 else if (*p == ']')
18725 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018730 if (*p == '{')
18731 {
18732 mb_nest++;
18733 if (expr_start != NULL && *expr_start == NULL)
18734 *expr_start = p;
18735 }
18736 else if (*p == '}')
18737 {
18738 mb_nest--;
18739 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18740 *expr_end = p;
18741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 }
18744
18745 return p;
18746}
18747
18748/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018749 * Expands out the 'magic' {}'s in a variable/function name.
18750 * Note that this can call itself recursively, to deal with
18751 * constructs like foo{bar}{baz}{bam}
18752 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18753 * "in_start" ^
18754 * "expr_start" ^
18755 * "expr_end" ^
18756 * "in_end" ^
18757 *
18758 * Returns a new allocated string, which the caller must free.
18759 * Returns NULL for failure.
18760 */
18761 static char_u *
18762make_expanded_name(in_start, expr_start, expr_end, in_end)
18763 char_u *in_start;
18764 char_u *expr_start;
18765 char_u *expr_end;
18766 char_u *in_end;
18767{
18768 char_u c1;
18769 char_u *retval = NULL;
18770 char_u *temp_result;
18771 char_u *nextcmd = NULL;
18772
18773 if (expr_end == NULL || in_end == NULL)
18774 return NULL;
18775 *expr_start = NUL;
18776 *expr_end = NUL;
18777 c1 = *in_end;
18778 *in_end = NUL;
18779
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018780 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018781 if (temp_result != NULL && nextcmd == NULL)
18782 {
18783 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18784 + (in_end - expr_end) + 1));
18785 if (retval != NULL)
18786 {
18787 STRCPY(retval, in_start);
18788 STRCAT(retval, temp_result);
18789 STRCAT(retval, expr_end + 1);
18790 }
18791 }
18792 vim_free(temp_result);
18793
18794 *in_end = c1; /* put char back for error messages */
18795 *expr_start = '{';
18796 *expr_end = '}';
18797
18798 if (retval != NULL)
18799 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018800 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018801 if (expr_start != NULL)
18802 {
18803 /* Further expansion! */
18804 temp_result = make_expanded_name(retval, expr_start,
18805 expr_end, temp_result);
18806 vim_free(retval);
18807 retval = temp_result;
18808 }
18809 }
18810
18811 return retval;
18812}
18813
18814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018816 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 */
18818 static int
18819eval_isnamec(c)
18820 int c;
18821{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018822 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18823}
18824
18825/*
18826 * Return TRUE if character "c" can be used as the first character in a
18827 * variable or function name (excluding '{' and '}').
18828 */
18829 static int
18830eval_isnamec1(c)
18831 int c;
18832{
18833 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834}
18835
18836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 * Set number v: variable to "val".
18838 */
18839 void
18840set_vim_var_nr(idx, val)
18841 int idx;
18842 long val;
18843{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018844 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845}
18846
18847/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018848 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 */
18850 long
18851get_vim_var_nr(idx)
18852 int idx;
18853{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018854 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855}
18856
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018857/*
18858 * Get string v: variable value. Uses a static buffer, can only be used once.
18859 */
18860 char_u *
18861get_vim_var_str(idx)
18862 int idx;
18863{
18864 return get_tv_string(&vimvars[idx].vv_tv);
18865}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018866
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018868 * Get List v: variable value. Caller must take care of reference count when
18869 * needed.
18870 */
18871 list_T *
18872get_vim_var_list(idx)
18873 int idx;
18874{
18875 return vimvars[idx].vv_list;
18876}
18877
18878/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018879 * Set v:char to character "c".
18880 */
18881 void
18882set_vim_var_char(c)
18883 int c;
18884{
18885#ifdef FEAT_MBYTE
18886 char_u buf[MB_MAXBYTES];
18887#else
18888 char_u buf[2];
18889#endif
18890
18891#ifdef FEAT_MBYTE
18892 if (has_mbyte)
18893 buf[(*mb_char2bytes)(c, buf)] = NUL;
18894 else
18895#endif
18896 {
18897 buf[0] = c;
18898 buf[1] = NUL;
18899 }
18900 set_vim_var_string(VV_CHAR, buf, -1);
18901}
18902
18903/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018904 * Set v:count to "count" and v:count1 to "count1".
18905 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 */
18907 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018908set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 long count;
18910 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018911 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018913 if (set_prevcount)
18914 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018915 vimvars[VV_COUNT].vv_nr = count;
18916 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917}
18918
18919/*
18920 * Set string v: variable to a copy of "val".
18921 */
18922 void
18923set_vim_var_string(idx, val, len)
18924 int idx;
18925 char_u *val;
18926 int len; /* length of "val" to use or -1 (whole string) */
18927{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018928 /* Need to do this (at least) once, since we can't initialize a union.
18929 * Will always be invoked when "v:progname" is set. */
18930 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18931
Bram Moolenaare9a41262005-01-15 22:18:47 +000018932 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018934 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018936 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018938 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939}
18940
18941/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018942 * Set List v: variable to "val".
18943 */
18944 void
18945set_vim_var_list(idx, val)
18946 int idx;
18947 list_T *val;
18948{
18949 list_unref(vimvars[idx].vv_list);
18950 vimvars[idx].vv_list = val;
18951 if (val != NULL)
18952 ++val->lv_refcount;
18953}
18954
18955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 * Set v:register if needed.
18957 */
18958 void
18959set_reg_var(c)
18960 int c;
18961{
18962 char_u regname;
18963
18964 if (c == 0 || c == ' ')
18965 regname = '"';
18966 else
18967 regname = c;
18968 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018969 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 set_vim_var_string(VV_REG, &regname, 1);
18971}
18972
18973/*
18974 * Get or set v:exception. If "oldval" == NULL, return the current value.
18975 * Otherwise, restore the value to "oldval" and return NULL.
18976 * Must always be called in pairs to save and restore v:exception! Does not
18977 * take care of memory allocations.
18978 */
18979 char_u *
18980v_exception(oldval)
18981 char_u *oldval;
18982{
18983 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018984 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985
Bram Moolenaare9a41262005-01-15 22:18:47 +000018986 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 return NULL;
18988}
18989
18990/*
18991 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18992 * Otherwise, restore the value to "oldval" and return NULL.
18993 * Must always be called in pairs to save and restore v:throwpoint! Does not
18994 * take care of memory allocations.
18995 */
18996 char_u *
18997v_throwpoint(oldval)
18998 char_u *oldval;
18999{
19000 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019001 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002
Bram Moolenaare9a41262005-01-15 22:18:47 +000019003 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 return NULL;
19005}
19006
19007#if defined(FEAT_AUTOCMD) || defined(PROTO)
19008/*
19009 * Set v:cmdarg.
19010 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19011 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19012 * Must always be called in pairs!
19013 */
19014 char_u *
19015set_cmdarg(eap, oldarg)
19016 exarg_T *eap;
19017 char_u *oldarg;
19018{
19019 char_u *oldval;
19020 char_u *newval;
19021 unsigned len;
19022
Bram Moolenaare9a41262005-01-15 22:18:47 +000019023 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019024 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019026 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019027 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019028 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 }
19030
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019031 if (eap->force_bin == FORCE_BIN)
19032 len = 6;
19033 else if (eap->force_bin == FORCE_NOBIN)
19034 len = 8;
19035 else
19036 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019037
19038 if (eap->read_edit)
19039 len += 7;
19040
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019041 if (eap->force_ff != 0)
19042 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19043# ifdef FEAT_MBYTE
19044 if (eap->force_enc != 0)
19045 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019046 if (eap->bad_char != 0)
19047 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019048# endif
19049
19050 newval = alloc(len + 1);
19051 if (newval == NULL)
19052 return NULL;
19053
19054 if (eap->force_bin == FORCE_BIN)
19055 sprintf((char *)newval, " ++bin");
19056 else if (eap->force_bin == FORCE_NOBIN)
19057 sprintf((char *)newval, " ++nobin");
19058 else
19059 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019060
19061 if (eap->read_edit)
19062 STRCAT(newval, " ++edit");
19063
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019064 if (eap->force_ff != 0)
19065 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19066 eap->cmd + eap->force_ff);
19067# ifdef FEAT_MBYTE
19068 if (eap->force_enc != 0)
19069 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19070 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019071 if (eap->bad_char == BAD_KEEP)
19072 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19073 else if (eap->bad_char == BAD_DROP)
19074 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19075 else if (eap->bad_char != 0)
19076 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019077# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019078 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019079 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080}
19081#endif
19082
19083/*
19084 * Get the value of internal variable "name".
19085 * Return OK or FAIL.
19086 */
19087 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019088get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 char_u *name;
19090 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019091 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019092 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093{
19094 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019095 typval_T *tv = NULL;
19096 typval_T atv;
19097 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099
19100 /* truncate the name, so that we can use strcmp() */
19101 cc = name[len];
19102 name[len] = NUL;
19103
19104 /*
19105 * Check for "b:changedtick".
19106 */
19107 if (STRCMP(name, "b:changedtick") == 0)
19108 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019109 atv.v_type = VAR_NUMBER;
19110 atv.vval.v_number = curbuf->b_changedtick;
19111 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 }
19113
19114 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 * Check for user-defined variables.
19116 */
19117 else
19118 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019119 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019121 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 }
19123
Bram Moolenaare9a41262005-01-15 22:18:47 +000019124 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019126 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019127 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 ret = FAIL;
19129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019131 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132
19133 name[len] = cc;
19134
19135 return ret;
19136}
19137
19138/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019139 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19140 * Also handle function call with Funcref variable: func(expr)
19141 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19142 */
19143 static int
19144handle_subscript(arg, rettv, evaluate, verbose)
19145 char_u **arg;
19146 typval_T *rettv;
19147 int evaluate; /* do more than finding the end */
19148 int verbose; /* give error messages */
19149{
19150 int ret = OK;
19151 dict_T *selfdict = NULL;
19152 char_u *s;
19153 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019154 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019155
19156 while (ret == OK
19157 && (**arg == '['
19158 || (**arg == '.' && rettv->v_type == VAR_DICT)
19159 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19160 && !vim_iswhite(*(*arg - 1)))
19161 {
19162 if (**arg == '(')
19163 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019164 /* need to copy the funcref so that we can clear rettv */
19165 functv = *rettv;
19166 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019167
19168 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019169 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019170 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019171 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19172 &len, evaluate, selfdict);
19173
19174 /* Clear the funcref afterwards, so that deleting it while
19175 * evaluating the arguments is possible (see test55). */
19176 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019177
19178 /* Stop the expression evaluation when immediately aborting on
19179 * error, or when an interrupt occurred or an exception was thrown
19180 * but not caught. */
19181 if (aborting())
19182 {
19183 if (ret == OK)
19184 clear_tv(rettv);
19185 ret = FAIL;
19186 }
19187 dict_unref(selfdict);
19188 selfdict = NULL;
19189 }
19190 else /* **arg == '[' || **arg == '.' */
19191 {
19192 dict_unref(selfdict);
19193 if (rettv->v_type == VAR_DICT)
19194 {
19195 selfdict = rettv->vval.v_dict;
19196 if (selfdict != NULL)
19197 ++selfdict->dv_refcount;
19198 }
19199 else
19200 selfdict = NULL;
19201 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19202 {
19203 clear_tv(rettv);
19204 ret = FAIL;
19205 }
19206 }
19207 }
19208 dict_unref(selfdict);
19209 return ret;
19210}
19211
19212/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019213 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019214 * value).
19215 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019216 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019218{
Bram Moolenaar33570922005-01-25 22:26:29 +000019219 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019220}
19221
19222/*
19223 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 * The string "s" must have been allocated, it is consumed.
19225 * Return NULL for out of memory, the variable otherwise.
19226 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019228alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 char_u *s;
19230{
Bram Moolenaar33570922005-01-25 22:26:29 +000019231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019233 rettv = alloc_tv();
19234 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236 rettv->v_type = VAR_STRING;
19237 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238 }
19239 else
19240 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019241 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242}
19243
19244/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019245 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019247 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019248free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019249 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250{
19251 if (varp != NULL)
19252 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019253 switch (varp->v_type)
19254 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019255 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019256 func_unref(varp->vval.v_string);
19257 /*FALLTHROUGH*/
19258 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019259 vim_free(varp->vval.v_string);
19260 break;
19261 case VAR_LIST:
19262 list_unref(varp->vval.v_list);
19263 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019264 case VAR_DICT:
19265 dict_unref(varp->vval.v_dict);
19266 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019267 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019268#ifdef FEAT_FLOAT
19269 case VAR_FLOAT:
19270#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019271 case VAR_UNKNOWN:
19272 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019273 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019274 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019275 break;
19276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 vim_free(varp);
19278 }
19279}
19280
19281/*
19282 * Free the memory for a variable value and set the value to NULL or 0.
19283 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019284 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019285clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019286 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287{
19288 if (varp != NULL)
19289 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019290 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019292 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019293 func_unref(varp->vval.v_string);
19294 /*FALLTHROUGH*/
19295 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019296 vim_free(varp->vval.v_string);
19297 varp->vval.v_string = NULL;
19298 break;
19299 case VAR_LIST:
19300 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019301 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019302 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019303 case VAR_DICT:
19304 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019305 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019306 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019308 varp->vval.v_number = 0;
19309 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019310#ifdef FEAT_FLOAT
19311 case VAR_FLOAT:
19312 varp->vval.v_float = 0.0;
19313 break;
19314#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019315 case VAR_UNKNOWN:
19316 break;
19317 default:
19318 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019320 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 }
19322}
19323
19324/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019325 * Set the value of a variable to NULL without freeing items.
19326 */
19327 static void
19328init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019329 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019330{
19331 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019332 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019333}
19334
19335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 * Get the number value of a variable.
19337 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019338 * For incompatible types, return 0.
19339 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19340 * caller of incompatible types: it sets *denote to TRUE if "denote"
19341 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 */
19343 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019344get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019345 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019347 int error = FALSE;
19348
19349 return get_tv_number_chk(varp, &error); /* return 0L on error */
19350}
19351
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019352 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019353get_tv_number_chk(varp, denote)
19354 typval_T *varp;
19355 int *denote;
19356{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019357 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019359 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019361 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019362 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019363#ifdef FEAT_FLOAT
19364 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019365 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019366 break;
19367#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019368 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019369 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019370 break;
19371 case VAR_STRING:
19372 if (varp->vval.v_string != NULL)
19373 vim_str2nr(varp->vval.v_string, NULL, NULL,
19374 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019375 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019376 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019377 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019378 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019379 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019380 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019381 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019382 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019383 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019384 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019386 if (denote == NULL) /* useful for values that must be unsigned */
19387 n = -1;
19388 else
19389 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019390 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391}
19392
19393/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019394 * Get the lnum from the first argument.
19395 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019396 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 */
19398 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019399get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019400 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401{
Bram Moolenaar33570922005-01-25 22:26:29 +000019402 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 linenr_T lnum;
19404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019405 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 if (lnum == 0) /* no valid number, try using line() */
19407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019408 rettv.v_type = VAR_NUMBER;
19409 f_line(argvars, &rettv);
19410 lnum = rettv.vval.v_number;
19411 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 }
19413 return lnum;
19414}
19415
19416/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019417 * Get the lnum from the first argument.
19418 * Also accepts "$", then "buf" is used.
19419 * Returns 0 on error.
19420 */
19421 static linenr_T
19422get_tv_lnum_buf(argvars, buf)
19423 typval_T *argvars;
19424 buf_T *buf;
19425{
19426 if (argvars[0].v_type == VAR_STRING
19427 && argvars[0].vval.v_string != NULL
19428 && argvars[0].vval.v_string[0] == '$'
19429 && buf != NULL)
19430 return buf->b_ml.ml_line_count;
19431 return get_tv_number_chk(&argvars[0], NULL);
19432}
19433
19434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 * Get the string value of a variable.
19436 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019437 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19438 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 * If the String variable has never been set, return an empty string.
19440 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019441 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19442 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 */
19444 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019445get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019446 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447{
19448 static char_u mybuf[NUMBUFLEN];
19449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019450 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451}
19452
19453 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019454get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019455 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019456 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019458 char_u *res = get_tv_string_buf_chk(varp, buf);
19459
19460 return res != NULL ? res : (char_u *)"";
19461}
19462
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019463 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019464get_tv_string_chk(varp)
19465 typval_T *varp;
19466{
19467 static char_u mybuf[NUMBUFLEN];
19468
19469 return get_tv_string_buf_chk(varp, mybuf);
19470}
19471
19472 static char_u *
19473get_tv_string_buf_chk(varp, buf)
19474 typval_T *varp;
19475 char_u *buf;
19476{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019477 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019479 case VAR_NUMBER:
19480 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19481 return buf;
19482 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019483 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019484 break;
19485 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019486 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019487 break;
19488 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019489 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019490 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019491#ifdef FEAT_FLOAT
19492 case VAR_FLOAT:
19493 EMSG(_("E806: using Float as a String"));
19494 break;
19495#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019496 case VAR_STRING:
19497 if (varp->vval.v_string != NULL)
19498 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019499 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019500 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019501 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019504 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505}
19506
19507/*
19508 * Find variable "name" in the list of variables.
19509 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019510 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019511 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019512 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019515find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019517 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521
Bram Moolenaara7043832005-01-21 11:56:39 +000019522 ht = find_var_ht(name, &varname);
19523 if (htp != NULL)
19524 *htp = ht;
19525 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019527 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528}
19529
19530/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019531 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019532 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019535find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019536 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019537 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019538 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019539{
Bram Moolenaar33570922005-01-25 22:26:29 +000019540 hashitem_T *hi;
19541
19542 if (*varname == NUL)
19543 {
19544 /* Must be something like "s:", otherwise "ht" would be NULL. */
19545 switch (varname[-2])
19546 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019547 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019548 case 'g': return &globvars_var;
19549 case 'v': return &vimvars_var;
19550 case 'b': return &curbuf->b_bufvar;
19551 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019552#ifdef FEAT_WINDOWS
19553 case 't': return &curtab->tp_winvar;
19554#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019555 case 'l': return current_funccal == NULL
19556 ? NULL : &current_funccal->l_vars_var;
19557 case 'a': return current_funccal == NULL
19558 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019559 }
19560 return NULL;
19561 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019562
19563 hi = hash_find(ht, varname);
19564 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019565 {
19566 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019567 * worked find the variable again. Don't auto-load a script if it was
19568 * loaded already, otherwise it would be loaded every time when
19569 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019570 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019571 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019572 hi = hash_find(ht, varname);
19573 if (HASHITEM_EMPTY(hi))
19574 return NULL;
19575 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019576 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019577}
19578
19579/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019581 * Set "varname" to the start of name without ':'.
19582 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019583 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019584find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 char_u *name;
19586 char_u **varname;
19587{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019588 hashitem_T *hi;
19589
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 if (name[1] != ':')
19591 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019592 /* The name must not start with a colon or #. */
19593 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 return NULL;
19595 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019596
19597 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019598 hi = hash_find(&compat_hashtab, name);
19599 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019600 return &compat_hashtab;
19601
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 return &globvarht; /* global variable */
19604 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 }
19606 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019607 if (*name == 'g') /* global variable */
19608 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019609 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19610 */
19611 if (vim_strchr(name + 2, ':') != NULL
19612 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019613 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019617 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019618#ifdef FEAT_WINDOWS
19619 if (*name == 't') /* tab page variable */
19620 return &curtab->tp_vars.dv_hashtab;
19621#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 if (*name == 'v') /* v: variable */
19623 return &vimvarht;
19624 if (*name == 'a' && current_funccal != NULL) /* function argument */
19625 return &current_funccal->l_avars.dv_hashtab;
19626 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19627 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 if (*name == 's' /* script variable */
19629 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19630 return &SCRIPT_VARS(current_SID);
19631 return NULL;
19632}
19633
19634/*
19635 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019636 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 * Returns NULL when it doesn't exist.
19638 */
19639 char_u *
19640get_var_value(name)
19641 char_u *name;
19642{
Bram Moolenaar33570922005-01-25 22:26:29 +000019643 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644
Bram Moolenaara7043832005-01-21 11:56:39 +000019645 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 if (v == NULL)
19647 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019648 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649}
19650
19651/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 * sourcing this script and when executing functions defined in the script.
19654 */
19655 void
19656new_script_vars(id)
19657 scid_T id;
19658{
Bram Moolenaara7043832005-01-21 11:56:39 +000019659 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019660 hashtab_T *ht;
19661 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019662
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19664 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019665 /* Re-allocating ga_data means that an ht_array pointing to
19666 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019667 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019668 for (i = 1; i <= ga_scripts.ga_len; ++i)
19669 {
19670 ht = &SCRIPT_VARS(i);
19671 if (ht->ht_mask == HT_INIT_SIZE - 1)
19672 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019673 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019674 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019675 }
19676
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 while (ga_scripts.ga_len < id)
19678 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019679 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019680 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019681 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 }
19684 }
19685}
19686
19687/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019688 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19689 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 */
19691 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019692init_var_dict(dict, dict_var)
19693 dict_T *dict;
19694 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695{
Bram Moolenaar33570922005-01-25 22:26:29 +000019696 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019697 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019698 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019699 dict_var->di_tv.vval.v_dict = dict;
19700 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019701 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019702 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19703 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704}
19705
19706/*
19707 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 * Frees all allocated variables and the value they contain.
19709 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 */
19711 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019712vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019713 hashtab_T *ht;
19714{
19715 vars_clear_ext(ht, TRUE);
19716}
19717
19718/*
19719 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19720 */
19721 static void
19722vars_clear_ext(ht, free_val)
19723 hashtab_T *ht;
19724 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725{
Bram Moolenaara7043832005-01-21 11:56:39 +000019726 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 hashitem_T *hi;
19728 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729
Bram Moolenaar33570922005-01-25 22:26:29 +000019730 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019731 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019732 for (hi = ht->ht_array; todo > 0; ++hi)
19733 {
19734 if (!HASHITEM_EMPTY(hi))
19735 {
19736 --todo;
19737
Bram Moolenaar33570922005-01-25 22:26:29 +000019738 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019739 * ht_array might change then. hash_clear() takes care of it
19740 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019741 v = HI2DI(hi);
19742 if (free_val)
19743 clear_tv(&v->di_tv);
19744 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19745 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019746 }
19747 }
19748 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019749 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750}
19751
Bram Moolenaara7043832005-01-21 11:56:39 +000019752/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 * Delete a variable from hashtab "ht" at item "hi".
19754 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019757delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019758 hashtab_T *ht;
19759 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760{
Bram Moolenaar33570922005-01-25 22:26:29 +000019761 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019762
19763 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 clear_tv(&di->di_tv);
19765 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766}
19767
19768/*
19769 * List the value of one internal variable.
19770 */
19771 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019772list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019773 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019775 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019777 char_u *tofree;
19778 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019779 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019780
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019781 current_copyID += COPYID_INC;
19782 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019784 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019785 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786}
19787
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019789list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 char_u *prefix;
19791 char_u *name;
19792 int type;
19793 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019794 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795{
Bram Moolenaar31859182007-08-14 20:41:13 +000019796 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19797 msg_start();
19798 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 if (name != NULL) /* "a:" vars don't have a name stored */
19800 msg_puts(name);
19801 msg_putchar(' ');
19802 msg_advance(22);
19803 if (type == VAR_NUMBER)
19804 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019805 else if (type == VAR_FUNC)
19806 msg_putchar('*');
19807 else if (type == VAR_LIST)
19808 {
19809 msg_putchar('[');
19810 if (*string == '[')
19811 ++string;
19812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019813 else if (type == VAR_DICT)
19814 {
19815 msg_putchar('{');
19816 if (*string == '{')
19817 ++string;
19818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 else
19820 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019821
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019823
19824 if (type == VAR_FUNC)
19825 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019826 if (*first)
19827 {
19828 msg_clr_eos();
19829 *first = FALSE;
19830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831}
19832
19833/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 * If the variable already exists, the value is updated.
19836 * Otherwise the variable is created.
19837 */
19838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019839set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019841 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843{
Bram Moolenaar33570922005-01-25 22:26:29 +000019844 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019846 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019848 ht = find_var_ht(name, &varname);
19849 if (ht == NULL || *varname == NUL)
19850 {
19851 EMSG2(_(e_illvar), name);
19852 return;
19853 }
19854 v = find_var_in_ht(ht, varname, TRUE);
19855
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019856 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
19857 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019858
Bram Moolenaar33570922005-01-25 22:26:29 +000019859 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019862 if (var_check_ro(v->di_flags, name)
19863 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 return;
19865 if (v->di_tv.v_type != tv->v_type
19866 && !((v->di_tv.v_type == VAR_STRING
19867 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019869 || tv->v_type == VAR_NUMBER))
19870#ifdef FEAT_FLOAT
19871 && !((v->di_tv.v_type == VAR_NUMBER
19872 || v->di_tv.v_type == VAR_FLOAT)
19873 && (tv->v_type == VAR_NUMBER
19874 || tv->v_type == VAR_FLOAT))
19875#endif
19876 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019877 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019878 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019879 return;
19880 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019881
19882 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019883 * Handle setting internal v: variables separately: we don't change
19884 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019885 */
19886 if (ht == &vimvarht)
19887 {
19888 if (v->di_tv.v_type == VAR_STRING)
19889 {
19890 vim_free(v->di_tv.vval.v_string);
19891 if (copy || tv->v_type != VAR_STRING)
19892 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19893 else
19894 {
19895 /* Take over the string to avoid an extra alloc/free. */
19896 v->di_tv.vval.v_string = tv->vval.v_string;
19897 tv->vval.v_string = NULL;
19898 }
19899 }
19900 else if (v->di_tv.v_type != VAR_NUMBER)
19901 EMSG2(_(e_intern2), "set_var()");
19902 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019903 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019904 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019905 if (STRCMP(varname, "searchforward") == 0)
19906 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19907 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 return;
19909 }
19910
19911 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912 }
19913 else /* add a new variable */
19914 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019915 /* Can't add "v:" variable. */
19916 if (ht == &vimvarht)
19917 {
19918 EMSG2(_(e_illvar), name);
19919 return;
19920 }
19921
Bram Moolenaar92124a32005-06-17 22:03:40 +000019922 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019923 if (!valid_varname(varname))
19924 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019926 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19927 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019928 if (v == NULL)
19929 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019930 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019933 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 return;
19935 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019936 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019938
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019939 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019940 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019941 else
19942 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019944 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019945 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947}
19948
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019949/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019950 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019951 * Also give an error message.
19952 */
19953 static int
19954var_check_ro(flags, name)
19955 int flags;
19956 char_u *name;
19957{
19958 if (flags & DI_FLAGS_RO)
19959 {
19960 EMSG2(_(e_readonlyvar), name);
19961 return TRUE;
19962 }
19963 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19964 {
19965 EMSG2(_(e_readonlysbx), name);
19966 return TRUE;
19967 }
19968 return FALSE;
19969}
19970
19971/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019972 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19973 * Also give an error message.
19974 */
19975 static int
19976var_check_fixed(flags, name)
19977 int flags;
19978 char_u *name;
19979{
19980 if (flags & DI_FLAGS_FIX)
19981 {
19982 EMSG2(_("E795: Cannot delete variable %s"), name);
19983 return TRUE;
19984 }
19985 return FALSE;
19986}
19987
19988/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020019989 * Check if a funcref is assigned to a valid variable name.
19990 * Return TRUE and give an error if not.
19991 */
19992 static int
19993var_check_func_name(name, new_var)
19994 char_u *name; /* points to start of variable name */
19995 int new_var; /* TRUE when creating the variable */
19996{
19997 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19998 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19999 ? name[2] : name[0]))
20000 {
20001 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20002 name);
20003 return TRUE;
20004 }
20005 /* Don't allow hiding a function. When "v" is not NULL we might be
20006 * assigning another function to the same var, the type is checked
20007 * below. */
20008 if (new_var && function_exists(name))
20009 {
20010 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20011 name);
20012 return TRUE;
20013 }
20014 return FALSE;
20015}
20016
20017/*
20018 * Check if a variable name is valid.
20019 * Return FALSE and give an error if not.
20020 */
20021 static int
20022valid_varname(varname)
20023 char_u *varname;
20024{
20025 char_u *p;
20026
20027 for (p = varname; *p != NUL; ++p)
20028 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20029 && *p != AUTOLOAD_CHAR)
20030 {
20031 EMSG2(_(e_illvar), varname);
20032 return FALSE;
20033 }
20034 return TRUE;
20035}
20036
20037/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020038 * Return TRUE if typeval "tv" is set to be locked (immutable).
20039 * Also give an error message, using "name".
20040 */
20041 static int
20042tv_check_lock(lock, name)
20043 int lock;
20044 char_u *name;
20045{
20046 if (lock & VAR_LOCKED)
20047 {
20048 EMSG2(_("E741: Value is locked: %s"),
20049 name == NULL ? (char_u *)_("Unknown") : name);
20050 return TRUE;
20051 }
20052 if (lock & VAR_FIXED)
20053 {
20054 EMSG2(_("E742: Cannot change value of %s"),
20055 name == NULL ? (char_u *)_("Unknown") : name);
20056 return TRUE;
20057 }
20058 return FALSE;
20059}
20060
20061/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020063 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020064 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020065 * It is OK for "from" and "to" to point to the same item. This is used to
20066 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020067 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020068 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020069copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 typval_T *from;
20071 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020073 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020074 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020075 switch (from->v_type)
20076 {
20077 case VAR_NUMBER:
20078 to->vval.v_number = from->vval.v_number;
20079 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020080#ifdef FEAT_FLOAT
20081 case VAR_FLOAT:
20082 to->vval.v_float = from->vval.v_float;
20083 break;
20084#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020085 case VAR_STRING:
20086 case VAR_FUNC:
20087 if (from->vval.v_string == NULL)
20088 to->vval.v_string = NULL;
20089 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020090 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020091 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020092 if (from->v_type == VAR_FUNC)
20093 func_ref(to->vval.v_string);
20094 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020095 break;
20096 case VAR_LIST:
20097 if (from->vval.v_list == NULL)
20098 to->vval.v_list = NULL;
20099 else
20100 {
20101 to->vval.v_list = from->vval.v_list;
20102 ++to->vval.v_list->lv_refcount;
20103 }
20104 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020105 case VAR_DICT:
20106 if (from->vval.v_dict == NULL)
20107 to->vval.v_dict = NULL;
20108 else
20109 {
20110 to->vval.v_dict = from->vval.v_dict;
20111 ++to->vval.v_dict->dv_refcount;
20112 }
20113 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020114 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020115 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020116 break;
20117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118}
20119
20120/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020121 * Make a copy of an item.
20122 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020123 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20124 * reference to an already copied list/dict can be used.
20125 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020126 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020127 static int
20128item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020129 typval_T *from;
20130 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020131 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020132 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020133{
20134 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020135 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020136
Bram Moolenaar33570922005-01-25 22:26:29 +000020137 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020138 {
20139 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020140 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020141 }
20142 ++recurse;
20143
20144 switch (from->v_type)
20145 {
20146 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020147#ifdef FEAT_FLOAT
20148 case VAR_FLOAT:
20149#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020150 case VAR_STRING:
20151 case VAR_FUNC:
20152 copy_tv(from, to);
20153 break;
20154 case VAR_LIST:
20155 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020156 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020157 if (from->vval.v_list == NULL)
20158 to->vval.v_list = NULL;
20159 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20160 {
20161 /* use the copy made earlier */
20162 to->vval.v_list = from->vval.v_list->lv_copylist;
20163 ++to->vval.v_list->lv_refcount;
20164 }
20165 else
20166 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20167 if (to->vval.v_list == NULL)
20168 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020169 break;
20170 case VAR_DICT:
20171 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020172 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020173 if (from->vval.v_dict == NULL)
20174 to->vval.v_dict = NULL;
20175 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20176 {
20177 /* use the copy made earlier */
20178 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20179 ++to->vval.v_dict->dv_refcount;
20180 }
20181 else
20182 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20183 if (to->vval.v_dict == NULL)
20184 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020185 break;
20186 default:
20187 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020188 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020189 }
20190 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020191 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020192}
20193
20194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 * ":echo expr1 ..." print each argument separated with a space, add a
20196 * newline at the end.
20197 * ":echon expr1 ..." print each argument plain.
20198 */
20199 void
20200ex_echo(eap)
20201 exarg_T *eap;
20202{
20203 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020205 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 char_u *p;
20207 int needclr = TRUE;
20208 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020209 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210
20211 if (eap->skip)
20212 ++emsg_skip;
20213 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20214 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020215 /* If eval1() causes an error message the text from the command may
20216 * still need to be cleared. E.g., "echo 22,44". */
20217 need_clr_eos = needclr;
20218
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020220 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 {
20222 /*
20223 * Report the invalid expression unless the expression evaluation
20224 * has been cancelled due to an aborting error, an interrupt, or an
20225 * exception.
20226 */
20227 if (!aborting())
20228 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020229 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 break;
20231 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020232 need_clr_eos = FALSE;
20233
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 if (!eap->skip)
20235 {
20236 if (atstart)
20237 {
20238 atstart = FALSE;
20239 /* Call msg_start() after eval1(), evaluating the expression
20240 * may cause a message to appear. */
20241 if (eap->cmdidx == CMD_echo)
20242 msg_start();
20243 }
20244 else if (eap->cmdidx == CMD_echo)
20245 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020246 current_copyID += COPYID_INC;
20247 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020248 if (p != NULL)
20249 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020251 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020253 if (*p != TAB && needclr)
20254 {
20255 /* remove any text still there from the command */
20256 msg_clr_eos();
20257 needclr = FALSE;
20258 }
20259 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 }
20261 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020262 {
20263#ifdef FEAT_MBYTE
20264 if (has_mbyte)
20265 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020266 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020267
20268 (void)msg_outtrans_len_attr(p, i, echo_attr);
20269 p += i - 1;
20270 }
20271 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020273 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020276 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 arg = skipwhite(arg);
20280 }
20281 eap->nextcmd = check_nextcmd(arg);
20282
20283 if (eap->skip)
20284 --emsg_skip;
20285 else
20286 {
20287 /* remove text that may still be there from the command */
20288 if (needclr)
20289 msg_clr_eos();
20290 if (eap->cmdidx == CMD_echo)
20291 msg_end();
20292 }
20293}
20294
20295/*
20296 * ":echohl {name}".
20297 */
20298 void
20299ex_echohl(eap)
20300 exarg_T *eap;
20301{
20302 int id;
20303
20304 id = syn_name2id(eap->arg);
20305 if (id == 0)
20306 echo_attr = 0;
20307 else
20308 echo_attr = syn_id2attr(id);
20309}
20310
20311/*
20312 * ":execute expr1 ..." execute the result of an expression.
20313 * ":echomsg expr1 ..." Print a message
20314 * ":echoerr expr1 ..." Print an error
20315 * Each gets spaces around each argument and a newline at the end for
20316 * echo commands
20317 */
20318 void
20319ex_execute(eap)
20320 exarg_T *eap;
20321{
20322 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 int ret = OK;
20325 char_u *p;
20326 garray_T ga;
20327 int len;
20328 int save_did_emsg;
20329
20330 ga_init2(&ga, 1, 80);
20331
20332 if (eap->skip)
20333 ++emsg_skip;
20334 while (*arg != NUL && *arg != '|' && *arg != '\n')
20335 {
20336 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020337 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 {
20339 /*
20340 * Report the invalid expression unless the expression evaluation
20341 * has been cancelled due to an aborting error, an interrupt, or an
20342 * exception.
20343 */
20344 if (!aborting())
20345 EMSG2(_(e_invexpr2), p);
20346 ret = FAIL;
20347 break;
20348 }
20349
20350 if (!eap->skip)
20351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020352 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 len = (int)STRLEN(p);
20354 if (ga_grow(&ga, len + 2) == FAIL)
20355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 ret = FAIL;
20358 break;
20359 }
20360 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 ga.ga_len += len;
20364 }
20365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020366 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 arg = skipwhite(arg);
20368 }
20369
20370 if (ret != FAIL && ga.ga_data != NULL)
20371 {
20372 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020375 out_flush();
20376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 else if (eap->cmdidx == CMD_echoerr)
20378 {
20379 /* We don't want to abort following commands, restore did_emsg. */
20380 save_did_emsg = did_emsg;
20381 EMSG((char_u *)ga.ga_data);
20382 if (!force_abort)
20383 did_emsg = save_did_emsg;
20384 }
20385 else if (eap->cmdidx == CMD_execute)
20386 do_cmdline((char_u *)ga.ga_data,
20387 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20388 }
20389
20390 ga_clear(&ga);
20391
20392 if (eap->skip)
20393 --emsg_skip;
20394
20395 eap->nextcmd = check_nextcmd(arg);
20396}
20397
20398/*
20399 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20400 * "arg" points to the "&" or '+' when called, to "option" when returning.
20401 * Returns NULL when no option name found. Otherwise pointer to the char
20402 * after the option name.
20403 */
20404 static char_u *
20405find_option_end(arg, opt_flags)
20406 char_u **arg;
20407 int *opt_flags;
20408{
20409 char_u *p = *arg;
20410
20411 ++p;
20412 if (*p == 'g' && p[1] == ':')
20413 {
20414 *opt_flags = OPT_GLOBAL;
20415 p += 2;
20416 }
20417 else if (*p == 'l' && p[1] == ':')
20418 {
20419 *opt_flags = OPT_LOCAL;
20420 p += 2;
20421 }
20422 else
20423 *opt_flags = 0;
20424
20425 if (!ASCII_ISALPHA(*p))
20426 return NULL;
20427 *arg = p;
20428
20429 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20430 p += 4; /* termcap option */
20431 else
20432 while (ASCII_ISALPHA(*p))
20433 ++p;
20434 return p;
20435}
20436
20437/*
20438 * ":function"
20439 */
20440 void
20441ex_function(eap)
20442 exarg_T *eap;
20443{
20444 char_u *theline;
20445 int j;
20446 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 char_u *name = NULL;
20449 char_u *p;
20450 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020451 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 garray_T newargs;
20453 garray_T newlines;
20454 int varargs = FALSE;
20455 int mustend = FALSE;
20456 int flags = 0;
20457 ufunc_T *fp;
20458 int indent;
20459 int nesting;
20460 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020461 dictitem_T *v;
20462 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020463 static int func_nr = 0; /* number for nameless function */
20464 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020465 hashtab_T *ht;
20466 int todo;
20467 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020468 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469
20470 /*
20471 * ":function" without argument: list functions.
20472 */
20473 if (ends_excmd(*eap->arg))
20474 {
20475 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020476 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020477 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020478 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020479 {
20480 if (!HASHITEM_EMPTY(hi))
20481 {
20482 --todo;
20483 fp = HI2UF(hi);
20484 if (!isdigit(*fp->uf_name))
20485 list_func_head(fp, FALSE);
20486 }
20487 }
20488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 eap->nextcmd = check_nextcmd(eap->arg);
20490 return;
20491 }
20492
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020493 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020494 * ":function /pat": list functions matching pattern.
20495 */
20496 if (*eap->arg == '/')
20497 {
20498 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20499 if (!eap->skip)
20500 {
20501 regmatch_T regmatch;
20502
20503 c = *p;
20504 *p = NUL;
20505 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20506 *p = c;
20507 if (regmatch.regprog != NULL)
20508 {
20509 regmatch.rm_ic = p_ic;
20510
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020511 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020512 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20513 {
20514 if (!HASHITEM_EMPTY(hi))
20515 {
20516 --todo;
20517 fp = HI2UF(hi);
20518 if (!isdigit(*fp->uf_name)
20519 && vim_regexec(&regmatch, fp->uf_name, 0))
20520 list_func_head(fp, FALSE);
20521 }
20522 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020523 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020524 }
20525 }
20526 if (*p == '/')
20527 ++p;
20528 eap->nextcmd = check_nextcmd(p);
20529 return;
20530 }
20531
20532 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020533 * Get the function name. There are these situations:
20534 * func normal function name
20535 * "name" == func, "fudi.fd_dict" == NULL
20536 * dict.func new dictionary entry
20537 * "name" == NULL, "fudi.fd_dict" set,
20538 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20539 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020540 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020541 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20542 * dict.func existing dict entry that's not a Funcref
20543 * "name" == NULL, "fudi.fd_dict" set,
20544 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020547 name = trans_function_name(&p, eap->skip, 0, &fudi);
20548 paren = (vim_strchr(p, '(') != NULL);
20549 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 {
20551 /*
20552 * Return on an invalid expression in braces, unless the expression
20553 * evaluation has been cancelled due to an aborting error, an
20554 * interrupt, or an exception.
20555 */
20556 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020557 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020558 if (!eap->skip && fudi.fd_newkey != NULL)
20559 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020560 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 else
20564 eap->skip = TRUE;
20565 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020566
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 /* An error in a function call during evaluation of an expression in magic
20568 * braces should not cause the function not to be defined. */
20569 saved_did_emsg = did_emsg;
20570 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571
20572 /*
20573 * ":function func" with only function name: list function.
20574 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020575 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 {
20577 if (!ends_excmd(*skipwhite(p)))
20578 {
20579 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020580 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 }
20582 eap->nextcmd = check_nextcmd(p);
20583 if (eap->nextcmd != NULL)
20584 *p = NUL;
20585 if (!eap->skip && !got_int)
20586 {
20587 fp = find_func(name);
20588 if (fp != NULL)
20589 {
20590 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020591 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020593 if (FUNCLINE(fp, j) == NULL)
20594 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 msg_putchar('\n');
20596 msg_outnum((long)(j + 1));
20597 if (j < 9)
20598 msg_putchar(' ');
20599 if (j < 99)
20600 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020601 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 out_flush(); /* show a line at a time */
20603 ui_breakcheck();
20604 }
20605 if (!got_int)
20606 {
20607 msg_putchar('\n');
20608 msg_puts((char_u *)" endfunction");
20609 }
20610 }
20611 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020612 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020614 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 }
20616
20617 /*
20618 * ":function name(arg1, arg2)" Define function.
20619 */
20620 p = skipwhite(p);
20621 if (*p != '(')
20622 {
20623 if (!eap->skip)
20624 {
20625 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020626 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 }
20628 /* attempt to continue by skipping some text */
20629 if (vim_strchr(p, '(') != NULL)
20630 p = vim_strchr(p, '(');
20631 }
20632 p = skipwhite(p + 1);
20633
20634 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20635 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20636
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020637 if (!eap->skip)
20638 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020639 /* Check the name of the function. Unless it's a dictionary function
20640 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020641 if (name != NULL)
20642 arg = name;
20643 else
20644 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020645 if (arg != NULL && (fudi.fd_di == NULL
20646 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020647 {
20648 if (*arg == K_SPECIAL)
20649 j = 3;
20650 else
20651 j = 0;
20652 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20653 : eval_isnamec(arg[j])))
20654 ++j;
20655 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020656 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020657 }
20658 }
20659
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 /*
20661 * Isolate the arguments: "arg1, arg2, ...)"
20662 */
20663 while (*p != ')')
20664 {
20665 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20666 {
20667 varargs = TRUE;
20668 p += 3;
20669 mustend = TRUE;
20670 }
20671 else
20672 {
20673 arg = p;
20674 while (ASCII_ISALNUM(*p) || *p == '_')
20675 ++p;
20676 if (arg == p || isdigit(*arg)
20677 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20678 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20679 {
20680 if (!eap->skip)
20681 EMSG2(_("E125: Illegal argument: %s"), arg);
20682 break;
20683 }
20684 if (ga_grow(&newargs, 1) == FAIL)
20685 goto erret;
20686 c = *p;
20687 *p = NUL;
20688 arg = vim_strsave(arg);
20689 if (arg == NULL)
20690 goto erret;
20691 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20692 *p = c;
20693 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 if (*p == ',')
20695 ++p;
20696 else
20697 mustend = TRUE;
20698 }
20699 p = skipwhite(p);
20700 if (mustend && *p != ')')
20701 {
20702 if (!eap->skip)
20703 EMSG2(_(e_invarg2), eap->arg);
20704 break;
20705 }
20706 }
20707 ++p; /* skip the ')' */
20708
Bram Moolenaare9a41262005-01-15 22:18:47 +000020709 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 for (;;)
20711 {
20712 p = skipwhite(p);
20713 if (STRNCMP(p, "range", 5) == 0)
20714 {
20715 flags |= FC_RANGE;
20716 p += 5;
20717 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020718 else if (STRNCMP(p, "dict", 4) == 0)
20719 {
20720 flags |= FC_DICT;
20721 p += 4;
20722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 else if (STRNCMP(p, "abort", 5) == 0)
20724 {
20725 flags |= FC_ABORT;
20726 p += 5;
20727 }
20728 else
20729 break;
20730 }
20731
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020732 /* When there is a line break use what follows for the function body.
20733 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20734 if (*p == '\n')
20735 line_arg = p + 1;
20736 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 EMSG(_(e_trailing));
20738
20739 /*
20740 * Read the body of the function, until ":endfunction" is found.
20741 */
20742 if (KeyTyped)
20743 {
20744 /* Check if the function already exists, don't let the user type the
20745 * whole function before telling him it doesn't work! For a script we
20746 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020747 if (!eap->skip && !eap->forceit)
20748 {
20749 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20750 EMSG(_(e_funcdict));
20751 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020752 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020755 if (!eap->skip && did_emsg)
20756 goto erret;
20757
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 msg_putchar('\n'); /* don't overwrite the function name */
20759 cmdline_row = msg_row;
20760 }
20761
20762 indent = 2;
20763 nesting = 0;
20764 for (;;)
20765 {
20766 msg_scroll = TRUE;
20767 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020768 sourcing_lnum_off = sourcing_lnum;
20769
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020770 if (line_arg != NULL)
20771 {
20772 /* Use eap->arg, split up in parts by line breaks. */
20773 theline = line_arg;
20774 p = vim_strchr(theline, '\n');
20775 if (p == NULL)
20776 line_arg += STRLEN(line_arg);
20777 else
20778 {
20779 *p = NUL;
20780 line_arg = p + 1;
20781 }
20782 }
20783 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 theline = getcmdline(':', 0L, indent);
20785 else
20786 theline = eap->getline(':', eap->cookie, indent);
20787 if (KeyTyped)
20788 lines_left = Rows - 1;
20789 if (theline == NULL)
20790 {
20791 EMSG(_("E126: Missing :endfunction"));
20792 goto erret;
20793 }
20794
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020795 /* Detect line continuation: sourcing_lnum increased more than one. */
20796 if (sourcing_lnum > sourcing_lnum_off + 1)
20797 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20798 else
20799 sourcing_lnum_off = 0;
20800
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 if (skip_until != NULL)
20802 {
20803 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20804 * don't check for ":endfunc". */
20805 if (STRCMP(theline, skip_until) == 0)
20806 {
20807 vim_free(skip_until);
20808 skip_until = NULL;
20809 }
20810 }
20811 else
20812 {
20813 /* skip ':' and blanks*/
20814 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20815 ;
20816
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020817 /* Check for "endfunction". */
20818 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020820 if (line_arg == NULL)
20821 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 break;
20823 }
20824
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020825 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 * at "end". */
20827 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20828 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020829 else if (STRNCMP(p, "if", 2) == 0
20830 || STRNCMP(p, "wh", 2) == 0
20831 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 || STRNCMP(p, "try", 3) == 0)
20833 indent += 2;
20834
20835 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020836 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020838 if (*p == '!')
20839 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 p += eval_fname_script(p);
20841 if (ASCII_ISALPHA(*p))
20842 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020843 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 if (*skipwhite(p) == '(')
20845 {
20846 ++nesting;
20847 indent += 2;
20848 }
20849 }
20850 }
20851
20852 /* Check for ":append" or ":insert". */
20853 p = skip_range(p, NULL);
20854 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20855 || (p[0] == 'i'
20856 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20857 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20858 skip_until = vim_strsave((char_u *)".");
20859
20860 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20861 arg = skipwhite(skiptowhite(p));
20862 if (arg[0] == '<' && arg[1] =='<'
20863 && ((p[0] == 'p' && p[1] == 'y'
20864 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20865 || (p[0] == 'p' && p[1] == 'e'
20866 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20867 || (p[0] == 't' && p[1] == 'c'
20868 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20869 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20870 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020871 || (p[0] == 'm' && p[1] == 'z'
20872 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873 ))
20874 {
20875 /* ":python <<" continues until a dot, like ":append" */
20876 p = skipwhite(arg + 2);
20877 if (*p == NUL)
20878 skip_until = vim_strsave((char_u *)".");
20879 else
20880 skip_until = vim_strsave(p);
20881 }
20882 }
20883
20884 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020885 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020886 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020887 if (line_arg == NULL)
20888 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020890 }
20891
20892 /* Copy the line to newly allocated memory. get_one_sourceline()
20893 * allocates 250 bytes per line, this saves 80% on average. The cost
20894 * is an extra alloc/free. */
20895 p = vim_strsave(theline);
20896 if (p != NULL)
20897 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020898 if (line_arg == NULL)
20899 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020900 theline = p;
20901 }
20902
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020903 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20904
20905 /* Add NULL lines for continuation lines, so that the line count is
20906 * equal to the index in the growarray. */
20907 while (sourcing_lnum_off-- > 0)
20908 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020909
20910 /* Check for end of eap->arg. */
20911 if (line_arg != NULL && *line_arg == NUL)
20912 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 }
20914
20915 /* Don't define the function when skipping commands or when an error was
20916 * detected. */
20917 if (eap->skip || did_emsg)
20918 goto erret;
20919
20920 /*
20921 * If there are no errors, add the function
20922 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020923 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020924 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020925 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020926 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020927 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020928 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020929 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020930 goto erret;
20931 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020933 fp = find_func(name);
20934 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020936 if (!eap->forceit)
20937 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020938 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020939 goto erret;
20940 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020941 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020942 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020943 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020944 name);
20945 goto erret;
20946 }
20947 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020948 ga_clear_strings(&(fp->uf_args));
20949 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020950 vim_free(name);
20951 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 }
20954 else
20955 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020956 char numbuf[20];
20957
20958 fp = NULL;
20959 if (fudi.fd_newkey == NULL && !eap->forceit)
20960 {
20961 EMSG(_(e_funcdict));
20962 goto erret;
20963 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020964 if (fudi.fd_di == NULL)
20965 {
20966 /* Can't add a function to a locked dictionary */
20967 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20968 goto erret;
20969 }
20970 /* Can't change an existing function if it is locked */
20971 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20972 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020973
20974 /* Give the function a sequential number. Can only be used with a
20975 * Funcref! */
20976 vim_free(name);
20977 sprintf(numbuf, "%d", ++func_nr);
20978 name = vim_strsave((char_u *)numbuf);
20979 if (name == NULL)
20980 goto erret;
20981 }
20982
20983 if (fp == NULL)
20984 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020985 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020986 {
20987 int slen, plen;
20988 char_u *scriptname;
20989
20990 /* Check that the autoload name matches the script name. */
20991 j = FAIL;
20992 if (sourcing_name != NULL)
20993 {
20994 scriptname = autoload_name(name);
20995 if (scriptname != NULL)
20996 {
20997 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020998 plen = (int)STRLEN(p);
20999 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021000 if (slen > plen && fnamecmp(p,
21001 sourcing_name + slen - plen) == 0)
21002 j = OK;
21003 vim_free(scriptname);
21004 }
21005 }
21006 if (j == FAIL)
21007 {
21008 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21009 goto erret;
21010 }
21011 }
21012
21013 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 if (fp == NULL)
21015 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021016
21017 if (fudi.fd_dict != NULL)
21018 {
21019 if (fudi.fd_di == NULL)
21020 {
21021 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021022 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021023 if (fudi.fd_di == NULL)
21024 {
21025 vim_free(fp);
21026 goto erret;
21027 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021028 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21029 {
21030 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021031 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021032 goto erret;
21033 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021034 }
21035 else
21036 /* overwrite existing dict entry */
21037 clear_tv(&fudi.fd_di->di_tv);
21038 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021039 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021040 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021041 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021042
21043 /* behave like "dict" was used */
21044 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021045 }
21046
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021048 STRCPY(fp->uf_name, name);
21049 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021051 fp->uf_args = newargs;
21052 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021053#ifdef FEAT_PROFILE
21054 fp->uf_tml_count = NULL;
21055 fp->uf_tml_total = NULL;
21056 fp->uf_tml_self = NULL;
21057 fp->uf_profiling = FALSE;
21058 if (prof_def_func())
21059 func_do_profile(fp);
21060#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021061 fp->uf_varargs = varargs;
21062 fp->uf_flags = flags;
21063 fp->uf_calls = 0;
21064 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021065 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066
21067erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 ga_clear_strings(&newargs);
21069 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021070ret_free:
21071 vim_free(skip_until);
21072 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075}
21076
21077/*
21078 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021079 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021081 * flags:
21082 * TFN_INT: internal function name OK
21083 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 * Advances "pp" to just after the function name (if no error).
21085 */
21086 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021087trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 char_u **pp;
21089 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021090 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021091 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021093 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 char_u *start;
21095 char_u *end;
21096 int lead;
21097 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021099 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021100
21101 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021102 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021104
21105 /* Check for hard coded <SNR>: already translated function ID (from a user
21106 * command). */
21107 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21108 && (*pp)[2] == (int)KE_SNR)
21109 {
21110 *pp += 3;
21111 len = get_id_len(pp) + 3;
21112 return vim_strnsave(start, len);
21113 }
21114
21115 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21116 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021118 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021120
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021121 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21122 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021123 if (end == start)
21124 {
21125 if (!skip)
21126 EMSG(_("E129: Function name required"));
21127 goto theend;
21128 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021129 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021130 {
21131 /*
21132 * Report an invalid expression in braces, unless the expression
21133 * evaluation has been cancelled due to an aborting error, an
21134 * interrupt, or an exception.
21135 */
21136 if (!aborting())
21137 {
21138 if (end != NULL)
21139 EMSG2(_(e_invarg2), start);
21140 }
21141 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021142 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021143 goto theend;
21144 }
21145
21146 if (lv.ll_tv != NULL)
21147 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021148 if (fdp != NULL)
21149 {
21150 fdp->fd_dict = lv.ll_dict;
21151 fdp->fd_newkey = lv.ll_newkey;
21152 lv.ll_newkey = NULL;
21153 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021154 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021155 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21156 {
21157 name = vim_strsave(lv.ll_tv->vval.v_string);
21158 *pp = end;
21159 }
21160 else
21161 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021162 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21163 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021164 EMSG(_(e_funcref));
21165 else
21166 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021167 name = NULL;
21168 }
21169 goto theend;
21170 }
21171
21172 if (lv.ll_name == NULL)
21173 {
21174 /* Error found, but continue after the function name. */
21175 *pp = end;
21176 goto theend;
21177 }
21178
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021179 /* Check if the name is a Funcref. If so, use the value. */
21180 if (lv.ll_exp_name != NULL)
21181 {
21182 len = (int)STRLEN(lv.ll_exp_name);
21183 name = deref_func_name(lv.ll_exp_name, &len);
21184 if (name == lv.ll_exp_name)
21185 name = NULL;
21186 }
21187 else
21188 {
21189 len = (int)(end - *pp);
21190 name = deref_func_name(*pp, &len);
21191 if (name == *pp)
21192 name = NULL;
21193 }
21194 if (name != NULL)
21195 {
21196 name = vim_strsave(name);
21197 *pp = end;
21198 goto theend;
21199 }
21200
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021201 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021202 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021203 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021204 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21205 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21206 {
21207 /* When there was "s:" already or the name expanded to get a
21208 * leading "s:" then remove it. */
21209 lv.ll_name += 2;
21210 len -= 2;
21211 lead = 2;
21212 }
21213 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021214 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021215 {
21216 if (lead == 2) /* skip over "s:" */
21217 lv.ll_name += 2;
21218 len = (int)(end - lv.ll_name);
21219 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021220
21221 /*
21222 * Copy the function name to allocated memory.
21223 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21224 * Accept <SNR>123_name() outside a script.
21225 */
21226 if (skip)
21227 lead = 0; /* do nothing */
21228 else if (lead > 0)
21229 {
21230 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021231 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21232 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021233 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021234 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021235 if (current_SID <= 0)
21236 {
21237 EMSG(_(e_usingsid));
21238 goto theend;
21239 }
21240 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21241 lead += (int)STRLEN(sid_buf);
21242 }
21243 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021244 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021245 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021246 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021247 goto theend;
21248 }
21249 name = alloc((unsigned)(len + lead + 1));
21250 if (name != NULL)
21251 {
21252 if (lead > 0)
21253 {
21254 name[0] = K_SPECIAL;
21255 name[1] = KS_EXTRA;
21256 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021257 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021258 STRCPY(name + 3, sid_buf);
21259 }
21260 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21261 name[len + lead] = NUL;
21262 }
21263 *pp = end;
21264
21265theend:
21266 clear_lval(&lv);
21267 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268}
21269
21270/*
21271 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21272 * Return 2 if "p" starts with "s:".
21273 * Return 0 otherwise.
21274 */
21275 static int
21276eval_fname_script(p)
21277 char_u *p;
21278{
21279 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21280 || STRNICMP(p + 1, "SNR>", 4) == 0))
21281 return 5;
21282 if (p[0] == 's' && p[1] == ':')
21283 return 2;
21284 return 0;
21285}
21286
21287/*
21288 * Return TRUE if "p" starts with "<SID>" or "s:".
21289 * Only works if eval_fname_script() returned non-zero for "p"!
21290 */
21291 static int
21292eval_fname_sid(p)
21293 char_u *p;
21294{
21295 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21296}
21297
21298/*
21299 * List the head of the function: "name(arg1, arg2)".
21300 */
21301 static void
21302list_func_head(fp, indent)
21303 ufunc_T *fp;
21304 int indent;
21305{
21306 int j;
21307
21308 msg_start();
21309 if (indent)
21310 MSG_PUTS(" ");
21311 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021312 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 {
21314 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021315 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 }
21317 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021318 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021320 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 {
21322 if (j)
21323 MSG_PUTS(", ");
21324 msg_puts(FUNCARG(fp, j));
21325 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021326 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 {
21328 if (j)
21329 MSG_PUTS(", ");
21330 MSG_PUTS("...");
21331 }
21332 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021333 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021334 if (p_verbose > 0)
21335 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336}
21337
21338/*
21339 * Find a function by name, return pointer to it in ufuncs.
21340 * Return NULL for unknown function.
21341 */
21342 static ufunc_T *
21343find_func(name)
21344 char_u *name;
21345{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021346 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021348 hi = hash_find(&func_hashtab, name);
21349 if (!HASHITEM_EMPTY(hi))
21350 return HI2UF(hi);
21351 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352}
21353
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021354#if defined(EXITFREE) || defined(PROTO)
21355 void
21356free_all_functions()
21357{
21358 hashitem_T *hi;
21359
21360 /* Need to start all over every time, because func_free() may change the
21361 * hash table. */
21362 while (func_hashtab.ht_used > 0)
21363 for (hi = func_hashtab.ht_array; ; ++hi)
21364 if (!HASHITEM_EMPTY(hi))
21365 {
21366 func_free(HI2UF(hi));
21367 break;
21368 }
21369}
21370#endif
21371
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021372/*
21373 * Return TRUE if a function "name" exists.
21374 */
21375 static int
21376function_exists(name)
21377 char_u *name;
21378{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021379 char_u *nm = name;
21380 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021381 int n = FALSE;
21382
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021383 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021384 nm = skipwhite(nm);
21385
21386 /* Only accept "funcname", "funcname ", "funcname (..." and
21387 * "funcname(...", not "funcname!...". */
21388 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021389 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021390 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021391 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021392 else
21393 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021395 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021396 return n;
21397}
21398
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021399/*
21400 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021401 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021402 */
21403 static int
21404builtin_function(name)
21405 char_u *name;
21406{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021407 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21408 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021409}
21410
Bram Moolenaar05159a02005-02-26 23:04:13 +000021411#if defined(FEAT_PROFILE) || defined(PROTO)
21412/*
21413 * Start profiling function "fp".
21414 */
21415 static void
21416func_do_profile(fp)
21417 ufunc_T *fp;
21418{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021419 int len = fp->uf_lines.ga_len;
21420
21421 if (len == 0)
21422 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021423 fp->uf_tm_count = 0;
21424 profile_zero(&fp->uf_tm_self);
21425 profile_zero(&fp->uf_tm_total);
21426 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021427 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021428 if (fp->uf_tml_total == NULL)
21429 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021430 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021431 if (fp->uf_tml_self == NULL)
21432 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021433 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021434 fp->uf_tml_idx = -1;
21435 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21436 || fp->uf_tml_self == NULL)
21437 return; /* out of memory */
21438
21439 fp->uf_profiling = TRUE;
21440}
21441
21442/*
21443 * Dump the profiling results for all functions in file "fd".
21444 */
21445 void
21446func_dump_profile(fd)
21447 FILE *fd;
21448{
21449 hashitem_T *hi;
21450 int todo;
21451 ufunc_T *fp;
21452 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021453 ufunc_T **sorttab;
21454 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021455
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021456 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021457 if (todo == 0)
21458 return; /* nothing to dump */
21459
Bram Moolenaar73830342005-02-28 22:48:19 +000021460 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21461
Bram Moolenaar05159a02005-02-26 23:04:13 +000021462 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21463 {
21464 if (!HASHITEM_EMPTY(hi))
21465 {
21466 --todo;
21467 fp = HI2UF(hi);
21468 if (fp->uf_profiling)
21469 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021470 if (sorttab != NULL)
21471 sorttab[st_len++] = fp;
21472
Bram Moolenaar05159a02005-02-26 23:04:13 +000021473 if (fp->uf_name[0] == K_SPECIAL)
21474 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21475 else
21476 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21477 if (fp->uf_tm_count == 1)
21478 fprintf(fd, "Called 1 time\n");
21479 else
21480 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21481 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21482 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21483 fprintf(fd, "\n");
21484 fprintf(fd, "count total (s) self (s)\n");
21485
21486 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21487 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021488 if (FUNCLINE(fp, i) == NULL)
21489 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021490 prof_func_line(fd, fp->uf_tml_count[i],
21491 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021492 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21493 }
21494 fprintf(fd, "\n");
21495 }
21496 }
21497 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021498
21499 if (sorttab != NULL && st_len > 0)
21500 {
21501 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21502 prof_total_cmp);
21503 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21504 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21505 prof_self_cmp);
21506 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21507 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021508
21509 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021510}
Bram Moolenaar73830342005-02-28 22:48:19 +000021511
21512 static void
21513prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21514 FILE *fd;
21515 ufunc_T **sorttab;
21516 int st_len;
21517 char *title;
21518 int prefer_self; /* when equal print only self time */
21519{
21520 int i;
21521 ufunc_T *fp;
21522
21523 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21524 fprintf(fd, "count total (s) self (s) function\n");
21525 for (i = 0; i < 20 && i < st_len; ++i)
21526 {
21527 fp = sorttab[i];
21528 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21529 prefer_self);
21530 if (fp->uf_name[0] == K_SPECIAL)
21531 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21532 else
21533 fprintf(fd, " %s()\n", fp->uf_name);
21534 }
21535 fprintf(fd, "\n");
21536}
21537
21538/*
21539 * Print the count and times for one function or function line.
21540 */
21541 static void
21542prof_func_line(fd, count, total, self, prefer_self)
21543 FILE *fd;
21544 int count;
21545 proftime_T *total;
21546 proftime_T *self;
21547 int prefer_self; /* when equal print only self time */
21548{
21549 if (count > 0)
21550 {
21551 fprintf(fd, "%5d ", count);
21552 if (prefer_self && profile_equal(total, self))
21553 fprintf(fd, " ");
21554 else
21555 fprintf(fd, "%s ", profile_msg(total));
21556 if (!prefer_self && profile_equal(total, self))
21557 fprintf(fd, " ");
21558 else
21559 fprintf(fd, "%s ", profile_msg(self));
21560 }
21561 else
21562 fprintf(fd, " ");
21563}
21564
21565/*
21566 * Compare function for total time sorting.
21567 */
21568 static int
21569#ifdef __BORLANDC__
21570_RTLENTRYF
21571#endif
21572prof_total_cmp(s1, s2)
21573 const void *s1;
21574 const void *s2;
21575{
21576 ufunc_T *p1, *p2;
21577
21578 p1 = *(ufunc_T **)s1;
21579 p2 = *(ufunc_T **)s2;
21580 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21581}
21582
21583/*
21584 * Compare function for self time sorting.
21585 */
21586 static int
21587#ifdef __BORLANDC__
21588_RTLENTRYF
21589#endif
21590prof_self_cmp(s1, s2)
21591 const void *s1;
21592 const void *s2;
21593{
21594 ufunc_T *p1, *p2;
21595
21596 p1 = *(ufunc_T **)s1;
21597 p2 = *(ufunc_T **)s2;
21598 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21599}
21600
Bram Moolenaar05159a02005-02-26 23:04:13 +000021601#endif
21602
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021603/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021604 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021605 * Return TRUE if a package was loaded.
21606 */
21607 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021608script_autoload(name, reload)
21609 char_u *name;
21610 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021611{
21612 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021613 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021614 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021615 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021616
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021617 /* Return quickly when autoload disabled. */
21618 if (no_autoload)
21619 return FALSE;
21620
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021621 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021622 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021623 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021624 return FALSE;
21625
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021626 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021627
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021628 /* Find the name in the list of previously loaded package names. Skip
21629 * "autoload/", it's always the same. */
21630 for (i = 0; i < ga_loaded.ga_len; ++i)
21631 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21632 break;
21633 if (!reload && i < ga_loaded.ga_len)
21634 ret = FALSE; /* was loaded already */
21635 else
21636 {
21637 /* Remember the name if it wasn't loaded already. */
21638 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21639 {
21640 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21641 tofree = NULL;
21642 }
21643
21644 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021645 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021646 ret = TRUE;
21647 }
21648
21649 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021650 return ret;
21651}
21652
21653/*
21654 * Return the autoload script name for a function or variable name.
21655 * Returns NULL when out of memory.
21656 */
21657 static char_u *
21658autoload_name(name)
21659 char_u *name;
21660{
21661 char_u *p;
21662 char_u *scriptname;
21663
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021664 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021665 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21666 if (scriptname == NULL)
21667 return FALSE;
21668 STRCPY(scriptname, "autoload/");
21669 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021670 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021671 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021672 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021673 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021674 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021675}
21676
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21678
21679/*
21680 * Function given to ExpandGeneric() to obtain the list of user defined
21681 * function names.
21682 */
21683 char_u *
21684get_user_func_name(xp, idx)
21685 expand_T *xp;
21686 int idx;
21687{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021688 static long_u done;
21689 static hashitem_T *hi;
21690 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691
21692 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021694 done = 0;
21695 hi = func_hashtab.ht_array;
21696 }
21697 if (done < func_hashtab.ht_used)
21698 {
21699 if (done++ > 0)
21700 ++hi;
21701 while (HASHITEM_EMPTY(hi))
21702 ++hi;
21703 fp = HI2UF(hi);
21704
21705 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21706 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707
21708 cat_func_name(IObuff, fp);
21709 if (xp->xp_context != EXPAND_USER_FUNC)
21710 {
21711 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021712 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 STRCAT(IObuff, ")");
21714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715 return IObuff;
21716 }
21717 return NULL;
21718}
21719
21720#endif /* FEAT_CMDL_COMPL */
21721
21722/*
21723 * Copy the function name of "fp" to buffer "buf".
21724 * "buf" must be able to hold the function name plus three bytes.
21725 * Takes care of script-local function names.
21726 */
21727 static void
21728cat_func_name(buf, fp)
21729 char_u *buf;
21730 ufunc_T *fp;
21731{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021732 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 {
21734 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021735 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 }
21737 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021738 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739}
21740
21741/*
21742 * ":delfunction {name}"
21743 */
21744 void
21745ex_delfunction(eap)
21746 exarg_T *eap;
21747{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021748 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 char_u *p;
21750 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752
21753 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021754 name = trans_function_name(&p, eap->skip, 0, &fudi);
21755 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021757 {
21758 if (fudi.fd_dict != NULL && !eap->skip)
21759 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 if (!ends_excmd(*skipwhite(p)))
21763 {
21764 vim_free(name);
21765 EMSG(_(e_trailing));
21766 return;
21767 }
21768 eap->nextcmd = check_nextcmd(p);
21769 if (eap->nextcmd != NULL)
21770 *p = NUL;
21771
21772 if (!eap->skip)
21773 fp = find_func(name);
21774 vim_free(name);
21775
21776 if (!eap->skip)
21777 {
21778 if (fp == NULL)
21779 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021780 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 return;
21782 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021783 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784 {
21785 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21786 return;
21787 }
21788
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021789 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021791 /* Delete the dict item that refers to the function, it will
21792 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021793 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021795 else
21796 func_free(fp);
21797 }
21798}
21799
21800/*
21801 * Free a function and remove it from the list of functions.
21802 */
21803 static void
21804func_free(fp)
21805 ufunc_T *fp;
21806{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021807 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021808
21809 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021810 ga_clear_strings(&(fp->uf_args));
21811 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021812#ifdef FEAT_PROFILE
21813 vim_free(fp->uf_tml_count);
21814 vim_free(fp->uf_tml_total);
21815 vim_free(fp->uf_tml_self);
21816#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021817
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021818 /* remove the function from the function hashtable */
21819 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21820 if (HASHITEM_EMPTY(hi))
21821 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021822 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021823 hash_remove(&func_hashtab, hi);
21824
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021825 vim_free(fp);
21826}
21827
21828/*
21829 * Unreference a Function: decrement the reference count and free it when it
21830 * becomes zero. Only for numbered functions.
21831 */
21832 static void
21833func_unref(name)
21834 char_u *name;
21835{
21836 ufunc_T *fp;
21837
21838 if (name != NULL && isdigit(*name))
21839 {
21840 fp = find_func(name);
21841 if (fp == NULL)
21842 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021843 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021844 {
21845 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021846 * when "uf_calls" becomes zero. */
21847 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021848 func_free(fp);
21849 }
21850 }
21851}
21852
21853/*
21854 * Count a reference to a Function.
21855 */
21856 static void
21857func_ref(name)
21858 char_u *name;
21859{
21860 ufunc_T *fp;
21861
21862 if (name != NULL && isdigit(*name))
21863 {
21864 fp = find_func(name);
21865 if (fp == NULL)
21866 EMSG2(_(e_intern2), "func_ref()");
21867 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021868 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 }
21870}
21871
21872/*
21873 * Call a user function.
21874 */
21875 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021876call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 ufunc_T *fp; /* pointer to function */
21878 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021879 typval_T *argvars; /* arguments */
21880 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 linenr_T firstline; /* first line of range */
21882 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021883 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884{
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 char_u *save_sourcing_name;
21886 linenr_T save_sourcing_lnum;
21887 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021888 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021889 int save_did_emsg;
21890 static int depth = 0;
21891 dictitem_T *v;
21892 int fixvar_idx = 0; /* index in fixvar[] */
21893 int i;
21894 int ai;
21895 char_u numbuf[NUMBUFLEN];
21896 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021897#ifdef FEAT_PROFILE
21898 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021899 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901
21902 /* If depth of calling is getting too high, don't execute the function */
21903 if (depth >= p_mfd)
21904 {
21905 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021906 rettv->v_type = VAR_NUMBER;
21907 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 return;
21909 }
21910 ++depth;
21911
21912 line_breakcheck(); /* check for CTRL-C hit */
21913
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021914 fc = (funccall_T *)alloc(sizeof(funccall_T));
21915 fc->caller = current_funccal;
21916 current_funccal = fc;
21917 fc->func = fp;
21918 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021919 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021920 fc->linenr = 0;
21921 fc->returned = FALSE;
21922 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021924 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21925 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021928 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21930 * each argument variable and saves a lot of time.
21931 */
21932 /*
21933 * Init l: variables.
21934 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021935 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021936 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021937 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021938 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21939 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021940 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021941 name = v->di_key;
21942 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021943 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021944 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021945 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021946 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021947 v->di_tv.vval.v_dict = selfdict;
21948 ++selfdict->dv_refcount;
21949 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021950
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 /*
21952 * Init a: variables.
21953 * Set a:0 to "argcount".
21954 * Set a:000 to a list with room for the "..." arguments.
21955 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021956 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21957 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021958 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021959 /* Use "name" to avoid a warning from some compiler that checks the
21960 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021961 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021962 name = v->di_key;
21963 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021964 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021965 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021967 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021968 v->di_tv.vval.v_list = &fc->l_varlist;
21969 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21970 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21971 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021972
21973 /*
21974 * Set a:firstline to "firstline" and a:lastline to "lastline".
21975 * Set a:name to named arguments.
21976 * Set a:N to the "..." arguments.
21977 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021978 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021979 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021980 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021981 (varnumber_T)lastline);
21982 for (i = 0; i < argcount; ++i)
21983 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021984 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021985 if (ai < 0)
21986 /* named argument a:name */
21987 name = FUNCARG(fp, i);
21988 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021989 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 /* "..." argument a:1, a:2, etc. */
21991 sprintf((char *)numbuf, "%d", ai + 1);
21992 name = numbuf;
21993 }
21994 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21995 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021996 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021997 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21998 }
21999 else
22000 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022001 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22002 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022003 if (v == NULL)
22004 break;
22005 v->di_flags = DI_FLAGS_RO;
22006 }
22007 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022008 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022009
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022010 /* Note: the values are copied directly to avoid alloc/free.
22011 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022012 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022013 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022014
22015 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22016 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022017 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22018 fc->l_listitems[ai].li_tv = argvars[i];
22019 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022020 }
22021 }
22022
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 /* Don't redraw while executing the function. */
22024 ++RedrawingDisabled;
22025 save_sourcing_name = sourcing_name;
22026 save_sourcing_lnum = sourcing_lnum;
22027 sourcing_lnum = 1;
22028 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022029 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 if (sourcing_name != NULL)
22031 {
22032 if (save_sourcing_name != NULL
22033 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22034 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22035 else
22036 STRCPY(sourcing_name, "function ");
22037 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22038
22039 if (p_verbose >= 12)
22040 {
22041 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022042 verbose_enter_scroll();
22043
Bram Moolenaar555b2802005-05-19 21:08:39 +000022044 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 if (p_verbose >= 14)
22046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022048 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022049 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022050 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051
22052 msg_puts((char_u *)"(");
22053 for (i = 0; i < argcount; ++i)
22054 {
22055 if (i > 0)
22056 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022057 if (argvars[i].v_type == VAR_NUMBER)
22058 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 else
22060 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022061 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22062 if (s != NULL)
22063 {
22064 trunc_string(s, buf, MSG_BUF_CLEN);
22065 msg_puts(buf);
22066 vim_free(tofree);
22067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 }
22069 }
22070 msg_puts((char_u *)")");
22071 }
22072 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022073
22074 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 --no_wait_return;
22076 }
22077 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022078#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022079 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022080 {
22081 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22082 func_do_profile(fp);
22083 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022084 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022085 {
22086 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022087 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022088 profile_zero(&fp->uf_tm_children);
22089 }
22090 script_prof_save(&wait_start);
22091 }
22092#endif
22093
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022095 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 save_did_emsg = did_emsg;
22097 did_emsg = FALSE;
22098
22099 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022100 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22102
22103 --RedrawingDisabled;
22104
22105 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022106 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022108 clear_tv(rettv);
22109 rettv->v_type = VAR_NUMBER;
22110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 }
22112
Bram Moolenaar05159a02005-02-26 23:04:13 +000022113#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022114 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022115 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022116 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022117 profile_end(&call_start);
22118 profile_sub_wait(&wait_start, &call_start);
22119 profile_add(&fp->uf_tm_total, &call_start);
22120 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022121 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022122 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022123 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22124 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022125 }
22126 }
22127#endif
22128
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 /* when being verbose, mention the return value */
22130 if (p_verbose >= 12)
22131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022133 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022136 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022137 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022138 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022139 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022140 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022142 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022143 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022144 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022145 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022146
Bram Moolenaar555b2802005-05-19 21:08:39 +000022147 /* The value may be very long. Skip the middle part, so that we
22148 * have some idea how it starts and ends. smsg() would always
22149 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022150 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022151 if (s != NULL)
22152 {
22153 trunc_string(s, buf, MSG_BUF_CLEN);
22154 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22155 vim_free(tofree);
22156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 }
22158 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022159
22160 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 --no_wait_return;
22162 }
22163
22164 vim_free(sourcing_name);
22165 sourcing_name = save_sourcing_name;
22166 sourcing_lnum = save_sourcing_lnum;
22167 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022168#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022169 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022170 script_prof_restore(&wait_start);
22171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172
22173 if (p_verbose >= 12 && sourcing_name != NULL)
22174 {
22175 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022176 verbose_enter_scroll();
22177
Bram Moolenaar555b2802005-05-19 21:08:39 +000022178 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022180
22181 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 --no_wait_return;
22183 }
22184
22185 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022186 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022188
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022189 /* If the a:000 list and the l: and a: dicts are not referenced we can
22190 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022191 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22192 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22193 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22194 {
22195 free_funccal(fc, FALSE);
22196 }
22197 else
22198 {
22199 hashitem_T *hi;
22200 listitem_T *li;
22201 int todo;
22202
22203 /* "fc" is still in use. This can happen when returning "a:000" or
22204 * assigning "l:" to a global variable.
22205 * Link "fc" in the list for garbage collection later. */
22206 fc->caller = previous_funccal;
22207 previous_funccal = fc;
22208
22209 /* Make a copy of the a: variables, since we didn't do that above. */
22210 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22211 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22212 {
22213 if (!HASHITEM_EMPTY(hi))
22214 {
22215 --todo;
22216 v = HI2DI(hi);
22217 copy_tv(&v->di_tv, &v->di_tv);
22218 }
22219 }
22220
22221 /* Make a copy of the a:000 items, since we didn't do that above. */
22222 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22223 copy_tv(&li->li_tv, &li->li_tv);
22224 }
22225}
22226
22227/*
22228 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022229 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022230 */
22231 static int
22232can_free_funccal(fc, copyID)
22233 funccall_T *fc;
22234 int copyID;
22235{
22236 return (fc->l_varlist.lv_copyID != copyID
22237 && fc->l_vars.dv_copyID != copyID
22238 && fc->l_avars.dv_copyID != copyID);
22239}
22240
22241/*
22242 * Free "fc" and what it contains.
22243 */
22244 static void
22245free_funccal(fc, free_val)
22246 funccall_T *fc;
22247 int free_val; /* a: vars were allocated */
22248{
22249 listitem_T *li;
22250
22251 /* The a: variables typevals may not have been allocated, only free the
22252 * allocated variables. */
22253 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22254
22255 /* free all l: variables */
22256 vars_clear(&fc->l_vars.dv_hashtab);
22257
22258 /* Free the a:000 variables if they were allocated. */
22259 if (free_val)
22260 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22261 clear_tv(&li->li_tv);
22262
22263 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264}
22265
22266/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022267 * Add a number variable "name" to dict "dp" with value "nr".
22268 */
22269 static void
22270add_nr_var(dp, v, name, nr)
22271 dict_T *dp;
22272 dictitem_T *v;
22273 char *name;
22274 varnumber_T nr;
22275{
22276 STRCPY(v->di_key, name);
22277 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22278 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22279 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022280 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022281 v->di_tv.vval.v_number = nr;
22282}
22283
22284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 * ":return [expr]"
22286 */
22287 void
22288ex_return(eap)
22289 exarg_T *eap;
22290{
22291 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022292 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 int returning = FALSE;
22294
22295 if (current_funccal == NULL)
22296 {
22297 EMSG(_("E133: :return not inside a function"));
22298 return;
22299 }
22300
22301 if (eap->skip)
22302 ++emsg_skip;
22303
22304 eap->nextcmd = NULL;
22305 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022306 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 {
22308 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022309 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022311 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 }
22313 /* It's safer to return also on error. */
22314 else if (!eap->skip)
22315 {
22316 /*
22317 * Return unless the expression evaluation has been cancelled due to an
22318 * aborting error, an interrupt, or an exception.
22319 */
22320 if (!aborting())
22321 returning = do_return(eap, FALSE, TRUE, NULL);
22322 }
22323
22324 /* When skipping or the return gets pending, advance to the next command
22325 * in this line (!returning). Otherwise, ignore the rest of the line.
22326 * Following lines will be ignored by get_func_line(). */
22327 if (returning)
22328 eap->nextcmd = NULL;
22329 else if (eap->nextcmd == NULL) /* no argument */
22330 eap->nextcmd = check_nextcmd(arg);
22331
22332 if (eap->skip)
22333 --emsg_skip;
22334}
22335
22336/*
22337 * Return from a function. Possibly makes the return pending. Also called
22338 * for a pending return at the ":endtry" or after returning from an extra
22339 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022340 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022341 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 * FALSE when the return gets pending.
22343 */
22344 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022345do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 exarg_T *eap;
22347 int reanimate;
22348 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022349 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350{
22351 int idx;
22352 struct condstack *cstack = eap->cstack;
22353
22354 if (reanimate)
22355 /* Undo the return. */
22356 current_funccal->returned = FALSE;
22357
22358 /*
22359 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22360 * not in its finally clause (which then is to be executed next) is found.
22361 * In this case, make the ":return" pending for execution at the ":endtry".
22362 * Otherwise, return normally.
22363 */
22364 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22365 if (idx >= 0)
22366 {
22367 cstack->cs_pending[idx] = CSTP_RETURN;
22368
22369 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022370 /* A pending return again gets pending. "rettv" points to an
22371 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022373 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 else
22375 {
22376 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022377 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022379 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022381 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 {
22383 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022384 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022385 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 else
22387 EMSG(_(e_outofmem));
22388 }
22389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022390 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391
22392 if (reanimate)
22393 {
22394 /* The pending return value could be overwritten by a ":return"
22395 * without argument in a finally clause; reset the default
22396 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022397 current_funccal->rettv->v_type = VAR_NUMBER;
22398 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 }
22400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022401 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 }
22403 else
22404 {
22405 current_funccal->returned = TRUE;
22406
22407 /* If the return is carried out now, store the return value. For
22408 * a return immediately after reanimation, the value is already
22409 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022410 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022412 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022413 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022415 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 }
22417 }
22418
22419 return idx < 0;
22420}
22421
22422/*
22423 * Free the variable with a pending return value.
22424 */
22425 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022426discard_pending_return(rettv)
22427 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428{
Bram Moolenaar33570922005-01-25 22:26:29 +000022429 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430}
22431
22432/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022433 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 * is an allocated string. Used by report_pending() for verbose messages.
22435 */
22436 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022437get_return_cmd(rettv)
22438 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022440 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022441 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022442 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022444 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022445 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022446 if (s == NULL)
22447 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022448
22449 STRCPY(IObuff, ":return ");
22450 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22451 if (STRLEN(s) + 8 >= IOSIZE)
22452 STRCPY(IObuff + IOSIZE - 4, "...");
22453 vim_free(tofree);
22454 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455}
22456
22457/*
22458 * Get next function line.
22459 * Called by do_cmdline() to get the next line.
22460 * Returns allocated string, or NULL for end of function.
22461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 char_u *
22463get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022464 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022466 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467{
Bram Moolenaar33570922005-01-25 22:26:29 +000022468 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022469 ufunc_T *fp = fcp->func;
22470 char_u *retval;
22471 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472
22473 /* If breakpoints have been added/deleted need to check for it. */
22474 if (fcp->dbg_tick != debug_tick)
22475 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022476 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 sourcing_lnum);
22478 fcp->dbg_tick = debug_tick;
22479 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022480#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022481 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022482 func_line_end(cookie);
22483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484
Bram Moolenaar05159a02005-02-26 23:04:13 +000022485 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022486 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22487 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 retval = NULL;
22489 else
22490 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022491 /* Skip NULL lines (continuation lines). */
22492 while (fcp->linenr < gap->ga_len
22493 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22494 ++fcp->linenr;
22495 if (fcp->linenr >= gap->ga_len)
22496 retval = NULL;
22497 else
22498 {
22499 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22500 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022501#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022502 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022503 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022504#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 }
22507
22508 /* Did we encounter a breakpoint? */
22509 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22510 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022511 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022513 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514 sourcing_lnum);
22515 fcp->dbg_tick = debug_tick;
22516 }
22517
22518 return retval;
22519}
22520
Bram Moolenaar05159a02005-02-26 23:04:13 +000022521#if defined(FEAT_PROFILE) || defined(PROTO)
22522/*
22523 * Called when starting to read a function line.
22524 * "sourcing_lnum" must be correct!
22525 * When skipping lines it may not actually be executed, but we won't find out
22526 * until later and we need to store the time now.
22527 */
22528 void
22529func_line_start(cookie)
22530 void *cookie;
22531{
22532 funccall_T *fcp = (funccall_T *)cookie;
22533 ufunc_T *fp = fcp->func;
22534
22535 if (fp->uf_profiling && sourcing_lnum >= 1
22536 && sourcing_lnum <= fp->uf_lines.ga_len)
22537 {
22538 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022539 /* Skip continuation lines. */
22540 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22541 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022542 fp->uf_tml_execed = FALSE;
22543 profile_start(&fp->uf_tml_start);
22544 profile_zero(&fp->uf_tml_children);
22545 profile_get_wait(&fp->uf_tml_wait);
22546 }
22547}
22548
22549/*
22550 * Called when actually executing a function line.
22551 */
22552 void
22553func_line_exec(cookie)
22554 void *cookie;
22555{
22556 funccall_T *fcp = (funccall_T *)cookie;
22557 ufunc_T *fp = fcp->func;
22558
22559 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22560 fp->uf_tml_execed = TRUE;
22561}
22562
22563/*
22564 * Called when done with a function line.
22565 */
22566 void
22567func_line_end(cookie)
22568 void *cookie;
22569{
22570 funccall_T *fcp = (funccall_T *)cookie;
22571 ufunc_T *fp = fcp->func;
22572
22573 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22574 {
22575 if (fp->uf_tml_execed)
22576 {
22577 ++fp->uf_tml_count[fp->uf_tml_idx];
22578 profile_end(&fp->uf_tml_start);
22579 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022580 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022581 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22582 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022583 }
22584 fp->uf_tml_idx = -1;
22585 }
22586}
22587#endif
22588
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589/*
22590 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022591 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 */
22593 int
22594func_has_ended(cookie)
22595 void *cookie;
22596{
Bram Moolenaar33570922005-01-25 22:26:29 +000022597 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598
22599 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22600 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022601 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 || fcp->returned);
22603}
22604
22605/*
22606 * return TRUE if cookie indicates a function which "abort"s on errors.
22607 */
22608 int
22609func_has_abort(cookie)
22610 void *cookie;
22611{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022612 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613}
22614
22615#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22616typedef enum
22617{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022618 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22619 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22620 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621} var_flavour_T;
22622
22623static var_flavour_T var_flavour __ARGS((char_u *varname));
22624
22625 static var_flavour_T
22626var_flavour(varname)
22627 char_u *varname;
22628{
22629 char_u *p = varname;
22630
22631 if (ASCII_ISUPPER(*p))
22632 {
22633 while (*(++p))
22634 if (ASCII_ISLOWER(*p))
22635 return VAR_FLAVOUR_SESSION;
22636 return VAR_FLAVOUR_VIMINFO;
22637 }
22638 else
22639 return VAR_FLAVOUR_DEFAULT;
22640}
22641#endif
22642
22643#if defined(FEAT_VIMINFO) || defined(PROTO)
22644/*
22645 * Restore global vars that start with a capital from the viminfo file
22646 */
22647 int
22648read_viminfo_varlist(virp, writing)
22649 vir_T *virp;
22650 int writing;
22651{
22652 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022653 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022654 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655
22656 if (!writing && (find_viminfo_parameter('!') != NULL))
22657 {
22658 tab = vim_strchr(virp->vir_line + 1, '\t');
22659 if (tab != NULL)
22660 {
22661 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022662 switch (*tab)
22663 {
22664 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022665#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022666 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022667#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022668 case 'D': type = VAR_DICT; break;
22669 case 'L': type = VAR_LIST; break;
22670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671
22672 tab = vim_strchr(tab, '\t');
22673 if (tab != NULL)
22674 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022675 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022676 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022677 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022679#ifdef FEAT_FLOAT
22680 else if (type == VAR_FLOAT)
22681 (void)string2float(tab + 1, &tv.vval.v_float);
22682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022684 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022685 if (type == VAR_DICT || type == VAR_LIST)
22686 {
22687 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22688
22689 if (etv == NULL)
22690 /* Failed to parse back the dict or list, use it as a
22691 * string. */
22692 tv.v_type = VAR_STRING;
22693 else
22694 {
22695 vim_free(tv.vval.v_string);
22696 tv = *etv;
22697 }
22698 }
22699
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022700 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022701
22702 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022703 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022704 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22705 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 }
22707 }
22708 }
22709
22710 return viminfo_readline(virp);
22711}
22712
22713/*
22714 * Write global vars that start with a capital to the viminfo file
22715 */
22716 void
22717write_viminfo_varlist(fp)
22718 FILE *fp;
22719{
Bram Moolenaar33570922005-01-25 22:26:29 +000022720 hashitem_T *hi;
22721 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022722 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022723 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022724 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022725 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022726 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727
22728 if (find_viminfo_parameter('!') == NULL)
22729 return;
22730
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022731 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022732
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022733 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022734 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022736 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022738 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 this_var = HI2DI(hi);
22740 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022741 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022742 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022743 {
22744 case VAR_STRING: s = "STR"; break;
22745 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022746#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022747 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022748#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022749 case VAR_DICT: s = "DIC"; break;
22750 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022751 default: continue;
22752 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022754 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022755 if (p != NULL)
22756 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022757 vim_free(tofree);
22758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 }
22760 }
22761}
22762#endif
22763
22764#if defined(FEAT_SESSION) || defined(PROTO)
22765 int
22766store_session_globals(fd)
22767 FILE *fd;
22768{
Bram Moolenaar33570922005-01-25 22:26:29 +000022769 hashitem_T *hi;
22770 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022771 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 char_u *p, *t;
22773
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022774 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022777 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022779 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022780 this_var = HI2DI(hi);
22781 if ((this_var->di_tv.v_type == VAR_NUMBER
22782 || this_var->di_tv.v_type == VAR_STRING)
22783 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022784 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022785 /* Escape special characters with a backslash. Turn a LF and
22786 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022788 (char_u *)"\\\"\n\r");
22789 if (p == NULL) /* out of memory */
22790 break;
22791 for (t = p; *t != NUL; ++t)
22792 if (*t == '\n')
22793 *t = 'n';
22794 else if (*t == '\r')
22795 *t = 'r';
22796 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022797 this_var->di_key,
22798 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22799 : ' ',
22800 p,
22801 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22802 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022803 || put_eol(fd) == FAIL)
22804 {
22805 vim_free(p);
22806 return FAIL;
22807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 vim_free(p);
22809 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022810#ifdef FEAT_FLOAT
22811 else if (this_var->di_tv.v_type == VAR_FLOAT
22812 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22813 {
22814 float_T f = this_var->di_tv.vval.v_float;
22815 int sign = ' ';
22816
22817 if (f < 0)
22818 {
22819 f = -f;
22820 sign = '-';
22821 }
22822 if ((fprintf(fd, "let %s = %c&%f",
22823 this_var->di_key, sign, f) < 0)
22824 || put_eol(fd) == FAIL)
22825 return FAIL;
22826 }
22827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 }
22829 }
22830 return OK;
22831}
22832#endif
22833
Bram Moolenaar661b1822005-07-28 22:36:45 +000022834/*
22835 * Display script name where an item was last set.
22836 * Should only be invoked when 'verbose' is non-zero.
22837 */
22838 void
22839last_set_msg(scriptID)
22840 scid_T scriptID;
22841{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022842 char_u *p;
22843
Bram Moolenaar661b1822005-07-28 22:36:45 +000022844 if (scriptID != 0)
22845 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022846 p = home_replace_save(NULL, get_scriptname(scriptID));
22847 if (p != NULL)
22848 {
22849 verbose_enter();
22850 MSG_PUTS(_("\n\tLast set from "));
22851 MSG_PUTS(p);
22852 vim_free(p);
22853 verbose_leave();
22854 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022855 }
22856}
22857
Bram Moolenaard812df62008-11-09 12:46:09 +000022858/*
22859 * List v:oldfiles in a nice way.
22860 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022861 void
22862ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022863 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022864{
22865 list_T *l = vimvars[VV_OLDFILES].vv_list;
22866 listitem_T *li;
22867 int nr = 0;
22868
22869 if (l == NULL)
22870 msg((char_u *)_("No old files"));
22871 else
22872 {
22873 msg_start();
22874 msg_scroll = TRUE;
22875 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22876 {
22877 msg_outnum((long)++nr);
22878 MSG_PUTS(": ");
22879 msg_outtrans(get_tv_string(&li->li_tv));
22880 msg_putchar('\n');
22881 out_flush(); /* output one line at a time */
22882 ui_breakcheck();
22883 }
22884 /* Assume "got_int" was set to truncate the listing. */
22885 got_int = FALSE;
22886
22887#ifdef FEAT_BROWSE_CMD
22888 if (cmdmod.browse)
22889 {
22890 quit_more = FALSE;
22891 nr = prompt_for_number(FALSE);
22892 msg_starthere();
22893 if (nr > 0)
22894 {
22895 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22896 (long)nr);
22897
22898 if (p != NULL)
22899 {
22900 p = expand_env_save(p);
22901 eap->arg = p;
22902 eap->cmdidx = CMD_edit;
22903 cmdmod.browse = FALSE;
22904 do_exedit(eap, NULL);
22905 vim_free(p);
22906 }
22907 }
22908 }
22909#endif
22910 }
22911}
22912
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913#endif /* FEAT_EVAL */
22914
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022916#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917
22918#ifdef WIN3264
22919/*
22920 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22921 */
22922static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22923static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22924static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22925
22926/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022927 * Get the short path (8.3) for the filename in "fnamep".
22928 * Only works for a valid file name.
22929 * When the path gets longer "fnamep" is changed and the allocated buffer
22930 * is put in "bufp".
22931 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22932 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 */
22934 static int
22935get_short_pathname(fnamep, bufp, fnamelen)
22936 char_u **fnamep;
22937 char_u **bufp;
22938 int *fnamelen;
22939{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022940 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 char_u *newbuf;
22942
22943 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 l = GetShortPathName(*fnamep, *fnamep, len);
22945 if (l > len - 1)
22946 {
22947 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022948 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 newbuf = vim_strnsave(*fnamep, l);
22950 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952
22953 vim_free(*bufp);
22954 *fnamep = *bufp = newbuf;
22955
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022956 /* Really should always succeed, as the buffer is big enough. */
22957 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 }
22959
22960 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022961 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962}
22963
22964/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022965 * Get the short path (8.3) for the filename in "fname". The converted
22966 * path is returned in "bufp".
22967 *
22968 * Some of the directories specified in "fname" may not exist. This function
22969 * will shorten the existing directories at the beginning of the path and then
22970 * append the remaining non-existing path.
22971 *
22972 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022973 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022974 * bufp - Pointer to an allocated buffer for the filename.
22975 * fnamelen - Length of the filename pointed to by fname
22976 *
22977 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 */
22979 static int
22980shortpath_for_invalid_fname(fname, bufp, fnamelen)
22981 char_u **fname;
22982 char_u **bufp;
22983 int *fnamelen;
22984{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022985 char_u *short_fname, *save_fname, *pbuf_unused;
22986 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022988 int old_len, len;
22989 int new_len, sfx_len;
22990 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022991
22992 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022993 old_len = *fnamelen;
22994 save_fname = vim_strnsave(*fname, old_len);
22995 pbuf_unused = NULL;
22996 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022998 endp = save_fname + old_len - 1; /* Find the end of the copy */
22999 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023001 /*
23002 * Try shortening the supplied path till it succeeds by removing one
23003 * directory at a time from the tail of the path.
23004 */
23005 len = 0;
23006 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023008 /* go back one path-separator */
23009 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23010 --endp;
23011 if (endp <= save_fname)
23012 break; /* processed the complete path */
23013
23014 /*
23015 * Replace the path separator with a NUL and try to shorten the
23016 * resulting path.
23017 */
23018 ch = *endp;
23019 *endp = 0;
23020 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023021 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023022 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23023 {
23024 retval = FAIL;
23025 goto theend;
23026 }
23027 *endp = ch; /* preserve the string */
23028
23029 if (len > 0)
23030 break; /* successfully shortened the path */
23031
23032 /* failed to shorten the path. Skip the path separator */
23033 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 }
23035
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023036 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023038 /*
23039 * Succeeded in shortening the path. Now concatenate the shortened
23040 * path with the remaining path at the tail.
23041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023043 /* Compute the length of the new path. */
23044 sfx_len = (int)(save_endp - endp) + 1;
23045 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023047 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023049 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023051 /* There is not enough space in the currently allocated string,
23052 * copy it to a buffer big enough. */
23053 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023055 {
23056 retval = FAIL;
23057 goto theend;
23058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 }
23060 else
23061 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023062 /* Transfer short_fname to the main buffer (it's big enough),
23063 * unless get_short_pathname() did its work in-place. */
23064 *fname = *bufp = save_fname;
23065 if (short_fname != save_fname)
23066 vim_strncpy(save_fname, short_fname, len);
23067 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023069
23070 /* concat the not-shortened part of the path */
23071 vim_strncpy(*fname + len, endp, sfx_len);
23072 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023074
23075theend:
23076 vim_free(pbuf_unused);
23077 vim_free(save_fname);
23078
23079 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080}
23081
23082/*
23083 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023084 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 */
23086 static int
23087shortpath_for_partial(fnamep, bufp, fnamelen)
23088 char_u **fnamep;
23089 char_u **bufp;
23090 int *fnamelen;
23091{
23092 int sepcount, len, tflen;
23093 char_u *p;
23094 char_u *pbuf, *tfname;
23095 int hasTilde;
23096
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023097 /* Count up the path separators from the RHS.. so we know which part
23098 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023100 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 if (vim_ispathsep(*p))
23102 ++sepcount;
23103
23104 /* Need full path first (use expand_env() to remove a "~/") */
23105 hasTilde = (**fnamep == '~');
23106 if (hasTilde)
23107 pbuf = tfname = expand_env_save(*fnamep);
23108 else
23109 pbuf = tfname = FullName_save(*fnamep, FALSE);
23110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023111 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023113 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23114 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115
23116 if (len == 0)
23117 {
23118 /* Don't have a valid filename, so shorten the rest of the
23119 * path if we can. This CAN give us invalid 8.3 filenames, but
23120 * there's not a lot of point in guessing what it might be.
23121 */
23122 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023123 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23124 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125 }
23126
23127 /* Count the paths backward to find the beginning of the desired string. */
23128 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023129 {
23130#ifdef FEAT_MBYTE
23131 if (has_mbyte)
23132 p -= mb_head_off(tfname, p);
23133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 if (vim_ispathsep(*p))
23135 {
23136 if (sepcount == 0 || (hasTilde && sepcount == 1))
23137 break;
23138 else
23139 sepcount --;
23140 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 if (hasTilde)
23143 {
23144 --p;
23145 if (p >= tfname)
23146 *p = '~';
23147 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023148 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 }
23150 else
23151 ++p;
23152
23153 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23154 vim_free(*bufp);
23155 *fnamelen = (int)STRLEN(p);
23156 *bufp = pbuf;
23157 *fnamep = p;
23158
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023159 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160}
23161#endif /* WIN3264 */
23162
23163/*
23164 * Adjust a filename, according to a string of modifiers.
23165 * *fnamep must be NUL terminated when called. When returning, the length is
23166 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023167 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 * When there is an error, *fnamep is set to NULL.
23169 */
23170 int
23171modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23172 char_u *src; /* string with modifiers */
23173 int *usedlen; /* characters after src that are used */
23174 char_u **fnamep; /* file name so far */
23175 char_u **bufp; /* buffer for allocated file name or NULL */
23176 int *fnamelen; /* length of fnamep */
23177{
23178 int valid = 0;
23179 char_u *tail;
23180 char_u *s, *p, *pbuf;
23181 char_u dirname[MAXPATHL];
23182 int c;
23183 int has_fullname = 0;
23184#ifdef WIN3264
23185 int has_shortname = 0;
23186#endif
23187
23188repeat:
23189 /* ":p" - full path/file_name */
23190 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23191 {
23192 has_fullname = 1;
23193
23194 valid |= VALID_PATH;
23195 *usedlen += 2;
23196
23197 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23198 if ((*fnamep)[0] == '~'
23199#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23200 && ((*fnamep)[1] == '/'
23201# ifdef BACKSLASH_IN_FILENAME
23202 || (*fnamep)[1] == '\\'
23203# endif
23204 || (*fnamep)[1] == NUL)
23205
23206#endif
23207 )
23208 {
23209 *fnamep = expand_env_save(*fnamep);
23210 vim_free(*bufp); /* free any allocated file name */
23211 *bufp = *fnamep;
23212 if (*fnamep == NULL)
23213 return -1;
23214 }
23215
23216 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023217 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 {
23219 if (vim_ispathsep(*p)
23220 && p[1] == '.'
23221 && (p[2] == NUL
23222 || vim_ispathsep(p[2])
23223 || (p[2] == '.'
23224 && (p[3] == NUL || vim_ispathsep(p[3])))))
23225 break;
23226 }
23227
23228 /* FullName_save() is slow, don't use it when not needed. */
23229 if (*p != NUL || !vim_isAbsName(*fnamep))
23230 {
23231 *fnamep = FullName_save(*fnamep, *p != NUL);
23232 vim_free(*bufp); /* free any allocated file name */
23233 *bufp = *fnamep;
23234 if (*fnamep == NULL)
23235 return -1;
23236 }
23237
23238 /* Append a path separator to a directory. */
23239 if (mch_isdir(*fnamep))
23240 {
23241 /* Make room for one or two extra characters. */
23242 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23243 vim_free(*bufp); /* free any allocated file name */
23244 *bufp = *fnamep;
23245 if (*fnamep == NULL)
23246 return -1;
23247 add_pathsep(*fnamep);
23248 }
23249 }
23250
23251 /* ":." - path relative to the current directory */
23252 /* ":~" - path relative to the home directory */
23253 /* ":8" - shortname path - postponed till after */
23254 while (src[*usedlen] == ':'
23255 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23256 {
23257 *usedlen += 2;
23258 if (c == '8')
23259 {
23260#ifdef WIN3264
23261 has_shortname = 1; /* Postpone this. */
23262#endif
23263 continue;
23264 }
23265 pbuf = NULL;
23266 /* Need full path first (use expand_env() to remove a "~/") */
23267 if (!has_fullname)
23268 {
23269 if (c == '.' && **fnamep == '~')
23270 p = pbuf = expand_env_save(*fnamep);
23271 else
23272 p = pbuf = FullName_save(*fnamep, FALSE);
23273 }
23274 else
23275 p = *fnamep;
23276
23277 has_fullname = 0;
23278
23279 if (p != NULL)
23280 {
23281 if (c == '.')
23282 {
23283 mch_dirname(dirname, MAXPATHL);
23284 s = shorten_fname(p, dirname);
23285 if (s != NULL)
23286 {
23287 *fnamep = s;
23288 if (pbuf != NULL)
23289 {
23290 vim_free(*bufp); /* free any allocated file name */
23291 *bufp = pbuf;
23292 pbuf = NULL;
23293 }
23294 }
23295 }
23296 else
23297 {
23298 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23299 /* Only replace it when it starts with '~' */
23300 if (*dirname == '~')
23301 {
23302 s = vim_strsave(dirname);
23303 if (s != NULL)
23304 {
23305 *fnamep = s;
23306 vim_free(*bufp);
23307 *bufp = s;
23308 }
23309 }
23310 }
23311 vim_free(pbuf);
23312 }
23313 }
23314
23315 tail = gettail(*fnamep);
23316 *fnamelen = (int)STRLEN(*fnamep);
23317
23318 /* ":h" - head, remove "/file_name", can be repeated */
23319 /* Don't remove the first "/" or "c:\" */
23320 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23321 {
23322 valid |= VALID_HEAD;
23323 *usedlen += 2;
23324 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023325 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023326 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 *fnamelen = (int)(tail - *fnamep);
23328#ifdef VMS
23329 if (*fnamelen > 0)
23330 *fnamelen += 1; /* the path separator is part of the path */
23331#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023332 if (*fnamelen == 0)
23333 {
23334 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23335 p = vim_strsave((char_u *)".");
23336 if (p == NULL)
23337 return -1;
23338 vim_free(*bufp);
23339 *bufp = *fnamep = tail = p;
23340 *fnamelen = 1;
23341 }
23342 else
23343 {
23344 while (tail > s && !after_pathsep(s, tail))
23345 mb_ptr_back(*fnamep, tail);
23346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347 }
23348
23349 /* ":8" - shortname */
23350 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23351 {
23352 *usedlen += 2;
23353#ifdef WIN3264
23354 has_shortname = 1;
23355#endif
23356 }
23357
23358#ifdef WIN3264
23359 /* Check shortname after we have done 'heads' and before we do 'tails'
23360 */
23361 if (has_shortname)
23362 {
23363 pbuf = NULL;
23364 /* Copy the string if it is shortened by :h */
23365 if (*fnamelen < (int)STRLEN(*fnamep))
23366 {
23367 p = vim_strnsave(*fnamep, *fnamelen);
23368 if (p == 0)
23369 return -1;
23370 vim_free(*bufp);
23371 *bufp = *fnamep = p;
23372 }
23373
23374 /* Split into two implementations - makes it easier. First is where
23375 * there isn't a full name already, second is where there is.
23376 */
23377 if (!has_fullname && !vim_isAbsName(*fnamep))
23378 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023379 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 return -1;
23381 }
23382 else
23383 {
23384 int l;
23385
23386 /* Simple case, already have the full-name
23387 * Nearly always shorter, so try first time. */
23388 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023389 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 return -1;
23391
23392 if (l == 0)
23393 {
23394 /* Couldn't find the filename.. search the paths.
23395 */
23396 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023397 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 return -1;
23399 }
23400 *fnamelen = l;
23401 }
23402 }
23403#endif /* WIN3264 */
23404
23405 /* ":t" - tail, just the basename */
23406 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23407 {
23408 *usedlen += 2;
23409 *fnamelen -= (int)(tail - *fnamep);
23410 *fnamep = tail;
23411 }
23412
23413 /* ":e" - extension, can be repeated */
23414 /* ":r" - root, without extension, can be repeated */
23415 while (src[*usedlen] == ':'
23416 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23417 {
23418 /* find a '.' in the tail:
23419 * - for second :e: before the current fname
23420 * - otherwise: The last '.'
23421 */
23422 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23423 s = *fnamep - 2;
23424 else
23425 s = *fnamep + *fnamelen - 1;
23426 for ( ; s > tail; --s)
23427 if (s[0] == '.')
23428 break;
23429 if (src[*usedlen + 1] == 'e') /* :e */
23430 {
23431 if (s > tail)
23432 {
23433 *fnamelen += (int)(*fnamep - (s + 1));
23434 *fnamep = s + 1;
23435#ifdef VMS
23436 /* cut version from the extension */
23437 s = *fnamep + *fnamelen - 1;
23438 for ( ; s > *fnamep; --s)
23439 if (s[0] == ';')
23440 break;
23441 if (s > *fnamep)
23442 *fnamelen = s - *fnamep;
23443#endif
23444 }
23445 else if (*fnamep <= tail)
23446 *fnamelen = 0;
23447 }
23448 else /* :r */
23449 {
23450 if (s > tail) /* remove one extension */
23451 *fnamelen = (int)(s - *fnamep);
23452 }
23453 *usedlen += 2;
23454 }
23455
23456 /* ":s?pat?foo?" - substitute */
23457 /* ":gs?pat?foo?" - global substitute */
23458 if (src[*usedlen] == ':'
23459 && (src[*usedlen + 1] == 's'
23460 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23461 {
23462 char_u *str;
23463 char_u *pat;
23464 char_u *sub;
23465 int sep;
23466 char_u *flags;
23467 int didit = FALSE;
23468
23469 flags = (char_u *)"";
23470 s = src + *usedlen + 2;
23471 if (src[*usedlen + 1] == 'g')
23472 {
23473 flags = (char_u *)"g";
23474 ++s;
23475 }
23476
23477 sep = *s++;
23478 if (sep)
23479 {
23480 /* find end of pattern */
23481 p = vim_strchr(s, sep);
23482 if (p != NULL)
23483 {
23484 pat = vim_strnsave(s, (int)(p - s));
23485 if (pat != NULL)
23486 {
23487 s = p + 1;
23488 /* find end of substitution */
23489 p = vim_strchr(s, sep);
23490 if (p != NULL)
23491 {
23492 sub = vim_strnsave(s, (int)(p - s));
23493 str = vim_strnsave(*fnamep, *fnamelen);
23494 if (sub != NULL && str != NULL)
23495 {
23496 *usedlen = (int)(p + 1 - src);
23497 s = do_string_sub(str, pat, sub, flags);
23498 if (s != NULL)
23499 {
23500 *fnamep = s;
23501 *fnamelen = (int)STRLEN(s);
23502 vim_free(*bufp);
23503 *bufp = s;
23504 didit = TRUE;
23505 }
23506 }
23507 vim_free(sub);
23508 vim_free(str);
23509 }
23510 vim_free(pat);
23511 }
23512 }
23513 /* after using ":s", repeat all the modifiers */
23514 if (didit)
23515 goto repeat;
23516 }
23517 }
23518
23519 return valid;
23520}
23521
23522/*
23523 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23524 * "flags" can be "g" to do a global substitute.
23525 * Returns an allocated string, NULL for error.
23526 */
23527 char_u *
23528do_string_sub(str, pat, sub, flags)
23529 char_u *str;
23530 char_u *pat;
23531 char_u *sub;
23532 char_u *flags;
23533{
23534 int sublen;
23535 regmatch_T regmatch;
23536 int i;
23537 int do_all;
23538 char_u *tail;
23539 garray_T ga;
23540 char_u *ret;
23541 char_u *save_cpo;
23542
23543 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23544 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023545 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546
23547 ga_init2(&ga, 1, 200);
23548
23549 do_all = (flags[0] == 'g');
23550
23551 regmatch.rm_ic = p_ic;
23552 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23553 if (regmatch.regprog != NULL)
23554 {
23555 tail = str;
23556 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23557 {
23558 /*
23559 * Get some space for a temporary buffer to do the substitution
23560 * into. It will contain:
23561 * - The text up to where the match is.
23562 * - The substituted text.
23563 * - The text after the match.
23564 */
23565 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23566 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23567 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23568 {
23569 ga_clear(&ga);
23570 break;
23571 }
23572
23573 /* copy the text up to where the match is */
23574 i = (int)(regmatch.startp[0] - tail);
23575 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23576 /* add the substituted text */
23577 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23578 + ga.ga_len + i, TRUE, TRUE, FALSE);
23579 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 /* avoid getting stuck on a match with an empty string */
23581 if (tail == regmatch.endp[0])
23582 {
23583 if (*tail == NUL)
23584 break;
23585 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23586 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 }
23588 else
23589 {
23590 tail = regmatch.endp[0];
23591 if (*tail == NUL)
23592 break;
23593 }
23594 if (!do_all)
23595 break;
23596 }
23597
23598 if (ga.ga_data != NULL)
23599 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23600
23601 vim_free(regmatch.regprog);
23602 }
23603
23604 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23605 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023606 if (p_cpo == empty_option)
23607 p_cpo = save_cpo;
23608 else
23609 /* Darn, evaluating {sub} expression changed the value. */
23610 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611
23612 return ret;
23613}
23614
23615#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */