blob: f9db2f9ffeb483d6fb0a8843237d265f75ec9056 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000116/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000117 * All user-defined global variables are stored in dictionary "globvardict".
118 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120static dict_T globvardict;
121static dictitem_T globvars_var;
122#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
124/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000125 * Old Vim variables such as "v:version" are also available without the "v:".
126 * Also in functions. We need a special hashtable for them.
127 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000128static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200130/* When using exists() don't auto-load a script. */
131static int no_autoload = FALSE;
132
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000134 * When recursively copying lists and dicts we need to remember which ones we
135 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000136 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 */
138static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139#define COPYID_INC 2
140#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000141
142/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000143 * Array to hold the hashtab with variables local to each sourced script.
144 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000146typedef struct
147{
148 dictitem_T sv_var;
149 dict_T sv_dict;
150} scriptvar_T;
151
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200152static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
153#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
154#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156static int echo_attr = 0; /* attributes used for ":echo" */
157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000158/* Values for trans_function_name() argument: */
159#define TFN_INT 1 /* internal function name OK */
160#define TFN_QUIET 2 /* no error messages */
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000363};
364
365/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_type vv_di.di_tv.v_type
367#define vv_nr vv_di.di_tv.vval.v_number
368#define vv_float vv_di.di_tv.vval.v_float
369#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000370#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000372
373/*
374 * The v: variables are stored in dictionary "vimvardict".
375 * "vimvars_var" is the variable that is used for the "l:" scope.
376 */
377static dict_T vimvardict;
378static dictitem_T vimvars_var;
379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
403static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
404static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
405static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
406static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
407static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
408static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
409static void item_lock __ARGS((typval_T *tv, int deep, int lock));
410static int tv_islocked __ARGS((typval_T *tv));
411
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
413static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000418static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
419static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000421static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000426static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100429static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
430static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
431static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000432static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000434static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
436static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000437static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100439static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000441static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200442static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000443static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
458static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
459static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200460static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200477static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200507static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
512static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000566static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000567static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200570static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000571static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000579static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000593static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100598static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200613static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200616#ifdef FEAT_LUA
617static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
618#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000623static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000624static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000627static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000631#ifdef vim_mkdir
632static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100635#ifdef FEAT_MZSCHEME
636static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
637#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100640static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000641static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000642#ifdef FEAT_FLOAT
643static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000646static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000647static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200648#ifdef FEAT_PYTHON3
649static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
651#ifdef FEAT_PYTHON
652static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000656static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
669static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
670#endif
Bram 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 Moolenaard6e256c2011-12-14 15:32:50 +0100758static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000760static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000761static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static int get_env_len __ARGS((char_u **arg));
763static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000765static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
766#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
767#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
768 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000769static 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 +0000770static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
773static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static typval_T *alloc_tv __ARGS((void));
775static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static void init_tv __ARGS((typval_T *varp));
777static long get_tv_number __ARGS((typval_T *varp));
778static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000779static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static char_u *get_tv_string __ARGS((typval_T *varp));
781static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000782static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
786static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
787static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000788static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
789static 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 +0000790static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
791static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000792static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200793static int var_check_func_name __ARGS((char_u *name, int new_var));
794static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000795static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000796static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
798static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
799static int eval_fname_script __ARGS((char_u *p));
800static int eval_fname_sid __ARGS((char_u *p));
801static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static ufunc_T *find_func __ARGS((char_u *name));
803static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000804static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
806static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000807static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
808static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_total_cmp __ARGS((const void *s1, const void *s2));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000820static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static 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 +0000825static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
826static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000828static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
829static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000830static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000831static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000832static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000833
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200834
835#ifdef EBCDIC
836static int compare_func_name __ARGS((const void *s1, const void *s2));
837static void sortFunctions __ARGS(());
838#endif
839
840
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000841/* Character used as separated in autoload function/variable names. */
842#define AUTOLOAD_CHAR '#'
843
Bram Moolenaar33570922005-01-25 22:26:29 +0000844/*
845 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000846 */
847 void
848eval_init()
849{
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 int i;
851 struct vimvar *p;
852
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200853 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
854 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200855 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000856 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000857 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
859 for (i = 0; i < VV_LEN; ++i)
860 {
861 p = &vimvars[i];
862 STRCPY(p->vv_di.di_key, p->vv_name);
863 if (p->vv_flags & VV_RO)
864 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
865 else if (p->vv_flags & VV_RO_SBX)
866 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
867 else
868 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000869
870 /* add to v: scope dict, unless the value is not always available */
871 if (p->vv_type != VAR_UNKNOWN)
872 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000874 /* add to compat scope dict */
875 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000877 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200914 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915
916 /* global variables */
917 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000919 /* autoloaded script names */
920 ga_clear_strings(&ga_loaded);
921
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200922 /* script-local variables */
923 for (i = 1; i <= ga_scripts.ga_len; ++i)
924 {
925 vars_clear(&SCRIPT_VARS(i));
926 vim_free(SCRIPT_SV(i));
927 }
928 ga_clear(&ga_scripts);
929
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000930 /* unreferenced lists and dicts */
931 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000932
933 /* functions */
934 free_all_functions();
935 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936}
937#endif
938
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Return the name of the executed function.
941 */
942 char_u *
943func_name(cookie)
944 void *cookie;
945{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000946 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Return the address holding the next breakpoint line for a funccall cookie.
951 */
952 linenr_T *
953func_breakpoint(cookie)
954 void *cookie;
955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the address holding the debug tick for a funccall cookie.
961 */
962 int *
963func_dbg_tick(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Return the nesting level for a funccall cookie.
971 */
972 int
973func_level(cookie)
974 void *cookie;
975{
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000980funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000982/* pointer to list of previously used funccal, still around because some
983 * item in it is still being used. */
984funccall_T *previous_funccal = NULL;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986/*
987 * Return TRUE when a function was ended by a ":return" command.
988 */
989 int
990current_func_returned()
991{
992 return current_funccal->returned;
993}
994
995
996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Set an internal variable to a string value. Creates the variable if it does
998 * not already exist.
999 */
1000 void
1001set_internal_string_var(name, value)
1002 char_u *name;
1003 char_u *value;
1004{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 val = vim_strsave(value);
1009 if (val != NULL)
1010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001015 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 }
1018}
1019
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001021static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022static char_u *redir_endp = NULL;
1023static char_u *redir_varname = NULL;
1024
1025/*
1026 * Start recording command output to a variable
1027 * Returns OK if successfully completed the setup. FAIL otherwise.
1028 */
1029 int
1030var_redir_start(name, append)
1031 char_u *name;
1032 int append; /* append to an existing variable */
1033{
1034 int save_emsg;
1035 int err;
1036 typval_T tv;
1037
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001038 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001039 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 {
1041 EMSG(_(e_invarg));
1042 return FAIL;
1043 }
1044
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001045 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 redir_varname = vim_strsave(name);
1047 if (redir_varname == NULL)
1048 return FAIL;
1049
1050 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1051 if (redir_lval == NULL)
1052 {
1053 var_redir_stop();
1054 return FAIL;
1055 }
1056
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 /* The output is stored in growarray "redir_ga" until redirection ends. */
1058 ga_init2(&redir_ga, (int)sizeof(char), 500);
1059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001061 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1062 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1064 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001065 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp != NULL && *redir_endp != NUL)
1067 /* Trailing characters are present after the variable name */
1068 EMSG(_(e_trailing));
1069 else
1070 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001071 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 var_redir_stop();
1073 return FAIL;
1074 }
1075
1076 /* check if we can write to the variable: set it to or append an empty
1077 * string */
1078 save_emsg = did_emsg;
1079 did_emsg = FALSE;
1080 tv.v_type = VAR_STRING;
1081 tv.vval.v_string = (char_u *)"";
1082 if (append)
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1084 else
1085 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001086 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001088 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 if (err)
1090 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001091 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 var_redir_stop();
1093 return FAIL;
1094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100 * Append "value[value_len]" to the variable set by var_redir_start().
1101 * The actual appending is postponed until redirection ends, because the value
1102 * appended may in fact be the string we write to, changing it may cause freed
1103 * memory to be used:
1104 * :redir => foo
1105 * :let foo
1106 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 */
1108 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114
1115 if (redir_lval == NULL)
1116 return;
1117
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 {
1125 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 }
1128 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130}
1131
1132/*
1133 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 */
1136 void
1137var_redir_stop()
1138{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 typval_T tv;
1140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_lval != NULL)
1142 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 /* If there was no error: assign the text to the variable. */
1144 if (redir_endp != NULL)
1145 {
1146 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001149 /* Call get_lval() again, if it's inside a Dict or List it may
1150 * have changed. */
1151 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1152 FALSE, FALSE, FALSE, FNE_CHECK_START);
1153 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1154 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1155 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* free the collected output */
1159 vim_free(redir_ga.ga_data);
1160 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 vim_free(redir_lval);
1163 redir_lval = NULL;
1164 }
1165 vim_free(redir_varname);
1166 redir_varname = NULL;
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169# if defined(FEAT_MBYTE) || defined(PROTO)
1170 int
1171eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1172 char_u *enc_from;
1173 char_u *enc_to;
1174 char_u *fname_from;
1175 char_u *fname_to;
1176{
1177 int err = FALSE;
1178
1179 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1180 set_vim_var_string(VV_CC_TO, enc_to, -1);
1181 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1182 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1183 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1184 err = TRUE;
1185 set_vim_var_string(VV_CC_FROM, NULL, -1);
1186 set_vim_var_string(VV_CC_TO, NULL, -1);
1187 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1188 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1189
1190 if (err)
1191 return FAIL;
1192 return OK;
1193}
1194# endif
1195
1196# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1197 int
1198eval_printexpr(fname, args)
1199 char_u *fname;
1200 char_u *args;
1201{
1202 int err = FALSE;
1203
1204 set_vim_var_string(VV_FNAME_IN, fname, -1);
1205 set_vim_var_string(VV_CMDARG, args, -1);
1206 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1207 err = TRUE;
1208 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1209 set_vim_var_string(VV_CMDARG, NULL, -1);
1210
1211 if (err)
1212 {
1213 mch_remove(fname);
1214 return FAIL;
1215 }
1216 return OK;
1217}
1218# endif
1219
1220# if defined(FEAT_DIFF) || defined(PROTO)
1221 void
1222eval_diff(origfile, newfile, outfile)
1223 char_u *origfile;
1224 char_u *newfile;
1225 char_u *outfile;
1226{
1227 int err = FALSE;
1228
1229 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1230 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1231 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1232 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1233 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1234 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1235 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1236}
1237
1238 void
1239eval_patch(origfile, difffile, outfile)
1240 char_u *origfile;
1241 char_u *difffile;
1242 char_u *outfile;
1243{
1244 int err;
1245
1246 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1248 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1249 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1250 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1251 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1252 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1253}
1254# endif
1255
1256/*
1257 * Top level evaluation function, returning a boolean.
1258 * Sets "error" to TRUE if there was an error.
1259 * Return TRUE or FALSE.
1260 */
1261 int
1262eval_to_bool(arg, error, nextcmd, skip)
1263 char_u *arg;
1264 int *error;
1265 char_u **nextcmd;
1266 int skip; /* only parse, don't execute */
1267{
Bram Moolenaar33570922005-01-25 22:26:29 +00001268 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 int retval = FALSE;
1270
1271 if (skip)
1272 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 else
1276 {
1277 *error = FALSE;
1278 if (!skip)
1279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001280 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 }
1284 if (skip)
1285 --emsg_skip;
1286
1287 return retval;
1288}
1289
1290/*
1291 * Top level evaluation function, returning a string. If "skip" is TRUE,
1292 * only parsing to "nextcmd" is done, without reporting errors. Return
1293 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1294 */
1295 char_u *
1296eval_to_string_skip(arg, nextcmd, skip)
1297 char_u *arg;
1298 char_u **nextcmd;
1299 int skip; /* only parse, don't execute */
1300{
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 char_u *retval;
1303
1304 if (skip)
1305 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 retval = NULL;
1308 else
1309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 retval = vim_strsave(get_tv_string(&tv));
1311 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 if (skip)
1314 --emsg_skip;
1315
1316 return retval;
1317}
1318
1319/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320 * Skip over an expression at "*pp".
1321 * Return FAIL for an error, OK otherwise.
1322 */
1323 int
1324skip_expr(pp)
1325 char_u **pp;
1326{
Bram Moolenaar33570922005-01-25 22:26:29 +00001327 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328
1329 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331}
1332
1333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335 * When "convert" is TRUE convert a List into a sequence of lines and convert
1336 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Return pointer to allocated memory, or NULL for failure.
1338 */
1339 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 char_u *arg;
1342 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001347 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 retval = NULL;
1354 else
1355 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 {
1358 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001359 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001360 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001361 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001362 if (tv.vval.v_list->lv_len > 0)
1363 ga_append(&ga, NL);
1364 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001365 ga_append(&ga, NUL);
1366 retval = (char_u *)ga.ga_data;
1367 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368#ifdef FEAT_FLOAT
1369 else if (convert && tv.v_type == VAR_FLOAT)
1370 {
1371 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1372 retval = vim_strsave(numbuf);
1373 }
1374#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001375 else
1376 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379
1380 return retval;
1381}
1382
1383/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384 * Call eval_to_string() without using current local variables and using
1385 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 */
1387 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 char_u *arg;
1390 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 char_u *retval;
1394 void *save_funccalp;
1395
1396 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 ++sandbox;
1399 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001400 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001401 if (use_sandbox)
1402 --sandbox;
1403 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 restore_funccal(save_funccalp);
1405 return retval;
1406}
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408/*
1409 * Top level evaluation function, returning a number.
1410 * Evaluates "expr" silently.
1411 * Returns -1 for an error.
1412 */
1413 int
1414eval_to_number(expr)
1415 char_u *expr;
1416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001419 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421 ++emsg_off;
1422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 retval = -1;
1425 else
1426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001427 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
1430 --emsg_off;
1431
1432 return retval;
1433}
1434
Bram Moolenaara40058a2005-07-11 22:42:07 +00001435/*
1436 * Prepare v: variable "idx" to be used.
1437 * Save the current typeval in "save_tv".
1438 * When not used yet add the variable to the v: hashtable.
1439 */
1440 static void
1441prepare_vimvar(idx, save_tv)
1442 int idx;
1443 typval_T *save_tv;
1444{
1445 *save_tv = vimvars[idx].vv_tv;
1446 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1447 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1448}
1449
1450/*
1451 * Restore v: variable "idx" to typeval "save_tv".
1452 * When no longer defined, remove the variable from the v: hashtable.
1453 */
1454 static void
1455restore_vimvar(idx, save_tv)
1456 int idx;
1457 typval_T *save_tv;
1458{
1459 hashitem_T *hi;
1460
Bram Moolenaara40058a2005-07-11 22:42:07 +00001461 vimvars[idx].vv_tv = *save_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 {
1464 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1465 if (HASHITEM_EMPTY(hi))
1466 EMSG2(_(e_intern2), "restore_vimvar()");
1467 else
1468 hash_remove(&vimvarht, hi);
1469 }
1470}
1471
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001472#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473/*
1474 * Evaluate an expression to a list with suggestions.
1475 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001476 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001477 */
1478 list_T *
1479eval_spell_expr(badword, expr)
1480 char_u *badword;
1481 char_u *expr;
1482{
1483 typval_T save_val;
1484 typval_T rettv;
1485 list_T *list = NULL;
1486 char_u *p = skipwhite(expr);
1487
1488 /* Set "v:val" to the bad word. */
1489 prepare_vimvar(VV_VAL, &save_val);
1490 vimvars[VV_VAL].vv_type = VAR_STRING;
1491 vimvars[VV_VAL].vv_str = badword;
1492 if (p_verbose == 0)
1493 ++emsg_off;
1494
1495 if (eval1(&p, &rettv, TRUE) == OK)
1496 {
1497 if (rettv.v_type != VAR_LIST)
1498 clear_tv(&rettv);
1499 else
1500 list = rettv.vval.v_list;
1501 }
1502
1503 if (p_verbose == 0)
1504 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505 restore_vimvar(VV_VAL, &save_val);
1506
1507 return list;
1508}
1509
1510/*
1511 * "list" is supposed to contain two items: a word and a number. Return the
1512 * word in "pp" and the number as the return value.
1513 * Return -1 if anything isn't right.
1514 * Used to get the good word and score from the eval_spell_expr() result.
1515 */
1516 int
1517get_spellword(list, pp)
1518 list_T *list;
1519 char_u **pp;
1520{
1521 listitem_T *li;
1522
1523 li = list->lv_first;
1524 if (li == NULL)
1525 return -1;
1526 *pp = get_tv_string(&li->li_tv);
1527
1528 li = li->li_next;
1529 if (li == NULL)
1530 return -1;
1531 return get_tv_number(&li->li_tv);
1532}
1533#endif
1534
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001536 * Top level evaluation function.
1537 * Returns an allocated typval_T with the result.
1538 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001539 */
1540 typval_T *
1541eval_expr(arg, nextcmd)
1542 char_u *arg;
1543 char_u **nextcmd;
1544{
1545 typval_T *tv;
1546
1547 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001548 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 {
1550 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 }
1553
1554 return tv;
1555}
1556
1557
Bram Moolenaar4f688582007-07-24 12:34:30 +00001558#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1559 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562 * Uses argv[argc] for the function arguments. Only Number and String
1563 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001566 int
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 char_u *func;
1569 int argc;
1570 char_u **argv;
1571 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
Bram Moolenaar33570922005-01-25 22:26:29 +00001574 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 long n;
1576 int len;
1577 int i;
1578 int doesrange;
1579 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001582 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 for (i = 0; i < argc; i++)
1587 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 /* Pass a NULL or empty argument as an empty string */
1589 if (argv[i] == NULL || *argv[i] == NUL)
1590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001591 argvars[i].v_type = VAR_STRING;
1592 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 continue;
1594 }
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /* Recognize a number argument, the others must be strings. */
1597 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1598 if (len != 0 && len == (int)STRLEN(argv[i]))
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_NUMBER;
1601 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 else
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_STRING;
1606 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 }
1609
1610 if (safe)
1611 {
1612 save_funccalp = save_funccal();
1613 ++sandbox;
1614 }
1615
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1617 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (safe)
1621 {
1622 --sandbox;
1623 restore_funccal(save_funccalp);
1624 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 vim_free(argvars);
1626
1627 if (ret == FAIL)
1628 clear_tv(rettv);
1629
1630 return ret;
1631}
1632
Bram Moolenaar4f688582007-07-24 12:34:30 +00001633# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001635 * Call vimL function "func" and return the result as a string.
1636 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 * Uses argv[argc] for the function arguments.
1638 */
1639 void *
1640call_func_retstr(func, argc, argv, safe)
1641 char_u *func;
1642 int argc;
1643 char_u **argv;
1644 int safe; /* use the sandbox */
1645{
1646 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001647 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648
1649 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1650 return NULL;
1651
1652 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 return retval;
1655}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001656# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001660 * Call vimL function "func" and return the result as a number.
1661 * Returns -1 when calling the function fails.
1662 * Uses argv[argc] for the function arguments.
1663 */
1664 long
1665call_func_retnr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
1672 long retval;
1673
1674 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1675 return -1;
1676
1677 retval = get_tv_number_chk(&rettv, NULL);
1678 clear_tv(&rettv);
1679 return retval;
1680}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001681# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682
1683/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001684 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001686 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 */
1688 void *
1689call_func_retlist(func, argc, argv, safe)
1690 char_u *func;
1691 int argc;
1692 char_u **argv;
1693 int safe; /* use the sandbox */
1694{
1695 typval_T rettv;
1696
1697 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1698 return NULL;
1699
1700 if (rettv.v_type != VAR_LIST)
1701 {
1702 clear_tv(&rettv);
1703 return NULL;
1704 }
1705
1706 return rettv.vval.v_list;
1707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709
Bram Moolenaar4f688582007-07-24 12:34:30 +00001710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711/*
1712 * Save the current function call pointer, and set it to NULL.
1713 * Used when executing autocommands and for ":source".
1714 */
1715 void *
1716save_funccal()
1717{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 current_funccal = NULL;
1721 return (void *)fc;
1722}
1723
1724 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725restore_funccal(vfc)
1726 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728 funccall_T *fc = (funccall_T *)vfc;
1729
1730 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731}
1732
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733#if defined(FEAT_PROFILE) || defined(PROTO)
1734/*
1735 * Prepare profiling for entering a child or something else that is not
1736 * counted for the script/function itself.
1737 * Should always be called in pair with prof_child_exit().
1738 */
1739 void
1740prof_child_enter(tm)
1741 proftime_T *tm; /* place to store waittime */
1742{
1743 funccall_T *fc = current_funccal;
1744
1745 if (fc != NULL && fc->func->uf_profiling)
1746 profile_start(&fc->prof_child);
1747 script_prof_save(tm);
1748}
1749
1750/*
1751 * Take care of time spent in a child.
1752 * Should always be called after prof_child_enter().
1753 */
1754 void
1755prof_child_exit(tm)
1756 proftime_T *tm; /* where waittime was stored */
1757{
1758 funccall_T *fc = current_funccal;
1759
1760 if (fc != NULL && fc->func->uf_profiling)
1761 {
1762 profile_end(&fc->prof_child);
1763 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1764 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1765 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1766 }
1767 script_prof_restore(tm);
1768}
1769#endif
1770
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_FOLDING
1773/*
1774 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1775 * it in "*cp". Doesn't give error messages.
1776 */
1777 int
1778eval_foldexpr(arg, cp)
1779 char_u *arg;
1780 int *cp;
1781{
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 int retval;
1784 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001785 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1786 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001789 if (use_sandbox)
1790 ++sandbox;
1791 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 retval = 0;
1795 else
1796 {
1797 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 if (tv.v_type == VAR_NUMBER)
1799 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001800 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 retval = 0;
1802 else
1803 {
1804 /* If the result is a string, check if there is a non-digit before
1805 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (!VIM_ISDIGIT(*s) && *s != '-')
1808 *cp = *s++;
1809 retval = atol((char *)s);
1810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001814 if (use_sandbox)
1815 --sandbox;
1816 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 return retval;
1819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 * ":let" list all variable values
1824 * ":let var1 var2" list variable values
1825 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 * ":let var += expr" assignment command.
1827 * ":let var -= expr" assignment command.
1828 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831 void
1832ex_let(eap)
1833 exarg_T *eap;
1834{
1835 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 int var_count = 0;
1840 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001843 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 argend = skip_var_list(arg, &var_count, &semicolon);
1846 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001848 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1849 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001853 /*
1854 * ":let" without "=": list variables
1855 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 if (*arg == '[')
1857 EMSG(_(e_invarg));
1858 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_glob_vars(&first);
1865 list_buf_vars(&first);
1866 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001867#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001869#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_script_vars(&first);
1871 list_func_vars(&first);
1872 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 eap->nextcmd = check_nextcmd(arg);
1875 }
1876 else
1877 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 op[0] = '=';
1879 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 {
1882 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1883 op[0] = expr[-1]; /* +=, -= or .= */
1884 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (eap->skip)
1888 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (eap->skip)
1891 {
1892 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 --emsg_skip;
1895 }
1896 else if (i != FAIL)
1897 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 }
1903}
1904
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905/*
1906 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1907 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 * When "nextchars" is not NULL it points to a string with characters that
1909 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1910 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 * Returns OK or FAIL;
1912 */
1913 static int
1914ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1915 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001916 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 int copy; /* copy values from "tv", don't move */
1918 int semicolon; /* from skip_var_list() */
1919 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921{
1922 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 listitem_T *item;
1926 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927
1928 if (*arg != '[')
1929 {
1930 /*
1931 * ":let var = expr" or ":for var in list"
1932 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 return FAIL;
1935 return OK;
1936 }
1937
1938 /*
1939 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1940 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001941 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 {
1943 EMSG(_(e_listreq));
1944 return FAIL;
1945 }
1946
1947 i = list_len(l);
1948 if (semicolon == 0 && var_count < i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953 if (var_count - semicolon > i)
1954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001955 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 return FAIL;
1957 }
1958
1959 item = l->lv_first;
1960 while (*arg != ']')
1961 {
1962 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 item = item->li_next;
1965 if (arg == NULL)
1966 return FAIL;
1967
1968 arg = skipwhite(arg);
1969 if (*arg == ';')
1970 {
1971 /* Put the rest of the list (may be empty) in the var after ';'.
1972 * Create a new list for this. */
1973 l = list_alloc();
1974 if (l == NULL)
1975 return FAIL;
1976 while (item != NULL)
1977 {
1978 list_append_tv(l, &item->li_tv);
1979 item = item->li_next;
1980 }
1981
1982 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001983 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 ltv.vval.v_list = l;
1985 l->lv_refcount = 1;
1986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1988 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 clear_tv(&ltv);
1990 if (arg == NULL)
1991 return FAIL;
1992 break;
1993 }
1994 else if (*arg != ',' && *arg != ']')
1995 {
1996 EMSG2(_(e_intern2), "ex_let_vars()");
1997 return FAIL;
1998 }
1999 }
2000
2001 return OK;
2002}
2003
2004/*
2005 * Skip over assignable variable "var" or list of variables "[var, var]".
2006 * Used for ":let varvar = expr" and ":for varvar in expr".
2007 * For "[var, var]" increment "*var_count" for each variable.
2008 * for "[var, var; var]" set "semicolon".
2009 * Return NULL for an error.
2010 */
2011 static char_u *
2012skip_var_list(arg, var_count, semicolon)
2013 char_u *arg;
2014 int *var_count;
2015 int *semicolon;
2016{
2017 char_u *p, *s;
2018
2019 if (*arg == '[')
2020 {
2021 /* "[var, var]": find the matching ']'. */
2022 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002023 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 {
2025 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2026 s = skip_var_one(p);
2027 if (s == p)
2028 {
2029 EMSG2(_(e_invarg2), p);
2030 return NULL;
2031 }
2032 ++*var_count;
2033
2034 p = skipwhite(s);
2035 if (*p == ']')
2036 break;
2037 else if (*p == ';')
2038 {
2039 if (*semicolon == 1)
2040 {
2041 EMSG(_("Double ; in list of variables"));
2042 return NULL;
2043 }
2044 *semicolon = 1;
2045 }
2046 else if (*p != ',')
2047 {
2048 EMSG2(_(e_invarg2), p);
2049 return NULL;
2050 }
2051 }
2052 return p + 1;
2053 }
2054 else
2055 return skip_var_one(arg);
2056}
2057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002059 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002060 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 static char_u *
2063skip_var_one(arg)
2064 char_u *arg;
2065{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002066 if (*arg == '@' && arg[1] != NUL)
2067 return arg + 2;
2068 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2069 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070}
2071
Bram Moolenaara7043832005-01-21 11:56:39 +00002072/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 * List variables for hashtab "ht" with prefix "prefix".
2074 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082{
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 hashitem_T *hi;
2084 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 int todo;
2086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002087 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2089 {
2090 if (!HASHITEM_EMPTY(hi))
2091 {
2092 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 di = HI2DI(hi);
2094 if (empty || di->di_tv.v_type != VAR_STRING
2095 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
2098 }
2099}
2100
2101/*
2102 * List global variables.
2103 */
2104 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105list_glob_vars(first)
2106 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002107{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002109}
2110
2111/*
2112 * List buffer variables.
2113 */
2114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_buf_vars(first)
2116 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 char_u numbuf[NUMBUFLEN];
2119
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2121 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122
2123 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2125 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List window variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_win_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2136 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137}
2138
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002139#ifdef FEAT_WINDOWS
2140/*
2141 * List tab page variables.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_tab_vars(first)
2145 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002146{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2148 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002149}
2150#endif
2151
Bram Moolenaara7043832005-01-21 11:56:39 +00002152/*
2153 * List Vim variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_vim_vars(first)
2157 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160}
2161
2162/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163 * List script-local variables, if there is a script.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_script_vars(first)
2167 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168{
2169 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2171 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172}
2173
2174/*
2175 * List function variables, if there is a function.
2176 */
2177 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178list_func_vars(first)
2179 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180{
2181 if (current_funccal != NULL)
2182 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002184}
2185
2186/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 * List variables in "arg".
2188 */
2189 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 exarg_T *eap;
2192 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194{
2195 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 char_u *name_start;
2199 char_u *arg_subsc;
2200 char_u *tofree;
2201 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202
2203 while (!ends_excmd(*arg) && !got_int)
2204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002207 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002208 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2209 {
2210 emsg_severe = TRUE;
2211 EMSG(_(e_trailing));
2212 break;
2213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* get_name_len() takes care of expanding curly braces */
2218 name_start = name = arg;
2219 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2220 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 /* This is mainly to keep test 49 working: when expanding
2223 * curly braces fails overrule the exception error message. */
2224 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 emsg_severe = TRUE;
2227 EMSG2(_(e_invarg2), arg);
2228 break;
2229 }
2230 error = TRUE;
2231 }
2232 else
2233 {
2234 if (tofree != NULL)
2235 name = tofree;
2236 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 else
2239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 /* handle d.key, l[idx], f(expr) */
2241 arg_subsc = arg;
2242 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 'g': list_glob_vars(first); break;
2251 case 'b': list_buf_vars(first); break;
2252 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002253#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 'v': list_vim_vars(first); break;
2257 case 's': list_script_vars(first); break;
2258 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 default:
2260 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
2263 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 {
2265 char_u numbuf[NUMBUFLEN];
2266 char_u *tf;
2267 int c;
2268 char_u *s;
2269
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002270 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 c = *arg;
2272 *arg = NUL;
2273 list_one_var_a((char_u *)"",
2274 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002275 tv.v_type,
2276 s == NULL ? (char_u *)"" : s,
2277 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 *arg = c;
2279 vim_free(tf);
2280 }
2281 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285
2286 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288
2289 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 }
2291
2292 return arg;
2293}
2294
2295/*
2296 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2297 * Returns a pointer to the char just after the var name.
2298 * Returns NULL if there is an error.
2299 */
2300 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002303 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002305 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307{
2308 int c1;
2309 char_u *name;
2310 char_u *p;
2311 char_u *arg_end = NULL;
2312 int len;
2313 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315
2316 /*
2317 * ":let $VAR = expr": Set environment variable.
2318 */
2319 if (*arg == '$')
2320 {
2321 /* Find the end of the name. */
2322 ++arg;
2323 name = arg;
2324 len = get_env_len(&arg);
2325 if (len == 0)
2326 EMSG2(_(e_invarg2), name - 1);
2327 else
2328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 if (op != NULL && (*op == '+' || *op == '-'))
2330 EMSG2(_(e_letwrong), op);
2331 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002332 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002334 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 {
2336 c1 = name[len];
2337 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 p = get_tv_string_chk(tv);
2339 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 {
2341 int mustfree = FALSE;
2342 char_u *s = vim_getenv(name, &mustfree);
2343
2344 if (s != NULL)
2345 {
2346 p = tofree = concat_str(s, p);
2347 if (mustfree)
2348 vim_free(s);
2349 }
2350 }
2351 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 if (STRICMP(name, "HOME") == 0)
2355 init_homedir();
2356 else if (didset_vim && STRICMP(name, "VIM") == 0)
2357 didset_vim = FALSE;
2358 else if (didset_vimruntime
2359 && STRICMP(name, "VIMRUNTIME") == 0)
2360 didset_vimruntime = FALSE;
2361 arg_end = arg;
2362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
2367 }
2368
2369 /*
2370 * ":let &option = expr": Set option value.
2371 * ":let &l:option = expr": Set local option value.
2372 * ":let &g:option = expr": Set global option value.
2373 */
2374 else if (*arg == '&')
2375 {
2376 /* Find the end of the name. */
2377 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 if (p == NULL || (endchars != NULL
2379 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 EMSG(_(e_letunexp));
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 long n;
2384 int opt_type;
2385 long numval;
2386 char_u *stringval = NULL;
2387 char_u *s;
2388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 c1 = *p;
2390 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391
2392 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 s = get_tv_string_chk(tv); /* != NULL if number or string */
2394 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 {
2396 opt_type = get_option_value(arg, &numval,
2397 &stringval, opt_flags);
2398 if ((opt_type == 1 && *op == '.')
2399 || (opt_type == 0 && *op != '.'))
2400 EMSG2(_(e_letwrong), op);
2401 else
2402 {
2403 if (opt_type == 1) /* number */
2404 {
2405 if (*op == '+')
2406 n = numval + n;
2407 else
2408 n = numval - n;
2409 }
2410 else if (opt_type == 0 && stringval != NULL) /* string */
2411 {
2412 s = concat_str(stringval, s);
2413 vim_free(stringval);
2414 stringval = s;
2415 }
2416 }
2417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 if (s != NULL)
2419 {
2420 set_option_value(arg, n, s, opt_flags);
2421 arg_end = p;
2422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 }
2426 }
2427
2428 /*
2429 * ":let @r = expr": Set register contents.
2430 */
2431 else if (*arg == '@')
2432 {
2433 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 if (op != NULL && (*op == '+' || *op == '-'))
2435 EMSG2(_(e_letwrong), op);
2436 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 EMSG(_(e_letunexp));
2439 else
2440 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002441 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 char_u *s;
2443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 p = get_tv_string_chk(tv);
2445 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002447 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (s != NULL)
2449 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002450 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(s);
2452 }
2453 }
2454 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 arg_end = arg + 1;
2458 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002459 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 }
2461 }
2462
2463 /*
2464 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002469 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002471 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 arg_end = p;
2480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484
2485 else
2486 EMSG2(_(e_invarg2), arg);
2487
2488 return arg_end;
2489}
2490
2491/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2493 */
2494 static int
2495check_changedtick(arg)
2496 char_u *arg;
2497{
2498 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2499 {
2500 EMSG2(_(e_readonlyvar), arg);
2501 return TRUE;
2502 }
2503 return FALSE;
2504}
2505
2506/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 * Get an lval: variable, Dict item or List item that can be assigned a value
2508 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2509 * "name.key", "name.key[expr]" etc.
2510 * Indexing only works if "name" is an existing List or Dictionary.
2511 * "name" points to the start of the name.
2512 * If "rettv" is not NULL it points to the value to be assigned.
2513 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2514 * wrong; must end in space or cmd separator.
2515 *
2516 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002517 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 */
2520 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002521get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 typval_T *rettv;
2524 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 int unlet;
2526 int skip;
2527 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002528 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 char_u *expr_start, *expr_end;
2532 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 dictitem_T *v;
2534 typval_T var1;
2535 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544
2545 if (skip)
2546 {
2547 /* When skipping just find the end of the name. */
2548 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 }
2551
2552 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (expr_start != NULL)
2555 {
2556 /* Don't expand the name when we already know there is an error. */
2557 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2558 && *p != '[' && *p != '.')
2559 {
2560 EMSG(_(e_trailing));
2561 return NULL;
2562 }
2563
2564 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2565 if (lp->ll_exp_name == NULL)
2566 {
2567 /* Report an invalid expression in braces, unless the
2568 * expression evaluation has been cancelled due to an
2569 * aborting error, an interrupt, or an exception. */
2570 if (!aborting() && !quiet)
2571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002572 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 EMSG2(_(e_invarg2), name);
2574 return NULL;
2575 }
2576 }
2577 lp->ll_name = lp->ll_exp_name;
2578 }
2579 else
2580 lp->ll_name = name;
2581
2582 /* Without [idx] or .key we are done. */
2583 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2584 return p;
2585
2586 cc = *p;
2587 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002588 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (v == NULL && !quiet)
2590 EMSG2(_(e_undefvar), lp->ll_name);
2591 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 if (v == NULL)
2593 return NULL;
2594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /*
2596 * Loop until no more [idx] or .key is following.
2597 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2602 && !(lp->ll_tv->v_type == VAR_DICT
2603 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_("E689: Can only index a List or Dictionary"));
2607 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!quiet)
2612 EMSG(_("E708: [:] must come last"));
2613 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 len = -1;
2617 if (*p == '.')
2618 {
2619 key = p + 1;
2620 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2621 ;
2622 if (len == 0)
2623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_(e_emptykey));
2626 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 }
2628 p = key + len;
2629 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 else
2631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 if (*p == ':')
2635 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 else
2637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 empty1 = FALSE;
2639 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002641 if (get_tv_string_chk(&var1) == NULL)
2642 {
2643 /* not a number or string */
2644 clear_tv(&var1);
2645 return NULL;
2646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 }
2648
2649 /* Optionally get the second index [ :expr]. */
2650 if (*p == ':')
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656 if (!empty1)
2657 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (rettv != NULL && (rettv->v_type != VAR_LIST
2661 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (!empty1)
2666 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = skipwhite(p + 1);
2670 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 else
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (!empty1)
2678 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002681 if (get_tv_string_chk(&var2) == NULL)
2682 {
2683 /* not a number or string */
2684 if (!empty1)
2685 clear_tv(&var1);
2686 clear_tv(&var2);
2687 return NULL;
2688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (!quiet)
2698 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705
2706 /* Skip to past ']'. */
2707 ++p;
2708 }
2709
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
2712 if (len == -1)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002715 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (*key == NUL)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 lp->ll_list = NULL;
2725 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002726 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002727
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002728 /* When assigning to a scope dictionary check that a function and
2729 * variable name is valid (only variable name unless it is l: or
2730 * g: dictionary). Disallow overwriting a builtin function. */
2731 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002732 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002733 int prevval;
2734 int wrong;
2735
2736 if (len != -1)
2737 {
2738 prevval = key[len];
2739 key[len] = NUL;
2740 }
2741 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2742 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002743 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002744 || !valid_varname(key);
2745 if (len != -1)
2746 key[len] = prevval;
2747 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002748 return NULL;
2749 }
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002753 /* Can't add "v:" variable. */
2754 if (lp->ll_dict == &vimvardict)
2755 {
2756 EMSG2(_(e_illvar), name);
2757 return NULL;
2758 }
2759
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002760 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (len == -1)
2766 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 }
2769 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 if (len == -1)
2774 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 p = NULL;
2777 break;
2778 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 /* existing variable, need to check if it can be changed */
2780 else if (var_check_ro(lp->ll_di->di_flags, name))
2781 return NULL;
2782
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 if (len == -1)
2784 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 }
2787 else
2788 {
2789 /*
2790 * Get the number and item for the only or first index of the List.
2791 */
2792 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 else
2795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002796 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var1);
2798 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002799 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_list = lp->ll_tv->vval.v_list;
2801 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2802 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002804 if (lp->ll_n1 < 0)
2805 {
2806 lp->ll_n1 = 0;
2807 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2808 }
2809 }
2810 if (lp->ll_li == NULL)
2811 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002814 if (!quiet)
2815 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818
2819 /*
2820 * May need to find the item or absolute index for the second
2821 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 * When no index given: "lp->ll_empty2" is TRUE.
2823 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 {
2834 if (!quiet)
2835 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2842 if (lp->ll_n1 < 0)
2843 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2844 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 {
2846 if (!quiet)
2847 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002849 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002850 }
2851
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 return p;
2857}
2858
2859/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 */
2862 static void
2863clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865{
2866 vim_free(lp->ll_exp_name);
2867 vim_free(lp->ll_newkey);
2868}
2869
2870/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002871 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002873 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 */
2875 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882{
2883 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 listitem_T *ri;
2885 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886
2887 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 cc = *endp;
2892 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 if (op != NULL && *op != '=')
2894 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002897 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002898 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002899 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900 {
2901 if (tv_op(&tv, rettv, op) == OK)
2902 set_var(lp->ll_name, &tv, FALSE);
2903 clear_tv(&tv);
2904 }
2905 }
2906 else
2907 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 else if (tv_check_lock(lp->ll_newkey == NULL
2912 ? lp->ll_tv->v_lock
2913 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2914 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 else if (lp->ll_range)
2916 {
2917 /*
2918 * Assign the List values to the list items.
2919 */
2920 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 if (op != NULL && *op != '=')
2923 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2924 else
2925 {
2926 clear_tv(&lp->ll_li->li_tv);
2927 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 ri = ri->li_next;
2930 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2931 break;
2932 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002935 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 ri = NULL;
2938 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 lp->ll_li = lp->ll_li->li_next;
2942 ++lp->ll_n1;
2943 }
2944 if (ri != NULL)
2945 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 else if (lp->ll_empty2
2947 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 : lp->ll_n1 != lp->ll_n2)
2949 EMSG(_("E711: List value has not enough items"));
2950 }
2951 else
2952 {
2953 /*
2954 * Assign to a List or Dictionary item.
2955 */
2956 if (lp->ll_newkey != NULL)
2957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 if (op != NULL && *op != '=')
2959 {
2960 EMSG2(_(e_letwrong), op);
2961 return;
2962 }
2963
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002965 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 if (di == NULL)
2967 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2969 {
2970 vim_free(di);
2971 return;
2972 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002974 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002975 else if (op != NULL && *op != '=')
2976 {
2977 tv_op(lp->ll_tv, rettv, op);
2978 return;
2979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002982
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 /*
2984 * Assign the value to the variable or list item.
2985 */
2986 if (copy)
2987 copy_tv(rettv, lp->ll_tv);
2988 else
2989 {
2990 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002991 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002993 }
2994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995}
2996
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002997/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2999 * Returns OK or FAIL.
3000 */
3001 static int
3002tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 typval_T *tv1;
3004 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 char_u *op;
3006{
3007 long n;
3008 char_u numbuf[NUMBUFLEN];
3009 char_u *s;
3010
3011 /* Can't do anything with a Funcref or a Dict on the right. */
3012 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3013 {
3014 switch (tv1->v_type)
3015 {
3016 case VAR_DICT:
3017 case VAR_FUNC:
3018 break;
3019
3020 case VAR_LIST:
3021 if (*op != '+' || tv2->v_type != VAR_LIST)
3022 break;
3023 /* List += List */
3024 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3025 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3026 return OK;
3027
3028 case VAR_NUMBER:
3029 case VAR_STRING:
3030 if (tv2->v_type == VAR_LIST)
3031 break;
3032 if (*op == '+' || *op == '-')
3033 {
3034 /* nr += nr or nr -= nr*/
3035 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003036#ifdef FEAT_FLOAT
3037 if (tv2->v_type == VAR_FLOAT)
3038 {
3039 float_T f = n;
3040
3041 if (*op == '+')
3042 f += tv2->vval.v_float;
3043 else
3044 f -= tv2->vval.v_float;
3045 clear_tv(tv1);
3046 tv1->v_type = VAR_FLOAT;
3047 tv1->vval.v_float = f;
3048 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050#endif
3051 {
3052 if (*op == '+')
3053 n += get_tv_number(tv2);
3054 else
3055 n -= get_tv_number(tv2);
3056 clear_tv(tv1);
3057 tv1->v_type = VAR_NUMBER;
3058 tv1->vval.v_number = n;
3059 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 }
3061 else
3062 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063 if (tv2->v_type == VAR_FLOAT)
3064 break;
3065
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 /* str .= str */
3067 s = get_tv_string(tv1);
3068 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_STRING;
3071 tv1->vval.v_string = s;
3072 }
3073 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074
3075#ifdef FEAT_FLOAT
3076 case VAR_FLOAT:
3077 {
3078 float_T f;
3079
3080 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3081 && tv2->v_type != VAR_NUMBER
3082 && tv2->v_type != VAR_STRING))
3083 break;
3084 if (tv2->v_type == VAR_FLOAT)
3085 f = tv2->vval.v_float;
3086 else
3087 f = get_tv_number(tv2);
3088 if (*op == '+')
3089 tv1->vval.v_float += f;
3090 else
3091 tv1->vval.v_float -= f;
3092 }
3093 return OK;
3094#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 }
3096 }
3097
3098 EMSG2(_(e_letwrong), op);
3099 return FAIL;
3100}
3101
3102/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003103 * Add a watcher to a list.
3104 */
3105 static void
3106list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 list_T *l;
3108 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109{
3110 lw->lw_next = l->lv_watch;
3111 l->lv_watch = lw;
3112}
3113
3114/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003115 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116 * No warning when it isn't found...
3117 */
3118 static void
3119list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003120 list_T *l;
3121 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122{
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124
3125 lwp = &l->lv_watch;
3126 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3127 {
3128 if (lw == lwrem)
3129 {
3130 *lwp = lw->lw_next;
3131 break;
3132 }
3133 lwp = &lw->lw_next;
3134 }
3135}
3136
3137/*
3138 * Just before removing an item from a list: advance watchers to the next
3139 * item.
3140 */
3141 static void
3142list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003143 list_T *l;
3144 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145{
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147
3148 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3149 if (lw->lw_item == item)
3150 lw->lw_item = item->li_next;
3151}
3152
3153/*
3154 * Evaluate the expression used in a ":for var in expr" command.
3155 * "arg" points to "var".
3156 * Set "*errp" to TRUE for an error, FALSE otherwise;
3157 * Return a pointer that holds the info. Null when there is an error.
3158 */
3159 void *
3160eval_for_line(arg, errp, nextcmdp, skip)
3161 char_u *arg;
3162 int *errp;
3163 char_u **nextcmdp;
3164 int skip;
3165{
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 typval_T tv;
3169 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170
3171 *errp = TRUE; /* default: there is an error */
3172
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 if (fi == NULL)
3175 return NULL;
3176
3177 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3178 if (expr == NULL)
3179 return fi;
3180
3181 expr = skipwhite(expr);
3182 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3183 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003184 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 return fi;
3186 }
3187
3188 if (skip)
3189 ++emsg_skip;
3190 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3191 {
3192 *errp = FALSE;
3193 if (!skip)
3194 {
3195 l = tv.vval.v_list;
3196 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003197 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 clear_tv(&tv);
3200 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 else
3202 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003203 /* No need to increment the refcount, it's already set for the
3204 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 fi->fi_list = l;
3206 list_add_watch(l, &fi->fi_lw);
3207 fi->fi_lw.lw_item = l->lv_first;
3208 }
3209 }
3210 }
3211 if (skip)
3212 --emsg_skip;
3213
3214 return fi;
3215}
3216
3217/*
3218 * Use the first item in a ":for" list. Advance to the next.
3219 * Assign the values to the variable (list). "arg" points to the first one.
3220 * Return TRUE when a valid item was found, FALSE when at end of list or
3221 * something wrong.
3222 */
3223 int
3224next_for_item(fi_void, arg)
3225 void *fi_void;
3226 char_u *arg;
3227{
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231
3232 item = fi->fi_lw.lw_item;
3233 if (item == NULL)
3234 result = FALSE;
3235 else
3236 {
3237 fi->fi_lw.lw_item = item->li_next;
3238 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3239 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3240 }
3241 return result;
3242}
3243
3244/*
3245 * Free the structure used to store info used by ":for".
3246 */
3247 void
3248free_for_info(fi_void)
3249 void *fi_void;
3250{
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003253 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 list_unref(fi->fi_list);
3257 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 vim_free(fi);
3259}
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3262
3263 void
3264set_context_for_expression(xp, arg, cmdidx)
3265 expand_T *xp;
3266 char_u *arg;
3267 cmdidx_T cmdidx;
3268{
3269 int got_eq = FALSE;
3270 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 if (cmdidx == CMD_let)
3274 {
3275 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003276 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 {
3278 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003279 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 {
3281 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 if (vim_iswhite(*p))
3284 break;
3285 }
3286 return;
3287 }
3288 }
3289 else
3290 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3291 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 while ((xp->xp_pattern = vim_strpbrk(arg,
3293 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3294 {
3295 c = *xp->xp_pattern;
3296 if (c == '&')
3297 {
3298 c = xp->xp_pattern[1];
3299 if (c == '&')
3300 {
3301 ++xp->xp_pattern;
3302 xp->xp_context = cmdidx != CMD_let || got_eq
3303 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3304 }
3305 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003308 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3309 xp->xp_pattern += 2;
3310
3311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313 else if (c == '$')
3314 {
3315 /* environment variable */
3316 xp->xp_context = EXPAND_ENV_VARS;
3317 }
3318 else if (c == '=')
3319 {
3320 got_eq = TRUE;
3321 xp->xp_context = EXPAND_EXPRESSION;
3322 }
3323 else if (c == '<'
3324 && xp->xp_context == EXPAND_FUNCTIONS
3325 && vim_strchr(xp->xp_pattern, '(') == NULL)
3326 {
3327 /* Function name can start with "<SNR>" */
3328 break;
3329 }
3330 else if (cmdidx != CMD_let || got_eq)
3331 {
3332 if (c == '"') /* string */
3333 {
3334 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3335 if (c == '\\' && xp->xp_pattern[1] != NUL)
3336 ++xp->xp_pattern;
3337 xp->xp_context = EXPAND_NOTHING;
3338 }
3339 else if (c == '\'') /* literal string */
3340 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003341 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3343 /* skip */ ;
3344 xp->xp_context = EXPAND_NOTHING;
3345 }
3346 else if (c == '|')
3347 {
3348 if (xp->xp_pattern[1] == '|')
3349 {
3350 ++xp->xp_pattern;
3351 xp->xp_context = EXPAND_EXPRESSION;
3352 }
3353 else
3354 xp->xp_context = EXPAND_COMMANDS;
3355 }
3356 else
3357 xp->xp_context = EXPAND_EXPRESSION;
3358 }
3359 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360 /* Doesn't look like something valid, expand as an expression
3361 * anyway. */
3362 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 arg = xp->xp_pattern;
3364 if (*arg != NUL)
3365 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3366 /* skip */ ;
3367 }
3368 xp->xp_pattern = arg;
3369}
3370
3371#endif /* FEAT_CMDL_COMPL */
3372
3373/*
3374 * ":1,25call func(arg1, arg2)" function call.
3375 */
3376 void
3377ex_call(eap)
3378 exarg_T *eap;
3379{
3380 char_u *arg = eap->arg;
3381 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003383 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003385 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 linenr_T lnum;
3387 int doesrange;
3388 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003391 if (eap->skip)
3392 {
3393 /* trans_function_name() doesn't work well when skipping, use eval0()
3394 * instead to skip to any following command, e.g. for:
3395 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003396 ++emsg_skip;
3397 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3398 clear_tv(&rettv);
3399 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003400 return;
3401 }
3402
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003403 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003404 if (fudi.fd_newkey != NULL)
3405 {
3406 /* Still need to give an error message for missing key. */
3407 EMSG2(_(e_dictkey), fudi.fd_newkey);
3408 vim_free(fudi.fd_newkey);
3409 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003410 if (tofree == NULL)
3411 return;
3412
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003413 /* Increase refcount on dictionary, it could get deleted when evaluating
3414 * the arguments. */
3415 if (fudi.fd_dict != NULL)
3416 ++fudi.fd_dict->dv_refcount;
3417
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003418 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003419 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003420 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar532c7802005-01-27 14:44:31 +00003422 /* Skip white space to allow ":call func ()". Not good, but required for
3423 * backward compatibility. */
3424 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427 if (*startarg != '(')
3428 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003429 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 goto end;
3431 }
3432
3433 /*
3434 * When skipping, evaluate the function once, to find the end of the
3435 * arguments.
3436 * When the function takes a range, this is discovered after the first
3437 * call, and the loop is broken.
3438 */
3439 if (eap->skip)
3440 {
3441 ++emsg_skip;
3442 lnum = eap->line2; /* do it once, also with an invalid range */
3443 }
3444 else
3445 lnum = eap->line1;
3446 for ( ; lnum <= eap->line2; ++lnum)
3447 {
3448 if (!eap->skip && eap->addr_count > 0)
3449 {
3450 curwin->w_cursor.lnum = lnum;
3451 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003452#ifdef FEAT_VIRTUALEDIT
3453 curwin->w_cursor.coladd = 0;
3454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003458 eap->line1, eap->line2, &doesrange,
3459 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 failed = TRUE;
3462 break;
3463 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003464
3465 /* Handle a function returning a Funcref, Dictionary or List. */
3466 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3467 {
3468 failed = TRUE;
3469 break;
3470 }
3471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003472 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (doesrange || eap->skip)
3474 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003477 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003479 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (aborting())
3481 break;
3482 }
3483 if (eap->skip)
3484 --emsg_skip;
3485
3486 if (!failed)
3487 {
3488 /* Check for trailing illegal characters and a following command. */
3489 if (!ends_excmd(*arg))
3490 {
3491 emsg_severe = TRUE;
3492 EMSG(_(e_trailing));
3493 }
3494 else
3495 eap->nextcmd = check_nextcmd(arg);
3496 }
3497
3498end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003499 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003500 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501}
3502
3503/*
3504 * ":unlet[!] var1 ... " command.
3505 */
3506 void
3507ex_unlet(eap)
3508 exarg_T *eap;
3509{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 ex_unletlock(eap, eap->arg, 0);
3511}
3512
3513/*
3514 * ":lockvar" and ":unlockvar" commands
3515 */
3516 void
3517ex_lockvar(eap)
3518 exarg_T *eap;
3519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003521 int deep = 2;
3522
3523 if (eap->forceit)
3524 deep = -1;
3525 else if (vim_isdigit(*arg))
3526 {
3527 deep = getdigits(&arg);
3528 arg = skipwhite(arg);
3529 }
3530
3531 ex_unletlock(eap, arg, deep);
3532}
3533
3534/*
3535 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3536 */
3537 static void
3538ex_unletlock(eap, argstart, deep)
3539 exarg_T *eap;
3540 char_u *argstart;
3541 int deep;
3542{
3543 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003546 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 do
3549 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003551 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3552 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 if (lv.ll_name == NULL)
3554 error = TRUE; /* error but continue parsing */
3555 if (name_end == NULL || (!vim_iswhite(*name_end)
3556 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 if (name_end != NULL)
3559 {
3560 emsg_severe = TRUE;
3561 EMSG(_(e_trailing));
3562 }
3563 if (!(eap->skip || error))
3564 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 break;
3566 }
3567
3568 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 {
3570 if (eap->cmdidx == CMD_unlet)
3571 {
3572 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3573 error = TRUE;
3574 }
3575 else
3576 {
3577 if (do_lock_var(&lv, name_end, deep,
3578 eap->cmdidx == CMD_lockvar) == FAIL)
3579 error = TRUE;
3580 }
3581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003583 if (!eap->skip)
3584 clear_lval(&lv);
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 arg = skipwhite(name_end);
3587 } while (!ends_excmd(*arg));
3588
3589 eap->nextcmd = check_nextcmd(arg);
3590}
3591
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 int forceit;
3597{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 int ret = OK;
3599 int cc;
3600
3601 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 cc = *name_end;
3604 *name_end = NUL;
3605
3606 /* Normal name or expanded name. */
3607 if (check_changedtick(lp->ll_name))
3608 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3614 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 else if (lp->ll_range)
3616 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003617 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618
3619 /* Delete a range of List items. */
3620 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3621 {
3622 li = lp->ll_li->li_next;
3623 listitem_remove(lp->ll_list, lp->ll_li);
3624 lp->ll_li = li;
3625 ++lp->ll_n1;
3626 }
3627 }
3628 else
3629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 /* unlet a List item. */
3632 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003635 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 }
3637
3638 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639}
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641/*
3642 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 */
3645 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
Bram Moolenaar33570922005-01-25 22:26:29 +00003650 hashtab_T *ht;
3651 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003653 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 ht = find_var_ht(name, &varname);
3656 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 hi = hash_find(ht, varname);
3659 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003660 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003661 di = HI2DI(hi);
3662 if (var_check_fixed(di->di_flags, name)
3663 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 return FAIL;
3665 delete_var(ht, hi);
3666 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003669 if (forceit)
3670 return OK;
3671 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return FAIL;
3673}
3674
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675/*
3676 * Lock or unlock variable indicated by "lp".
3677 * "deep" is the levels to go (-1 for unlimited);
3678 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3679 */
3680 static int
3681do_lock_var(lp, name_end, deep, lock)
3682 lval_T *lp;
3683 char_u *name_end;
3684 int deep;
3685 int lock;
3686{
3687 int ret = OK;
3688 int cc;
3689 dictitem_T *di;
3690
3691 if (deep == 0) /* nothing to do */
3692 return OK;
3693
3694 if (lp->ll_tv == NULL)
3695 {
3696 cc = *name_end;
3697 *name_end = NUL;
3698
3699 /* Normal name or expanded name. */
3700 if (check_changedtick(lp->ll_name))
3701 ret = FAIL;
3702 else
3703 {
3704 di = find_var(lp->ll_name, NULL);
3705 if (di == NULL)
3706 ret = FAIL;
3707 else
3708 {
3709 if (lock)
3710 di->di_flags |= DI_FLAGS_LOCK;
3711 else
3712 di->di_flags &= ~DI_FLAGS_LOCK;
3713 item_lock(&di->di_tv, deep, lock);
3714 }
3715 }
3716 *name_end = cc;
3717 }
3718 else if (lp->ll_range)
3719 {
3720 listitem_T *li = lp->ll_li;
3721
3722 /* (un)lock a range of List items. */
3723 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3724 {
3725 item_lock(&li->li_tv, deep, lock);
3726 li = li->li_next;
3727 ++lp->ll_n1;
3728 }
3729 }
3730 else if (lp->ll_list != NULL)
3731 /* (un)lock a List item. */
3732 item_lock(&lp->ll_li->li_tv, deep, lock);
3733 else
3734 /* un(lock) a Dictionary item. */
3735 item_lock(&lp->ll_di->di_tv, deep, lock);
3736
3737 return ret;
3738}
3739
3740/*
3741 * Lock or unlock an item. "deep" is nr of levels to go.
3742 */
3743 static void
3744item_lock(tv, deep, lock)
3745 typval_T *tv;
3746 int deep;
3747 int lock;
3748{
3749 static int recurse = 0;
3750 list_T *l;
3751 listitem_T *li;
3752 dict_T *d;
3753 hashitem_T *hi;
3754 int todo;
3755
3756 if (recurse >= DICT_MAXNEST)
3757 {
3758 EMSG(_("E743: variable nested too deep for (un)lock"));
3759 return;
3760 }
3761 if (deep == 0)
3762 return;
3763 ++recurse;
3764
3765 /* lock/unlock the item itself */
3766 if (lock)
3767 tv->v_lock |= VAR_LOCKED;
3768 else
3769 tv->v_lock &= ~VAR_LOCKED;
3770
3771 switch (tv->v_type)
3772 {
3773 case VAR_LIST:
3774 if ((l = tv->vval.v_list) != NULL)
3775 {
3776 if (lock)
3777 l->lv_lock |= VAR_LOCKED;
3778 else
3779 l->lv_lock &= ~VAR_LOCKED;
3780 if (deep < 0 || deep > 1)
3781 /* recursive: lock/unlock the items the List contains */
3782 for (li = l->lv_first; li != NULL; li = li->li_next)
3783 item_lock(&li->li_tv, deep - 1, lock);
3784 }
3785 break;
3786 case VAR_DICT:
3787 if ((d = tv->vval.v_dict) != NULL)
3788 {
3789 if (lock)
3790 d->dv_lock |= VAR_LOCKED;
3791 else
3792 d->dv_lock &= ~VAR_LOCKED;
3793 if (deep < 0 || deep > 1)
3794 {
3795 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003796 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3798 {
3799 if (!HASHITEM_EMPTY(hi))
3800 {
3801 --todo;
3802 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3803 }
3804 }
3805 }
3806 }
3807 }
3808 --recurse;
3809}
3810
Bram Moolenaara40058a2005-07-11 22:42:07 +00003811/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003812 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3813 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003814 */
3815 static int
3816tv_islocked(tv)
3817 typval_T *tv;
3818{
3819 return (tv->v_lock & VAR_LOCKED)
3820 || (tv->v_type == VAR_LIST
3821 && tv->vval.v_list != NULL
3822 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3823 || (tv->v_type == VAR_DICT
3824 && tv->vval.v_dict != NULL
3825 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3826}
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3829/*
3830 * Delete all "menutrans_" variables.
3831 */
3832 void
3833del_menutrans_vars()
3834{
Bram Moolenaar33570922005-01-25 22:26:29 +00003835 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003836 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003839 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003840 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003841 {
3842 if (!HASHITEM_EMPTY(hi))
3843 {
3844 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3846 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003847 }
3848 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850}
3851#endif
3852
3853#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3854
3855/*
3856 * Local string buffer for the next two functions to store a variable name
3857 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3858 * get_user_var_name().
3859 */
3860
3861static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3862
3863static char_u *varnamebuf = NULL;
3864static int varnamebuflen = 0;
3865
3866/*
3867 * Function to concatenate a prefix and a variable name.
3868 */
3869 static char_u *
3870cat_prefix_varname(prefix, name)
3871 int prefix;
3872 char_u *name;
3873{
3874 int len;
3875
3876 len = (int)STRLEN(name) + 3;
3877 if (len > varnamebuflen)
3878 {
3879 vim_free(varnamebuf);
3880 len += 10; /* some additional space */
3881 varnamebuf = alloc(len);
3882 if (varnamebuf == NULL)
3883 {
3884 varnamebuflen = 0;
3885 return NULL;
3886 }
3887 varnamebuflen = len;
3888 }
3889 *varnamebuf = prefix;
3890 varnamebuf[1] = ':';
3891 STRCPY(varnamebuf + 2, name);
3892 return varnamebuf;
3893}
3894
3895/*
3896 * Function given to ExpandGeneric() to obtain the list of user defined
3897 * (global/buffer/window/built-in) variable names.
3898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 char_u *
3900get_user_var_name(xp, idx)
3901 expand_T *xp;
3902 int idx;
3903{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003904 static long_u gdone;
3905 static long_u bdone;
3906 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003907#ifdef FEAT_WINDOWS
3908 static long_u tdone;
3909#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003910 static int vidx;
3911 static hashitem_T *hi;
3912 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003915 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003916 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003917#ifdef FEAT_WINDOWS
3918 tdone = 0;
3919#endif
3920 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003921
3922 /* Global variables */
3923 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003925 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003927 else
3928 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 while (HASHITEM_EMPTY(hi))
3930 ++hi;
3931 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3932 return cat_prefix_varname('g', hi->hi_key);
3933 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003935
3936 /* b: variables */
3937 ht = &curbuf->b_vars.dv_hashtab;
3938 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003942 else
3943 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 while (HASHITEM_EMPTY(hi))
3945 ++hi;
3946 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 return (char_u *)"b:changedtick";
3952 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003953
3954 /* w: variables */
3955 ht = &curwin->w_vars.dv_hashtab;
3956 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003958 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003960 else
3961 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003962 while (HASHITEM_EMPTY(hi))
3963 ++hi;
3964 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003966
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003967#ifdef FEAT_WINDOWS
3968 /* t: variables */
3969 ht = &curtab->tp_vars.dv_hashtab;
3970 if (tdone < ht->ht_used)
3971 {
3972 if (tdone++ == 0)
3973 hi = ht->ht_array;
3974 else
3975 ++hi;
3976 while (HASHITEM_EMPTY(hi))
3977 ++hi;
3978 return cat_prefix_varname('t', hi->hi_key);
3979 }
3980#endif
3981
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 /* v: variables */
3983 if (vidx < VV_LEN)
3984 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 vim_free(varnamebuf);
3987 varnamebuf = NULL;
3988 varnamebuflen = 0;
3989 return NULL;
3990}
3991
3992#endif /* FEAT_CMDL_COMPL */
3993
3994/*
3995 * types for expressions.
3996 */
3997typedef enum
3998{
3999 TYPE_UNKNOWN = 0
4000 , TYPE_EQUAL /* == */
4001 , TYPE_NEQUAL /* != */
4002 , TYPE_GREATER /* > */
4003 , TYPE_GEQUAL /* >= */
4004 , TYPE_SMALLER /* < */
4005 , TYPE_SEQUAL /* <= */
4006 , TYPE_MATCH /* =~ */
4007 , TYPE_NOMATCH /* !~ */
4008} exptype_T;
4009
4010/*
4011 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4014 */
4015
4016/*
4017 * Handle zero level expression.
4018 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004020 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * Return OK or FAIL.
4022 */
4023 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 char_u **nextcmd;
4028 int evaluate;
4029{
4030 int ret;
4031 char_u *p;
4032
4033 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (ret == FAIL || !ends_excmd(*p))
4036 {
4037 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 /*
4040 * Report the invalid expression unless the expression evaluation has
4041 * been cancelled due to an aborting error, an interrupt, or an
4042 * exception.
4043 */
4044 if (!aborting())
4045 EMSG2(_(e_invexpr2), arg);
4046 ret = FAIL;
4047 }
4048 if (nextcmd != NULL)
4049 *nextcmd = check_nextcmd(p);
4050
4051 return ret;
4052}
4053
4054/*
4055 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004056 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 *
4058 * "arg" must point to the first non-white of the expression.
4059 * "arg" is advanced to the next non-white after the recognized expression.
4060 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004061 * Note: "rettv.v_lock" is not set.
4062 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 * Return OK or FAIL.
4064 */
4065 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 int evaluate;
4070{
4071 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
4074 /*
4075 * Get the first variable.
4076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
4079
4080 if ((*arg)[0] == '?')
4081 {
4082 result = FALSE;
4083 if (evaluate)
4084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 int error = FALSE;
4086
4087 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090 if (error)
4091 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093
4094 /*
4095 * Get the second variable.
4096 */
4097 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100
4101 /*
4102 * Check for the ":".
4103 */
4104 if ((*arg)[0] != ':')
4105 {
4106 EMSG(_("E109: Missing ':' after '?'"));
4107 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110 }
4111
4112 /*
4113 * Get the third variable.
4114 */
4115 *arg = skipwhite(*arg + 1);
4116 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4117 {
4118 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 return FAIL;
4121 }
4122 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125
4126 return OK;
4127}
4128
4129/*
4130 * Handle first level expression:
4131 * expr2 || expr2 || expr2 logical OR
4132 *
4133 * "arg" must point to the first non-white of the expression.
4134 * "arg" is advanced to the next non-white after the recognized expression.
4135 *
4136 * Return OK or FAIL.
4137 */
4138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int evaluate;
4143{
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 long result;
4146 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
4150 * Get the first variable.
4151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 return FAIL;
4154
4155 /*
4156 * Repeat until there is no following "||".
4157 */
4158 first = TRUE;
4159 result = FALSE;
4160 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4161 {
4162 if (evaluate && first)
4163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (error)
4168 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 first = FALSE;
4170 }
4171
4172 /*
4173 * Get the second variable.
4174 */
4175 *arg = skipwhite(*arg + 2);
4176 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4177 return FAIL;
4178
4179 /*
4180 * Compute the result.
4181 */
4182 if (evaluate && !result)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 if (evaluate)
4191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 rettv->v_type = VAR_NUMBER;
4193 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 }
4196
4197 return OK;
4198}
4199
4200/*
4201 * Handle second level expression:
4202 * expr3 && expr3 && expr3 logical AND
4203 *
4204 * "arg" must point to the first non-white of the expression.
4205 * "arg" is advanced to the next non-white after the recognized expression.
4206 *
4207 * Return OK or FAIL.
4208 */
4209 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 int evaluate;
4214{
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 long result;
4217 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220 /*
4221 * Get the first variable.
4222 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return FAIL;
4225
4226 /*
4227 * Repeat until there is no following "&&".
4228 */
4229 first = TRUE;
4230 result = TRUE;
4231 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4232 {
4233 if (evaluate && first)
4234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004235 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (error)
4239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 first = FALSE;
4241 }
4242
4243 /*
4244 * Get the second variable.
4245 */
4246 *arg = skipwhite(*arg + 2);
4247 if (eval4(arg, &var2, evaluate && result) == FAIL)
4248 return FAIL;
4249
4250 /*
4251 * Compute the result.
4252 */
4253 if (evaluate && result)
4254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (error)
4259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 if (evaluate)
4262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 rettv->v_type = VAR_NUMBER;
4264 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 }
4267
4268 return OK;
4269}
4270
4271/*
4272 * Handle third level expression:
4273 * var1 == var2
4274 * var1 =~ var2
4275 * var1 != var2
4276 * var1 !~ var2
4277 * var1 > var2
4278 * var1 >= var2
4279 * var1 < var2
4280 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004281 * var1 is var2
4282 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 *
4284 * "arg" must point to the first non-white of the expression.
4285 * "arg" is advanced to the next non-white after the recognized expression.
4286 *
4287 * Return OK or FAIL.
4288 */
4289 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 int evaluate;
4294{
Bram Moolenaar33570922005-01-25 22:26:29 +00004295 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 char_u *p;
4297 int i;
4298 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004299 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int len = 2;
4301 long n1, n2;
4302 char_u *s1, *s2;
4303 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4304 regmatch_T regmatch;
4305 int ic;
4306 char_u *save_cpo;
4307
4308 /*
4309 * Get the first variable.
4310 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313
4314 p = *arg;
4315 switch (p[0])
4316 {
4317 case '=': if (p[1] == '=')
4318 type = TYPE_EQUAL;
4319 else if (p[1] == '~')
4320 type = TYPE_MATCH;
4321 break;
4322 case '!': if (p[1] == '=')
4323 type = TYPE_NEQUAL;
4324 else if (p[1] == '~')
4325 type = TYPE_NOMATCH;
4326 break;
4327 case '>': if (p[1] != '=')
4328 {
4329 type = TYPE_GREATER;
4330 len = 1;
4331 }
4332 else
4333 type = TYPE_GEQUAL;
4334 break;
4335 case '<': if (p[1] != '=')
4336 {
4337 type = TYPE_SMALLER;
4338 len = 1;
4339 }
4340 else
4341 type = TYPE_SEQUAL;
4342 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004343 case 'i': if (p[1] == 's')
4344 {
4345 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4346 len = 5;
4347 if (!vim_isIDc(p[len]))
4348 {
4349 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4350 type_is = TRUE;
4351 }
4352 }
4353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355
4356 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 */
4359 if (type != TYPE_UNKNOWN)
4360 {
4361 /* extra question mark appended: ignore case */
4362 if (p[len] == '?')
4363 {
4364 ic = TRUE;
4365 ++len;
4366 }
4367 /* extra '#' appended: match case */
4368 else if (p[len] == '#')
4369 {
4370 ic = FALSE;
4371 ++len;
4372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004373 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 else
4375 ic = p_ic;
4376
4377 /*
4378 * Get the second variable.
4379 */
4380 *arg = skipwhite(p + len);
4381 if (eval5(arg, &var2, evaluate) == FAIL)
4382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 return FAIL;
4385 }
4386
4387 if (evaluate)
4388 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 if (type_is && rettv->v_type != var2.v_type)
4390 {
4391 /* For "is" a different type always means FALSE, for "notis"
4392 * it means TRUE. */
4393 n1 = (type == TYPE_NEQUAL);
4394 }
4395 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4396 {
4397 if (type_is)
4398 {
4399 n1 = (rettv->v_type == var2.v_type
4400 && rettv->vval.v_list == var2.vval.v_list);
4401 if (type == TYPE_NEQUAL)
4402 n1 = !n1;
4403 }
4404 else if (rettv->v_type != var2.v_type
4405 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4406 {
4407 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004408 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004410 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004411 clear_tv(rettv);
4412 clear_tv(&var2);
4413 return FAIL;
4414 }
4415 else
4416 {
4417 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004418 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4419 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004420 if (type == TYPE_NEQUAL)
4421 n1 = !n1;
4422 }
4423 }
4424
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004425 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4426 {
4427 if (type_is)
4428 {
4429 n1 = (rettv->v_type == var2.v_type
4430 && rettv->vval.v_dict == var2.vval.v_dict);
4431 if (type == TYPE_NEQUAL)
4432 n1 = !n1;
4433 }
4434 else if (rettv->v_type != var2.v_type
4435 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4436 {
4437 if (rettv->v_type != var2.v_type)
4438 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4439 else
4440 EMSG(_("E736: Invalid operation for Dictionary"));
4441 clear_tv(rettv);
4442 clear_tv(&var2);
4443 return FAIL;
4444 }
4445 else
4446 {
4447 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004448 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4449 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004450 if (type == TYPE_NEQUAL)
4451 n1 = !n1;
4452 }
4453 }
4454
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4456 {
4457 if (rettv->v_type != var2.v_type
4458 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4459 {
4460 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004461 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004463 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 clear_tv(rettv);
4465 clear_tv(&var2);
4466 return FAIL;
4467 }
4468 else
4469 {
4470 /* Compare two Funcrefs for being equal or unequal. */
4471 if (rettv->vval.v_string == NULL
4472 || var2.vval.v_string == NULL)
4473 n1 = FALSE;
4474 else
4475 n1 = STRCMP(rettv->vval.v_string,
4476 var2.vval.v_string) == 0;
4477 if (type == TYPE_NEQUAL)
4478 n1 = !n1;
4479 }
4480 }
4481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004482#ifdef FEAT_FLOAT
4483 /*
4484 * If one of the two variables is a float, compare as a float.
4485 * When using "=~" or "!~", always compare as string.
4486 */
4487 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4488 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4489 {
4490 float_T f1, f2;
4491
4492 if (rettv->v_type == VAR_FLOAT)
4493 f1 = rettv->vval.v_float;
4494 else
4495 f1 = get_tv_number(rettv);
4496 if (var2.v_type == VAR_FLOAT)
4497 f2 = var2.vval.v_float;
4498 else
4499 f2 = get_tv_number(&var2);
4500 n1 = FALSE;
4501 switch (type)
4502 {
4503 case TYPE_EQUAL: n1 = (f1 == f2); break;
4504 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4505 case TYPE_GREATER: n1 = (f1 > f2); break;
4506 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4507 case TYPE_SMALLER: n1 = (f1 < f2); break;
4508 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4509 case TYPE_UNKNOWN:
4510 case TYPE_MATCH:
4511 case TYPE_NOMATCH: break; /* avoid gcc warning */
4512 }
4513 }
4514#endif
4515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 /*
4517 * If one of the two variables is a number, compare as a number.
4518 * When using "=~" or "!~", always compare as string.
4519 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004520 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 n1 = get_tv_number(rettv);
4524 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (n1 == n2); break;
4528 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4529 case TYPE_GREATER: n1 = (n1 > n2); break;
4530 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4531 case TYPE_SMALLER: n1 = (n1 < n2); break;
4532 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4533 case TYPE_UNKNOWN:
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH: break; /* avoid gcc warning */
4536 }
4537 }
4538 else
4539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540 s1 = get_tv_string_buf(rettv, buf1);
4541 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4543 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4544 else
4545 i = 0;
4546 n1 = FALSE;
4547 switch (type)
4548 {
4549 case TYPE_EQUAL: n1 = (i == 0); break;
4550 case TYPE_NEQUAL: n1 = (i != 0); break;
4551 case TYPE_GREATER: n1 = (i > 0); break;
4552 case TYPE_GEQUAL: n1 = (i >= 0); break;
4553 case TYPE_SMALLER: n1 = (i < 0); break;
4554 case TYPE_SEQUAL: n1 = (i <= 0); break;
4555
4556 case TYPE_MATCH:
4557 case TYPE_NOMATCH:
4558 /* avoid 'l' flag in 'cpoptions' */
4559 save_cpo = p_cpo;
4560 p_cpo = (char_u *)"";
4561 regmatch.regprog = vim_regcomp(s2,
4562 RE_MAGIC + RE_STRING);
4563 regmatch.rm_ic = ic;
4564 if (regmatch.regprog != NULL)
4565 {
4566 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4567 vim_free(regmatch.regprog);
4568 if (type == TYPE_NOMATCH)
4569 n1 = !n1;
4570 }
4571 p_cpo = save_cpo;
4572 break;
4573
4574 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4575 }
4576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 clear_tv(rettv);
4578 clear_tv(&var2);
4579 rettv->v_type = VAR_NUMBER;
4580 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 }
4583
4584 return OK;
4585}
4586
4587/*
4588 * Handle fourth level expression:
4589 * + number addition
4590 * - number subtraction
4591 * . string concatenation
4592 *
4593 * "arg" must point to the first non-white of the expression.
4594 * "arg" is advanced to the next non-white after the recognized expression.
4595 *
4596 * Return OK or FAIL.
4597 */
4598 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 int evaluate;
4603{
Bram Moolenaar33570922005-01-25 22:26:29 +00004604 typval_T var2;
4605 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 int op;
4607 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004608#ifdef FEAT_FLOAT
4609 float_T f1 = 0, f2 = 0;
4610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 char_u *s1, *s2;
4612 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4613 char_u *p;
4614
4615 /*
4616 * Get the first variable.
4617 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004618 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 return FAIL;
4620
4621 /*
4622 * Repeat computing, until no '+', '-' or '.' is following.
4623 */
4624 for (;;)
4625 {
4626 op = **arg;
4627 if (op != '+' && op != '-' && op != '.')
4628 break;
4629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630 if ((op != '+' || rettv->v_type != VAR_LIST)
4631#ifdef FEAT_FLOAT
4632 && (op == '.' || rettv->v_type != VAR_FLOAT)
4633#endif
4634 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004635 {
4636 /* For "list + ...", an illegal use of the first operand as
4637 * a number cannot be determined before evaluating the 2nd
4638 * operand: if this is also a list, all is ok.
4639 * For "something . ...", "something - ..." or "non-list + ...",
4640 * we know that the first operand needs to be a string or number
4641 * without evaluating the 2nd operand. So check before to avoid
4642 * side effects after an error. */
4643 if (evaluate && get_tv_string_chk(rettv) == NULL)
4644 {
4645 clear_tv(rettv);
4646 return FAIL;
4647 }
4648 }
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 /*
4651 * Get the second variable.
4652 */
4653 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004654 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 return FAIL;
4658 }
4659
4660 if (evaluate)
4661 {
4662 /*
4663 * Compute the result.
4664 */
4665 if (op == '.')
4666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004667 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4668 s2 = get_tv_string_buf_chk(&var2, buf2);
4669 if (s2 == NULL) /* type error ? */
4670 {
4671 clear_tv(rettv);
4672 clear_tv(&var2);
4673 return FAIL;
4674 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004675 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 clear_tv(rettv);
4677 rettv->v_type = VAR_STRING;
4678 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004680 else if (op == '+' && rettv->v_type == VAR_LIST
4681 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004682 {
4683 /* concatenate Lists */
4684 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4685 &var3) == FAIL)
4686 {
4687 clear_tv(rettv);
4688 clear_tv(&var2);
4689 return FAIL;
4690 }
4691 clear_tv(rettv);
4692 *rettv = var3;
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 else
4695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 int error = FALSE;
4697
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004698#ifdef FEAT_FLOAT
4699 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004700 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701 f1 = rettv->vval.v_float;
4702 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 else
4705#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 n1 = get_tv_number_chk(rettv, &error);
4708 if (error)
4709 {
4710 /* This can only happen for "list + non-list". For
4711 * "non-list + ..." or "something - ...", we returned
4712 * before evaluating the 2nd operand. */
4713 clear_tv(rettv);
4714 return FAIL;
4715 }
4716#ifdef FEAT_FLOAT
4717 if (var2.v_type == VAR_FLOAT)
4718 f1 = n1;
4719#endif
4720 }
4721#ifdef FEAT_FLOAT
4722 if (var2.v_type == VAR_FLOAT)
4723 {
4724 f2 = var2.vval.v_float;
4725 n2 = 0;
4726 }
4727 else
4728#endif
4729 {
4730 n2 = get_tv_number_chk(&var2, &error);
4731 if (error)
4732 {
4733 clear_tv(rettv);
4734 clear_tv(&var2);
4735 return FAIL;
4736 }
4737#ifdef FEAT_FLOAT
4738 if (rettv->v_type == VAR_FLOAT)
4739 f2 = n2;
4740#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743
4744#ifdef FEAT_FLOAT
4745 /* If there is a float on either side the result is a float. */
4746 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4747 {
4748 if (op == '+')
4749 f1 = f1 + f2;
4750 else
4751 f1 = f1 - f2;
4752 rettv->v_type = VAR_FLOAT;
4753 rettv->vval.v_float = f1;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756#endif
4757 {
4758 if (op == '+')
4759 n1 = n1 + n2;
4760 else
4761 n1 = n1 - n2;
4762 rettv->v_type = VAR_NUMBER;
4763 rettv->vval.v_number = n1;
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 }
4769 return OK;
4770}
4771
4772/*
4773 * Handle fifth level expression:
4774 * * number multiplication
4775 * / number division
4776 * % number modulo
4777 *
4778 * "arg" must point to the first non-white of the expression.
4779 * "arg" is advanced to the next non-white after the recognized expression.
4780 *
4781 * Return OK or FAIL.
4782 */
4783 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004784eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004788 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
Bram Moolenaar33570922005-01-25 22:26:29 +00004790 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 int op;
4792 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793#ifdef FEAT_FLOAT
4794 int use_float = FALSE;
4795 float_T f1 = 0, f2;
4796#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 /*
4800 * Get the first variable.
4801 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004802 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 return FAIL;
4804
4805 /*
4806 * Repeat computing, until no '*', '/' or '%' is following.
4807 */
4808 for (;;)
4809 {
4810 op = **arg;
4811 if (op != '*' && op != '/' && op != '%')
4812 break;
4813
4814 if (evaluate)
4815 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004816#ifdef FEAT_FLOAT
4817 if (rettv->v_type == VAR_FLOAT)
4818 {
4819 f1 = rettv->vval.v_float;
4820 use_float = TRUE;
4821 n1 = 0;
4822 }
4823 else
4824#endif
4825 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004826 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 if (error)
4828 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 else
4831 n1 = 0;
4832
4833 /*
4834 * Get the second variable.
4835 */
4836 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004837 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 return FAIL;
4839
4840 if (evaluate)
4841 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842#ifdef FEAT_FLOAT
4843 if (var2.v_type == VAR_FLOAT)
4844 {
4845 if (!use_float)
4846 {
4847 f1 = n1;
4848 use_float = TRUE;
4849 }
4850 f2 = var2.vval.v_float;
4851 n2 = 0;
4852 }
4853 else
4854#endif
4855 {
4856 n2 = get_tv_number_chk(&var2, &error);
4857 clear_tv(&var2);
4858 if (error)
4859 return FAIL;
4860#ifdef FEAT_FLOAT
4861 if (use_float)
4862 f2 = n2;
4863#endif
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866 /*
4867 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870#ifdef FEAT_FLOAT
4871 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873 if (op == '*')
4874 f1 = f1 * f2;
4875 else if (op == '/')
4876 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004877# ifdef VMS
4878 /* VMS crashes on divide by zero, work around it */
4879 if (f2 == 0.0)
4880 {
4881 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004882 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004884 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004885 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004886 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887 }
4888 else
4889 f1 = f1 / f2;
4890# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891 /* We rely on the floating point library to handle divide
4892 * by zero to result in "inf" and not a crash. */
4893 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004894# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004898 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 return FAIL;
4900 }
4901 rettv->v_type = VAR_FLOAT;
4902 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 if (op == '*')
4908 n1 = n1 * n2;
4909 else if (op == '/')
4910 {
4911 if (n2 == 0) /* give an error message? */
4912 {
4913 if (n1 == 0)
4914 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4915 else if (n1 < 0)
4916 n1 = -0x7fffffffL;
4917 else
4918 n1 = 0x7fffffffL;
4919 }
4920 else
4921 n1 = n1 / n2;
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 {
4925 if (n2 == 0) /* give an error message? */
4926 n1 = 0;
4927 else
4928 n1 = n1 % n2;
4929 }
4930 rettv->v_type = VAR_NUMBER;
4931 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 }
4935
4936 return OK;
4937}
4938
4939/*
4940 * Handle sixth level expression:
4941 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004942 * "string" string constant
4943 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 * &option-name option value
4945 * @r register contents
4946 * identifier variable value
4947 * function() function call
4948 * $VAR environment variable
4949 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004950 * [expr, expr] List
4951 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 *
4953 * Also handle:
4954 * ! in front logical NOT
4955 * - in front unary minus
4956 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 * trailing [] subscript in String or List
4958 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 *
4960 * "arg" must point to the first non-white of the expression.
4961 * "arg" is advanced to the next non-white after the recognized expression.
4962 *
4963 * Return OK or FAIL.
4964 */
4965 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004966eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004970 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 long n;
4973 int len;
4974 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 char_u *start_leader, *end_leader;
4976 int ret = OK;
4977 char_u *alias;
4978
4979 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985 /*
4986 * Skip '!' and '-' characters. They are handled later.
4987 */
4988 start_leader = *arg;
4989 while (**arg == '!' || **arg == '-' || **arg == '+')
4990 *arg = skipwhite(*arg + 1);
4991 end_leader = *arg;
4992
4993 switch (**arg)
4994 {
4995 /*
4996 * Number constant.
4997 */
4998 case '0':
4999 case '1':
5000 case '2':
5001 case '3':
5002 case '4':
5003 case '5':
5004 case '6':
5005 case '7':
5006 case '8':
5007 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 {
5009#ifdef FEAT_FLOAT
5010 char_u *p = skipdigits(*arg + 1);
5011 int get_float = FALSE;
5012
5013 /* We accept a float when the format matches
5014 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005015 * strict to avoid backwards compatibility problems.
5016 * Don't look for a float after the "." operator, so that
5017 * ":let vers = 1.2.3" doesn't fail. */
5018 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 get_float = TRUE;
5021 p = skipdigits(p + 2);
5022 if (*p == 'e' || *p == 'E')
5023 {
5024 ++p;
5025 if (*p == '-' || *p == '+')
5026 ++p;
5027 if (!vim_isdigit(*p))
5028 get_float = FALSE;
5029 else
5030 p = skipdigits(p + 1);
5031 }
5032 if (ASCII_ISALPHA(*p) || *p == '.')
5033 get_float = FALSE;
5034 }
5035 if (get_float)
5036 {
5037 float_T f;
5038
5039 *arg += string2float(*arg, &f);
5040 if (evaluate)
5041 {
5042 rettv->v_type = VAR_FLOAT;
5043 rettv->vval.v_float = f;
5044 }
5045 }
5046 else
5047#endif
5048 {
5049 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5050 *arg += len;
5051 if (evaluate)
5052 {
5053 rettv->v_type = VAR_NUMBER;
5054 rettv->vval.v_number = n;
5055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 /*
5061 * String constant: "string".
5062 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 break;
5065
5066 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070 break;
5071
5072 /*
5073 * List: [expr, expr]
5074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077
5078 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 * Dictionary: {key: val, key: val}
5080 */
5081 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5082 break;
5083
5084 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005085 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005087 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089
5090 /*
5091 * Environment variable: $VAR.
5092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 break;
5095
5096 /*
5097 * Register contents: @r.
5098 */
5099 case '@': ++*arg;
5100 if (evaluate)
5101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005103 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 if (**arg != NUL)
5106 ++*arg;
5107 break;
5108
5109 /*
5110 * nested expression: (expression).
5111 */
5112 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005113 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (**arg == ')')
5115 ++*arg;
5116 else if (ret == OK)
5117 {
5118 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 ret = FAIL;
5121 }
5122 break;
5123
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 break;
5126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127
5128 if (ret == NOTDONE)
5129 {
5130 /*
5131 * Must be a variable or function name.
5132 * Can also be a curly-braces kind of name: {expr}.
5133 */
5134 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005135 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 if (alias != NULL)
5137 s = alias;
5138
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005139 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 ret = FAIL;
5141 else
5142 {
5143 if (**arg == '(') /* recursive! */
5144 {
5145 /* If "s" is the name of a variable of type VAR_FUNC
5146 * use its contents. */
5147 s = deref_func_name(s, &len);
5148
5149 /* Invoke the function. */
5150 ret = get_func_tv(s, len, rettv, arg,
5151 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005152 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 /* Stop the expression evaluation when immediately
5154 * aborting on error, or when an interrupt occurred or
5155 * an exception was thrown but not caught. */
5156 if (aborting())
5157 {
5158 if (ret == OK)
5159 clear_tv(rettv);
5160 ret = FAIL;
5161 }
5162 }
5163 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005164 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005165 else
5166 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005168 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 }
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 *arg = skipwhite(*arg);
5172
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005173 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5174 * expr(expr). */
5175 if (ret == OK)
5176 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
5178 /*
5179 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5180 */
5181 if (ret == OK && evaluate && end_leader > start_leader)
5182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005183 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005184 int val = 0;
5185#ifdef FEAT_FLOAT
5186 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005187
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188 if (rettv->v_type == VAR_FLOAT)
5189 f = rettv->vval.v_float;
5190 else
5191#endif
5192 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005195 clear_tv(rettv);
5196 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005198 else
5199 {
5200 while (end_leader > start_leader)
5201 {
5202 --end_leader;
5203 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005204 {
5205#ifdef FEAT_FLOAT
5206 if (rettv->v_type == VAR_FLOAT)
5207 f = !f;
5208 else
5209#endif
5210 val = !val;
5211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 {
5214#ifdef FEAT_FLOAT
5215 if (rettv->v_type == VAR_FLOAT)
5216 f = -f;
5217 else
5218#endif
5219 val = -val;
5220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005221 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005222#ifdef FEAT_FLOAT
5223 if (rettv->v_type == VAR_FLOAT)
5224 {
5225 clear_tv(rettv);
5226 rettv->vval.v_float = f;
5227 }
5228 else
5229#endif
5230 {
5231 clear_tv(rettv);
5232 rettv->v_type = VAR_NUMBER;
5233 rettv->vval.v_number = val;
5234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237
5238 return ret;
5239}
5240
5241/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005242 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5243 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5245 */
5246 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005247eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005249 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252{
5253 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005254 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 long len = -1;
5257 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005261 if (rettv->v_type == VAR_FUNC
5262#ifdef FEAT_FLOAT
5263 || rettv->v_type == VAR_FLOAT
5264#endif
5265 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005267 if (verbose)
5268 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 return FAIL;
5270 }
5271
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 /*
5275 * dict.name
5276 */
5277 key = *arg + 1;
5278 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5279 ;
5280 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 }
5284 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 /*
5287 * something[idx]
5288 *
5289 * Get the (first) variable from inside the [].
5290 */
5291 *arg = skipwhite(*arg + 1);
5292 if (**arg == ':')
5293 empty1 = TRUE;
5294 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5295 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5297 {
5298 /* not a number or string */
5299 clear_tv(&var1);
5300 return FAIL;
5301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302
5303 /*
5304 * Get the second variable from inside the [:].
5305 */
5306 if (**arg == ':')
5307 {
5308 range = TRUE;
5309 *arg = skipwhite(*arg + 1);
5310 if (**arg == ']')
5311 empty2 = TRUE;
5312 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 if (!empty1)
5315 clear_tv(&var1);
5316 return FAIL;
5317 }
5318 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5319 {
5320 /* not a number or string */
5321 if (!empty1)
5322 clear_tv(&var1);
5323 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324 return FAIL;
5325 }
5326 }
5327
5328 /* Check for the ']'. */
5329 if (**arg != ']')
5330 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005331 if (verbose)
5332 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 clear_tv(&var1);
5334 if (range)
5335 clear_tv(&var2);
5336 return FAIL;
5337 }
5338 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 }
5340
5341 if (evaluate)
5342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 n1 = 0;
5344 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005346 n1 = get_tv_number(&var1);
5347 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 }
5349 if (range)
5350 {
5351 if (empty2)
5352 n2 = -1;
5353 else
5354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005355 n2 = get_tv_number(&var2);
5356 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358 }
5359
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 {
5362 case VAR_NUMBER:
5363 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 len = (long)STRLEN(s);
5366 if (range)
5367 {
5368 /* The resulting variable is a substring. If the indexes
5369 * are out of range the result is empty. */
5370 if (n1 < 0)
5371 {
5372 n1 = len + n1;
5373 if (n1 < 0)
5374 n1 = 0;
5375 }
5376 if (n2 < 0)
5377 n2 = len + n2;
5378 else if (n2 >= len)
5379 n2 = len;
5380 if (n1 >= len || n2 < 0 || n1 > n2)
5381 s = NULL;
5382 else
5383 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5384 }
5385 else
5386 {
5387 /* The resulting variable is a string of a single
5388 * character. If the index is too big or negative the
5389 * result is empty. */
5390 if (n1 >= len || n1 < 0)
5391 s = NULL;
5392 else
5393 s = vim_strnsave(s + n1, 1);
5394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005395 clear_tv(rettv);
5396 rettv->v_type = VAR_STRING;
5397 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 break;
5399
5400 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 if (n1 < 0)
5403 n1 = len + n1;
5404 if (!empty1 && (n1 < 0 || n1 >= len))
5405 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005406 /* For a range we allow invalid values and return an empty
5407 * list. A list index out of range is an error. */
5408 if (!range)
5409 {
5410 if (verbose)
5411 EMSGN(_(e_listidx), n1);
5412 return FAIL;
5413 }
5414 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 }
5416 if (range)
5417 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005418 list_T *l;
5419 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420
5421 if (n2 < 0)
5422 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005423 else if (n2 >= len)
5424 n2 = len - 1;
5425 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005426 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 l = list_alloc();
5428 if (l == NULL)
5429 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 n1 <= n2; ++n1)
5432 {
5433 if (list_append_tv(l, &item->li_tv) == FAIL)
5434 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005435 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 return FAIL;
5437 }
5438 item = item->li_next;
5439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 clear_tv(rettv);
5441 rettv->v_type = VAR_LIST;
5442 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005443 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 else
5446 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005447 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 clear_tv(rettv);
5449 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
5451 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452
5453 case VAR_DICT:
5454 if (range)
5455 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005456 if (verbose)
5457 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 if (len == -1)
5459 clear_tv(&var1);
5460 return FAIL;
5461 }
5462 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005463 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464
5465 if (len == -1)
5466 {
5467 key = get_tv_string(&var1);
5468 if (*key == NUL)
5469 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005470 if (verbose)
5471 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 clear_tv(&var1);
5473 return FAIL;
5474 }
5475 }
5476
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005477 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005479 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005480 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 if (len == -1)
5482 clear_tv(&var1);
5483 if (item == NULL)
5484 return FAIL;
5485
5486 copy_tv(&item->di_tv, &var1);
5487 clear_tv(rettv);
5488 *rettv = var1;
5489 }
5490 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 }
5492 }
5493
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 return OK;
5495}
5496
5497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 * Get an option value.
5499 * "arg" points to the '&' or '+' before the option name.
5500 * "arg" is advanced to character after the option name.
5501 * Return OK or FAIL.
5502 */
5503 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 int evaluate;
5508{
5509 char_u *option_end;
5510 long numval;
5511 char_u *stringval;
5512 int opt_type;
5513 int c;
5514 int working = (**arg == '+'); /* has("+option") */
5515 int ret = OK;
5516 int opt_flags;
5517
5518 /*
5519 * Isolate the option name and find its value.
5520 */
5521 option_end = find_option_end(arg, &opt_flags);
5522 if (option_end == NULL)
5523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 EMSG2(_("E112: Option name missing: %s"), *arg);
5526 return FAIL;
5527 }
5528
5529 if (!evaluate)
5530 {
5531 *arg = option_end;
5532 return OK;
5533 }
5534
5535 c = *option_end;
5536 *option_end = NUL;
5537 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 if (opt_type == -3) /* invalid name */
5541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 EMSG2(_("E113: Unknown option: %s"), *arg);
5544 ret = FAIL;
5545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
5548 if (opt_type == -2) /* hidden string option */
5549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 rettv->v_type = VAR_STRING;
5551 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553 else if (opt_type == -1) /* hidden number option */
5554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 rettv->v_type = VAR_NUMBER;
5556 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558 else if (opt_type == 1) /* number option */
5559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 rettv->v_type = VAR_NUMBER;
5561 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 else /* string option */
5564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 rettv->v_type = VAR_STRING;
5566 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 }
5569 else if (working && (opt_type == -2 || opt_type == -1))
5570 ret = FAIL;
5571
5572 *option_end = c; /* put back for error messages */
5573 *arg = option_end;
5574
5575 return ret;
5576}
5577
5578/*
5579 * Allocate a variable for a string constant.
5580 * Return OK or FAIL.
5581 */
5582 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 int evaluate;
5587{
5588 char_u *p;
5589 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 int extra = 0;
5591
5592 /*
5593 * Find the end of the string, skipping backslashed characters.
5594 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005595 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {
5597 if (*p == '\\' && p[1] != NUL)
5598 {
5599 ++p;
5600 /* A "\<x>" form occupies at least 4 characters, and produces up
5601 * to 6 characters: reserve space for 2 extra */
5602 if (*p == '<')
5603 extra += 2;
5604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
5606
5607 if (*p != '"')
5608 {
5609 EMSG2(_("E114: Missing quote: %s"), *arg);
5610 return FAIL;
5611 }
5612
5613 /* If only parsing, set *arg and return here */
5614 if (!evaluate)
5615 {
5616 *arg = p + 1;
5617 return OK;
5618 }
5619
5620 /*
5621 * Copy the string into allocated memory, handling backslashed
5622 * characters.
5623 */
5624 name = alloc((unsigned)(p - *arg + extra));
5625 if (name == NULL)
5626 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 rettv->v_type = VAR_STRING;
5628 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 if (*p == '\\')
5633 {
5634 switch (*++p)
5635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 case 'b': *name++ = BS; ++p; break;
5637 case 'e': *name++ = ESC; ++p; break;
5638 case 'f': *name++ = FF; ++p; break;
5639 case 'n': *name++ = NL; ++p; break;
5640 case 'r': *name++ = CAR; ++p; break;
5641 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
5643 case 'X': /* hex: "\x1", "\x12" */
5644 case 'x':
5645 case 'u': /* Unicode: "\u0023" */
5646 case 'U':
5647 if (vim_isxdigit(p[1]))
5648 {
5649 int n, nr;
5650 int c = toupper(*p);
5651
5652 if (c == 'X')
5653 n = 2;
5654 else
5655 n = 4;
5656 nr = 0;
5657 while (--n >= 0 && vim_isxdigit(p[1]))
5658 {
5659 ++p;
5660 nr = (nr << 4) + hex2nr(*p);
5661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663#ifdef FEAT_MBYTE
5664 /* For "\u" store the number according to
5665 * 'encoding'. */
5666 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 else
5669#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 break;
5673
5674 /* octal: "\1", "\12", "\123" */
5675 case '0':
5676 case '1':
5677 case '2':
5678 case '3':
5679 case '4':
5680 case '5':
5681 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 case '7': *name = *p++ - '0';
5683 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 *name = (*name << 3) + *p++ - '0';
5686 if (*p >= '0' && *p <= '7')
5687 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 break;
5691
5692 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 if (extra != 0)
5695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 break;
5698 }
5699 /* FALLTHROUGH */
5700
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 break;
5703 }
5704 }
5705 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 *arg = p + 1;
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 return OK;
5713}
5714
5715/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 * Return OK or FAIL.
5718 */
5719 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005720get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 int evaluate;
5724{
5725 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005726 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 int reduce = 0;
5728
5729 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005731 */
5732 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 break;
5738 ++reduce;
5739 ++p;
5740 }
5741 }
5742
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 return FAIL;
5747 }
5748
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 if (!evaluate)
5751 {
5752 *arg = p + 1;
5753 return OK;
5754 }
5755
5756 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 */
5759 str = alloc((unsigned)((p - *arg) - reduce));
5760 if (str == NULL)
5761 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 rettv->v_type = VAR_STRING;
5763 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 break;
5771 ++p;
5772 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 *arg = p + 1;
5777
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 return OK;
5779}
5780
5781/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782 * Allocate a variable for a List and fill it from "*arg".
5783 * Return OK or FAIL.
5784 */
5785 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005786get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789 int evaluate;
5790{
Bram Moolenaar33570922005-01-25 22:26:29 +00005791 list_T *l = NULL;
5792 typval_T tv;
5793 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005794
5795 if (evaluate)
5796 {
5797 l = list_alloc();
5798 if (l == NULL)
5799 return FAIL;
5800 }
5801
5802 *arg = skipwhite(*arg + 1);
5803 while (**arg != ']' && **arg != NUL)
5804 {
5805 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5806 goto failret;
5807 if (evaluate)
5808 {
5809 item = listitem_alloc();
5810 if (item != NULL)
5811 {
5812 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005813 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814 list_append(l, item);
5815 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005816 else
5817 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 }
5819
5820 if (**arg == ']')
5821 break;
5822 if (**arg != ',')
5823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 goto failret;
5826 }
5827 *arg = skipwhite(*arg + 1);
5828 }
5829
5830 if (**arg != ']')
5831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833failret:
5834 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005835 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 return FAIL;
5837 }
5838
5839 *arg = skipwhite(*arg + 1);
5840 if (evaluate)
5841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005842 rettv->v_type = VAR_LIST;
5843 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 ++l->lv_refcount;
5845 }
5846
5847 return OK;
5848}
5849
5850/*
5851 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005852 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005854 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855list_alloc()
5856{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005857 list_T *l;
5858
5859 l = (list_T *)alloc_clear(sizeof(list_T));
5860 if (l != NULL)
5861 {
5862 /* Prepend the list to the list of lists for garbage collection. */
5863 if (first_list != NULL)
5864 first_list->lv_used_prev = l;
5865 l->lv_used_prev = NULL;
5866 l->lv_used_next = first_list;
5867 first_list = l;
5868 }
5869 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870}
5871
5872/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005873 * Allocate an empty list for a return value.
5874 * Returns OK or FAIL.
5875 */
5876 static int
5877rettv_list_alloc(rettv)
5878 typval_T *rettv;
5879{
5880 list_T *l = list_alloc();
5881
5882 if (l == NULL)
5883 return FAIL;
5884
5885 rettv->vval.v_list = l;
5886 rettv->v_type = VAR_LIST;
5887 ++l->lv_refcount;
5888 return OK;
5889}
5890
5891/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 * Unreference a list: decrement the reference count and free it when it
5893 * becomes zero.
5894 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005895 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005899 if (l != NULL && --l->lv_refcount <= 0)
5900 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901}
5902
5903/*
5904 * Free a list, including all items it points to.
5905 * Ignores the reference count.
5906 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005907 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005908list_free(l, recurse)
5909 list_T *l;
5910 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911{
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005914 /* Remove the list from the list of lists for garbage collection. */
5915 if (l->lv_used_prev == NULL)
5916 first_list = l->lv_used_next;
5917 else
5918 l->lv_used_prev->lv_used_next = l->lv_used_next;
5919 if (l->lv_used_next != NULL)
5920 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5921
Bram Moolenaard9fba312005-06-26 22:34:35 +00005922 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005924 /* Remove the item before deleting it. */
5925 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005926 if (recurse || (item->li_tv.v_type != VAR_LIST
5927 && item->li_tv.v_type != VAR_DICT))
5928 clear_tv(&item->li_tv);
5929 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 }
5931 vim_free(l);
5932}
5933
5934/*
5935 * Allocate a list item.
5936 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005937 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938listitem_alloc()
5939{
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941}
5942
5943/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005944 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 */
5946 static void
5947listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005950 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 vim_free(item);
5952}
5953
5954/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005955 * Remove a list item from a List and free it. Also clears the value.
5956 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005957 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005958listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005959 list_T *l;
5960 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005961{
5962 list_remove(l, item, item);
5963 listitem_free(item);
5964}
5965
5966/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 * Get the number of items in a list.
5968 */
5969 static long
5970list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 if (l == NULL)
5974 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005975 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976}
5977
5978/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005979 * Return TRUE when two lists have exactly the same values.
5980 */
5981 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005982list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 list_T *l1;
5984 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005985 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005986 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005987{
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005990 if (l1 == NULL || l2 == NULL)
5991 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005992 if (l1 == l2)
5993 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005994 if (list_len(l1) != list_len(l2))
5995 return FALSE;
5996
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 for (item1 = l1->lv_first, item2 = l2->lv_first;
5998 item1 != NULL && item2 != NULL;
5999 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006000 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001 return FALSE;
6002 return item1 == NULL && item2 == NULL;
6003}
6004
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006005#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6006 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007/*
6008 * Return the dictitem that an entry in a hashtable points to.
6009 */
6010 dictitem_T *
6011dict_lookup(hi)
6012 hashitem_T *hi;
6013{
6014 return HI2DI(hi);
6015}
6016#endif
6017
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006019 * Return TRUE when two dictionaries have exactly the same key/values.
6020 */
6021 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006022dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 dict_T *d1;
6024 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006025 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006026 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 hashitem_T *hi;
6029 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006030 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006031
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006032 if (d1 == NULL || d2 == NULL)
6033 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006034 if (d1 == d2)
6035 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006036 if (dict_len(d1) != dict_len(d2))
6037 return FALSE;
6038
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006039 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006042 if (!HASHITEM_EMPTY(hi))
6043 {
6044 item2 = dict_find(d2, hi->hi_key, -1);
6045 if (item2 == NULL)
6046 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006047 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048 return FALSE;
6049 --todo;
6050 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 }
6052 return TRUE;
6053}
6054
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055static int tv_equal_recurse_limit;
6056
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006057/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006058 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006059 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006060 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006061 */
6062 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006063tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 typval_T *tv1;
6065 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006066 int ic; /* ignore case */
6067 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068{
6069 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006070 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006072 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006073
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006074 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006075 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006077 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006078 * recursiveness to a limit. We guess they are equal then.
6079 * A fixed limit has the problem of still taking an awful long time.
6080 * Reduce the limit every time running into it. That should work fine for
6081 * deeply linked structures that are not recursively linked and catch
6082 * recursiveness quickly. */
6083 if (!recursive)
6084 tv_equal_recurse_limit = 1000;
6085 if (recursive_cnt >= tv_equal_recurse_limit)
6086 {
6087 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006088 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006090
6091 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006093 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094 ++recursive_cnt;
6095 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6096 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006097 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098
6099 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 ++recursive_cnt;
6101 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6102 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006103 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104
6105 case VAR_FUNC:
6106 return (tv1->vval.v_string != NULL
6107 && tv2->vval.v_string != NULL
6108 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6109
6110 case VAR_NUMBER:
6111 return tv1->vval.v_number == tv2->vval.v_number;
6112
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006113#ifdef FEAT_FLOAT
6114 case VAR_FLOAT:
6115 return tv1->vval.v_float == tv2->vval.v_float;
6116#endif
6117
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006118 case VAR_STRING:
6119 s1 = get_tv_string_buf(tv1, buf1);
6120 s2 = get_tv_string_buf(tv2, buf2);
6121 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006122 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006123
6124 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 return TRUE;
6126}
6127
6128/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 * Locate item with index "n" in list "l" and return it.
6130 * A negative index is counted from the end; -1 is the last item.
6131 * Returns NULL when "n" is out of range.
6132 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006133 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 long n;
6137{
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 long idx;
6140
6141 if (l == NULL)
6142 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006143
6144 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006146 n = l->lv_len + n;
6147
6148 /* Check for index out of range. */
6149 if (n < 0 || n >= l->lv_len)
6150 return NULL;
6151
6152 /* When there is a cached index may start search from there. */
6153 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006155 if (n < l->lv_idx / 2)
6156 {
6157 /* closest to the start of the list */
6158 item = l->lv_first;
6159 idx = 0;
6160 }
6161 else if (n > (l->lv_idx + l->lv_len) / 2)
6162 {
6163 /* closest to the end of the list */
6164 item = l->lv_last;
6165 idx = l->lv_len - 1;
6166 }
6167 else
6168 {
6169 /* closest to the cached index */
6170 item = l->lv_idx_item;
6171 idx = l->lv_idx;
6172 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 }
6174 else
6175 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006176 if (n < l->lv_len / 2)
6177 {
6178 /* closest to the start of the list */
6179 item = l->lv_first;
6180 idx = 0;
6181 }
6182 else
6183 {
6184 /* closest to the end of the list */
6185 item = l->lv_last;
6186 idx = l->lv_len - 1;
6187 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006189
6190 while (n > idx)
6191 {
6192 /* search forward */
6193 item = item->li_next;
6194 ++idx;
6195 }
6196 while (n < idx)
6197 {
6198 /* search backward */
6199 item = item->li_prev;
6200 --idx;
6201 }
6202
6203 /* cache the used index */
6204 l->lv_idx = idx;
6205 l->lv_idx_item = item;
6206
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006207 return item;
6208}
6209
6210/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006211 * Get list item "l[idx]" as a number.
6212 */
6213 static long
6214list_find_nr(l, idx, errorp)
6215 list_T *l;
6216 long idx;
6217 int *errorp; /* set to TRUE when something wrong */
6218{
6219 listitem_T *li;
6220
6221 li = list_find(l, idx);
6222 if (li == NULL)
6223 {
6224 if (errorp != NULL)
6225 *errorp = TRUE;
6226 return -1L;
6227 }
6228 return get_tv_number_chk(&li->li_tv, errorp);
6229}
6230
6231/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006232 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6233 */
6234 char_u *
6235list_find_str(l, idx)
6236 list_T *l;
6237 long idx;
6238{
6239 listitem_T *li;
6240
6241 li = list_find(l, idx - 1);
6242 if (li == NULL)
6243 {
6244 EMSGN(_(e_listidx), idx);
6245 return NULL;
6246 }
6247 return get_tv_string(&li->li_tv);
6248}
6249
6250/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006251 * Locate "item" list "l" and return its index.
6252 * Returns -1 when "item" is not in the list.
6253 */
6254 static long
6255list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006256 list_T *l;
6257 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006258{
6259 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006261
6262 if (l == NULL)
6263 return -1;
6264 idx = 0;
6265 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6266 ++idx;
6267 if (li == NULL)
6268 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006269 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006270}
6271
6272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273 * Append item "item" to the end of list "l".
6274 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006275 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 list_T *l;
6278 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279{
6280 if (l->lv_last == NULL)
6281 {
6282 /* empty list */
6283 l->lv_first = item;
6284 l->lv_last = item;
6285 item->li_prev = NULL;
6286 }
6287 else
6288 {
6289 l->lv_last->li_next = item;
6290 item->li_prev = l->lv_last;
6291 l->lv_last = item;
6292 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294 item->li_next = NULL;
6295}
6296
6297/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006298 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006299 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006301 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006303 list_T *l;
6304 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006306 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307
Bram Moolenaar05159a02005-02-26 23:04:13 +00006308 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006310 copy_tv(tv, &li->li_tv);
6311 list_append(l, li);
6312 return OK;
6313}
6314
6315/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006316 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006317 * Return FAIL when out of memory.
6318 */
6319 int
6320list_append_dict(list, dict)
6321 list_T *list;
6322 dict_T *dict;
6323{
6324 listitem_T *li = listitem_alloc();
6325
6326 if (li == NULL)
6327 return FAIL;
6328 li->li_tv.v_type = VAR_DICT;
6329 li->li_tv.v_lock = 0;
6330 li->li_tv.vval.v_dict = dict;
6331 list_append(list, li);
6332 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 return OK;
6334}
6335
6336/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006337 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006338 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006339 * Returns FAIL when out of memory.
6340 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006341 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006342list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006343 list_T *l;
6344 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006345 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006346{
6347 listitem_T *li = listitem_alloc();
6348
6349 if (li == NULL)
6350 return FAIL;
6351 list_append(l, li);
6352 li->li_tv.v_type = VAR_STRING;
6353 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006354 if (str == NULL)
6355 li->li_tv.vval.v_string = NULL;
6356 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006357 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006358 return FAIL;
6359 return OK;
6360}
6361
6362/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006363 * Append "n" to list "l".
6364 * Returns FAIL when out of memory.
6365 */
6366 static int
6367list_append_number(l, n)
6368 list_T *l;
6369 varnumber_T n;
6370{
6371 listitem_T *li;
6372
6373 li = listitem_alloc();
6374 if (li == NULL)
6375 return FAIL;
6376 li->li_tv.v_type = VAR_NUMBER;
6377 li->li_tv.v_lock = 0;
6378 li->li_tv.vval.v_number = n;
6379 list_append(l, li);
6380 return OK;
6381}
6382
6383/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006385 * If "item" is NULL append at the end.
6386 * Return FAIL when out of memory.
6387 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006389list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 list_T *l;
6391 typval_T *tv;
6392 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006393{
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395
6396 if (ni == NULL)
6397 return FAIL;
6398 copy_tv(tv, &ni->li_tv);
6399 if (item == NULL)
6400 /* Append new item at end of list. */
6401 list_append(l, ni);
6402 else
6403 {
6404 /* Insert new item before existing item. */
6405 ni->li_prev = item->li_prev;
6406 ni->li_next = item;
6407 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006408 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006410 ++l->lv_idx;
6411 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006413 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006415 l->lv_idx_item = NULL;
6416 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 }
6420 return OK;
6421}
6422
6423/*
6424 * Extend "l1" with "l2".
6425 * If "bef" is NULL append at the end, otherwise insert before this item.
6426 * Returns FAIL when out of memory.
6427 */
6428 static int
6429list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 list_T *l1;
6431 list_T *l2;
6432 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433{
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006435 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006437 /* We also quit the loop when we have inserted the original item count of
6438 * the list, avoid a hang when we extend a list with itself. */
6439 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6441 return FAIL;
6442 return OK;
6443}
6444
6445/*
6446 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6447 * Return FAIL when out of memory.
6448 */
6449 static int
6450list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 list_T *l1;
6452 list_T *l2;
6453 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454{
Bram Moolenaar33570922005-01-25 22:26:29 +00006455 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006457 if (l1 == NULL || l2 == NULL)
6458 return FAIL;
6459
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006461 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 if (l == NULL)
6463 return FAIL;
6464 tv->v_type = VAR_LIST;
6465 tv->vval.v_list = l;
6466
6467 /* append all items from the second list */
6468 return list_extend(l, l2, NULL);
6469}
6470
6471/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006472 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006474 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475 * Returns NULL when out of memory.
6476 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006478list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482{
Bram Moolenaar33570922005-01-25 22:26:29 +00006483 list_T *copy;
6484 listitem_T *item;
6485 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486
6487 if (orig == NULL)
6488 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489
6490 copy = list_alloc();
6491 if (copy != NULL)
6492 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006493 if (copyID != 0)
6494 {
6495 /* Do this before adding the items, because one of the items may
6496 * refer back to this list. */
6497 orig->lv_copyID = copyID;
6498 orig->lv_copylist = copy;
6499 }
6500 for (item = orig->lv_first; item != NULL && !got_int;
6501 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 {
6503 ni = listitem_alloc();
6504 if (ni == NULL)
6505 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507 {
6508 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6509 {
6510 vim_free(ni);
6511 break;
6512 }
6513 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006515 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516 list_append(copy, ni);
6517 }
6518 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 if (item != NULL)
6520 {
6521 list_unref(copy);
6522 copy = NULL;
6523 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 }
6525
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 return copy;
6527}
6528
6529/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006531 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006533 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006534list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006535 list_T *l;
6536 listitem_T *item;
6537 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538{
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 /* notify watchers */
6542 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006544 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 list_fix_watch(l, ip);
6546 if (ip == item2)
6547 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549
6550 if (item2->li_next == NULL)
6551 l->lv_last = item->li_prev;
6552 else
6553 item2->li_next->li_prev = item->li_prev;
6554 if (item->li_prev == NULL)
6555 l->lv_first = item2->li_next;
6556 else
6557 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006558 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559}
6560
6561/*
6562 * Return an allocated string with the string representation of a list.
6563 * May return NULL.
6564 */
6565 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006566list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006568 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569{
6570 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571
6572 if (tv->vval.v_list == NULL)
6573 return NULL;
6574 ga_init2(&ga, (int)sizeof(char), 80);
6575 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577 {
6578 vim_free(ga.ga_data);
6579 return NULL;
6580 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 ga_append(&ga, ']');
6582 ga_append(&ga, NUL);
6583 return (char_u *)ga.ga_data;
6584}
6585
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006586typedef struct join_S {
6587 char_u *s;
6588 char_u *tofree;
6589} join_T;
6590
6591 static int
6592list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6593 garray_T *gap; /* to store the result in */
6594 list_T *l;
6595 char_u *sep;
6596 int echo_style;
6597 int copyID;
6598 garray_T *join_gap; /* to keep each list item string */
6599{
6600 int i;
6601 join_T *p;
6602 int len;
6603 int sumlen = 0;
6604 int first = TRUE;
6605 char_u *tofree;
6606 char_u numbuf[NUMBUFLEN];
6607 listitem_T *item;
6608 char_u *s;
6609
6610 /* Stringify each item in the list. */
6611 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6612 {
6613 if (echo_style)
6614 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6615 else
6616 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6617 if (s == NULL)
6618 return FAIL;
6619
6620 len = (int)STRLEN(s);
6621 sumlen += len;
6622
6623 ga_grow(join_gap, 1);
6624 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6625 if (tofree != NULL || s != numbuf)
6626 {
6627 p->s = s;
6628 p->tofree = tofree;
6629 }
6630 else
6631 {
6632 p->s = vim_strnsave(s, len);
6633 p->tofree = p->s;
6634 }
6635
6636 line_breakcheck();
6637 }
6638
6639 /* Allocate result buffer with its total size, avoid re-allocation and
6640 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6641 if (join_gap->ga_len >= 2)
6642 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6643 if (ga_grow(gap, sumlen + 2) == FAIL)
6644 return FAIL;
6645
6646 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6647 {
6648 if (first)
6649 first = FALSE;
6650 else
6651 ga_concat(gap, sep);
6652 p = ((join_T *)join_gap->ga_data) + i;
6653
6654 if (p->s != NULL)
6655 ga_concat(gap, p->s);
6656 line_breakcheck();
6657 }
6658
6659 return OK;
6660}
6661
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006663 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006664 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006665 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006666 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006667 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006668list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006669 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006672 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006673 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006675 garray_T join_ga;
6676 int retval;
6677 join_T *p;
6678 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006680 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6681 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6682
6683 /* Dispose each item in join_ga. */
6684 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006686 p = (join_T *)join_ga.ga_data;
6687 for (i = 0; i < join_ga.ga_len; ++i)
6688 {
6689 vim_free(p->tofree);
6690 ++p;
6691 }
6692 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694
6695 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696}
6697
6698/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006699 * Garbage collection for lists and dictionaries.
6700 *
6701 * We use reference counts to be able to free most items right away when they
6702 * are no longer used. But for composite items it's possible that it becomes
6703 * unused while the reference count is > 0: When there is a recursive
6704 * reference. Example:
6705 * :let l = [1, 2, 3]
6706 * :let d = {9: l}
6707 * :let l[1] = d
6708 *
6709 * Since this is quite unusual we handle this with garbage collection: every
6710 * once in a while find out which lists and dicts are not referenced from any
6711 * variable.
6712 *
6713 * Here is a good reference text about garbage collection (refers to Python
6714 * but it applies to all reference-counting mechanisms):
6715 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006716 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006717
6718/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006719 * Do garbage collection for lists and dicts.
6720 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006721 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006722 int
6723garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006724{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006725 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006726 buf_T *buf;
6727 win_T *wp;
6728 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006729 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006730 int did_free;
6731 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006732#ifdef FEAT_WINDOWS
6733 tabpage_T *tp;
6734#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006735
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006736 /* Only do this once. */
6737 want_garbage_collect = FALSE;
6738 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006739 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006740
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006741 /* We advance by two because we add one for items referenced through
6742 * previous_funccal. */
6743 current_copyID += COPYID_INC;
6744 copyID = current_copyID;
6745
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 /*
6747 * 1. Go through all accessible variables and mark all lists and dicts
6748 * with copyID.
6749 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006750
6751 /* Don't free variables in the previous_funccal list unless they are only
6752 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006753 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006754 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6755 {
6756 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6757 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6758 }
6759
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 /* script-local variables */
6761 for (i = 1; i <= ga_scripts.ga_len; ++i)
6762 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6763
6764 /* buffer-local variables */
6765 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6766 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6767
6768 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006769 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6771
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006772#ifdef FEAT_WINDOWS
6773 /* tabpage-local variables */
6774 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6775 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6776#endif
6777
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 /* global variables */
6779 set_ref_in_ht(&globvarht, copyID);
6780
6781 /* function-local variables */
6782 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6783 {
6784 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6785 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6786 }
6787
Bram Moolenaard812df62008-11-09 12:46:09 +00006788 /* v: vars */
6789 set_ref_in_ht(&vimvarht, copyID);
6790
Bram Moolenaar1dced572012-04-05 16:54:08 +02006791#ifdef FEAT_LUA
6792 set_ref_in_lua(copyID);
6793#endif
6794
Bram Moolenaardb913952012-06-29 12:54:53 +02006795#ifdef FEAT_PYTHON
6796 set_ref_in_python(copyID);
6797#endif
6798
6799#ifdef FEAT_PYTHON3
6800 set_ref_in_python3(copyID);
6801#endif
6802
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006803 /*
6804 * 2. Free lists and dictionaries that are not referenced.
6805 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006806 did_free = free_unref_items(copyID);
6807
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006808 /*
6809 * 3. Check if any funccal can be freed now.
6810 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006811 for (pfc = &previous_funccal; *pfc != NULL; )
6812 {
6813 if (can_free_funccal(*pfc, copyID))
6814 {
6815 fc = *pfc;
6816 *pfc = fc->caller;
6817 free_funccal(fc, TRUE);
6818 did_free = TRUE;
6819 did_free_funccal = TRUE;
6820 }
6821 else
6822 pfc = &(*pfc)->caller;
6823 }
6824 if (did_free_funccal)
6825 /* When a funccal was freed some more items might be garbage
6826 * collected, so run again. */
6827 (void)garbage_collect();
6828
6829 return did_free;
6830}
6831
6832/*
6833 * Free lists and dictionaries that are no longer referenced.
6834 */
6835 static int
6836free_unref_items(copyID)
6837 int copyID;
6838{
6839 dict_T *dd;
6840 list_T *ll;
6841 int did_free = FALSE;
6842
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 */
6846 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006849 /* Free the Dictionary and ordinary items it contains, but don't
6850 * recurse into Lists and Dictionaries, they will be in the list
6851 * of dicts or list of lists. */
6852 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 did_free = TRUE;
6854
6855 /* restart, next dict may also have been freed */
6856 dd = first_dict;
6857 }
6858 else
6859 dd = dd->dv_used_next;
6860
6861 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 * Go through the list of lists and free items without the copyID.
6863 * But don't free a list that has a watcher (used in a for loop), these
6864 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 */
6866 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006867 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6868 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006870 /* Free the List and ordinary items it contains, but don't recurse
6871 * into Lists and Dictionaries, they will be in the list of dicts
6872 * or list of lists. */
6873 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 did_free = TRUE;
6875
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006876 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 ll = first_list;
6878 }
6879 else
6880 ll = ll->lv_used_next;
6881
6882 return did_free;
6883}
6884
6885/*
6886 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6887 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006888 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889set_ref_in_ht(ht, copyID)
6890 hashtab_T *ht;
6891 int copyID;
6892{
6893 int todo;
6894 hashitem_T *hi;
6895
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006896 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 for (hi = ht->ht_array; todo > 0; ++hi)
6898 if (!HASHITEM_EMPTY(hi))
6899 {
6900 --todo;
6901 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6902 }
6903}
6904
6905/*
6906 * Mark all lists and dicts referenced through list "l" with "copyID".
6907 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006908 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909set_ref_in_list(l, copyID)
6910 list_T *l;
6911 int copyID;
6912{
6913 listitem_T *li;
6914
6915 for (li = l->lv_first; li != NULL; li = li->li_next)
6916 set_ref_in_item(&li->li_tv, copyID);
6917}
6918
6919/*
6920 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6921 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006922 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923set_ref_in_item(tv, copyID)
6924 typval_T *tv;
6925 int copyID;
6926{
6927 dict_T *dd;
6928 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006929
6930 switch (tv->v_type)
6931 {
6932 case VAR_DICT:
6933 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006934 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006935 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 /* Didn't see this dict yet. */
6937 dd->dv_copyID = copyID;
6938 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006941
6942 case VAR_LIST:
6943 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006944 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006945 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006946 /* Didn't see this list yet. */
6947 ll->lv_copyID = copyID;
6948 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006949 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953}
6954
6955/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006956 * Allocate an empty header for a dictionary.
6957 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006958 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006959dict_alloc()
6960{
Bram Moolenaar33570922005-01-25 22:26:29 +00006961 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006962
Bram Moolenaar33570922005-01-25 22:26:29 +00006963 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006964 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006965 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006966 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 if (first_dict != NULL)
6968 first_dict->dv_used_prev = d;
6969 d->dv_used_next = first_dict;
6970 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006971 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006974 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006975 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006976 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006977 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006978 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006979 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980}
6981
6982/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006983 * Allocate an empty dict for a return value.
6984 * Returns OK or FAIL.
6985 */
6986 static int
6987rettv_dict_alloc(rettv)
6988 typval_T *rettv;
6989{
6990 dict_T *d = dict_alloc();
6991
6992 if (d == NULL)
6993 return FAIL;
6994
6995 rettv->vval.v_dict = d;
6996 rettv->v_type = VAR_DICT;
6997 ++d->dv_refcount;
6998 return OK;
6999}
7000
7001
7002/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007003 * Unreference a Dictionary: decrement the reference count and free it when it
7004 * becomes zero.
7005 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007006 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007007dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007010 if (d != NULL && --d->dv_refcount <= 0)
7011 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007012}
7013
7014/*
7015 * Free a Dictionary, including all items it contains.
7016 * Ignores the reference count.
7017 */
7018 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007019dict_free(d, recurse)
7020 dict_T *d;
7021 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007023 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007025 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 /* Remove the dict from the list of dicts for garbage collection. */
7028 if (d->dv_used_prev == NULL)
7029 first_dict = d->dv_used_next;
7030 else
7031 d->dv_used_prev->dv_used_next = d->dv_used_next;
7032 if (d->dv_used_next != NULL)
7033 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7034
7035 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007036 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007037 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007039 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040 if (!HASHITEM_EMPTY(hi))
7041 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007042 /* Remove the item before deleting it, just in case there is
7043 * something recursive causing trouble. */
7044 di = HI2DI(hi);
7045 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007046 if (recurse || (di->di_tv.v_type != VAR_LIST
7047 && di->di_tv.v_type != VAR_DICT))
7048 clear_tv(&di->di_tv);
7049 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 --todo;
7051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007053 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054 vim_free(d);
7055}
7056
7057/*
7058 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007059 * The "key" is copied to the new item.
7060 * Note that the value of the item "di_tv" still needs to be initialized!
7061 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007063 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007064dictitem_alloc(key)
7065 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066{
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007068
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007069 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007070 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007071 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007072 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007073 di->di_flags = 0;
7074 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076}
7077
7078/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007079 * Make a copy of a Dictionary item.
7080 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007081 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007082dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007083 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007084{
Bram Moolenaar33570922005-01-25 22:26:29 +00007085 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007087 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7088 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089 if (di != NULL)
7090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007092 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093 copy_tv(&org->di_tv, &di->di_tv);
7094 }
7095 return di;
7096}
7097
7098/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099 * Remove item "item" from Dictionary "dict" and free it.
7100 */
7101 static void
7102dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 dict_T *dict;
7104 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105{
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007107
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 if (HASHITEM_EMPTY(hi))
7110 EMSG2(_(e_intern2), "dictitem_remove()");
7111 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007112 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 dictitem_free(item);
7114}
7115
7116/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 * Free a dict item. Also clears the value.
7118 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007119 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007122{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007123 clear_tv(&item->di_tv);
7124 vim_free(item);
7125}
7126
7127/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7129 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007130 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 * Returns NULL when out of memory.
7132 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007134dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007137 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138{
Bram Moolenaar33570922005-01-25 22:26:29 +00007139 dict_T *copy;
7140 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143
7144 if (orig == NULL)
7145 return NULL;
7146
7147 copy = dict_alloc();
7148 if (copy != NULL)
7149 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007150 if (copyID != 0)
7151 {
7152 orig->dv_copyID = copyID;
7153 orig->dv_copydict = copy;
7154 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007155 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007156 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007158 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160 --todo;
7161
7162 di = dictitem_alloc(hi->hi_key);
7163 if (di == NULL)
7164 break;
7165 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 {
7167 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7168 copyID) == FAIL)
7169 {
7170 vim_free(di);
7171 break;
7172 }
7173 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 else
7175 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7176 if (dict_add(copy, di) == FAIL)
7177 {
7178 dictitem_free(di);
7179 break;
7180 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007182 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183
Bram Moolenaare9a41262005-01-15 22:18:47 +00007184 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007185 if (todo > 0)
7186 {
7187 dict_unref(copy);
7188 copy = NULL;
7189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 }
7191
7192 return copy;
7193}
7194
7195/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007197 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007199 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 dict_T *d;
7202 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203{
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205}
7206
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007208 * Add a number or string entry to dictionary "d".
7209 * When "str" is NULL use number "nr", otherwise use "str".
7210 * Returns FAIL when out of memory and when key already exists.
7211 */
7212 int
7213dict_add_nr_str(d, key, nr, str)
7214 dict_T *d;
7215 char *key;
7216 long nr;
7217 char_u *str;
7218{
7219 dictitem_T *item;
7220
7221 item = dictitem_alloc((char_u *)key);
7222 if (item == NULL)
7223 return FAIL;
7224 item->di_tv.v_lock = 0;
7225 if (str == NULL)
7226 {
7227 item->di_tv.v_type = VAR_NUMBER;
7228 item->di_tv.vval.v_number = nr;
7229 }
7230 else
7231 {
7232 item->di_tv.v_type = VAR_STRING;
7233 item->di_tv.vval.v_string = vim_strsave(str);
7234 }
7235 if (dict_add(d, item) == FAIL)
7236 {
7237 dictitem_free(item);
7238 return FAIL;
7239 }
7240 return OK;
7241}
7242
7243/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007244 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007245 * Returns FAIL when out of memory and when key already exists.
7246 */
7247 int
7248dict_add_list(d, key, list)
7249 dict_T *d;
7250 char *key;
7251 list_T *list;
7252{
7253 dictitem_T *item;
7254
7255 item = dictitem_alloc((char_u *)key);
7256 if (item == NULL)
7257 return FAIL;
7258 item->di_tv.v_lock = 0;
7259 item->di_tv.v_type = VAR_LIST;
7260 item->di_tv.vval.v_list = list;
7261 if (dict_add(d, item) == FAIL)
7262 {
7263 dictitem_free(item);
7264 return FAIL;
7265 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007266 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007267 return OK;
7268}
7269
7270/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007271 * Get the number of items in a Dictionary.
7272 */
7273 static long
7274dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007275 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277 if (d == NULL)
7278 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007279 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280}
7281
7282/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 * Find item "key[len]" in Dictionary "d".
7284 * If "len" is negative use strlen(key).
7285 * Returns NULL when not found.
7286 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007287 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 char_u *key;
7291 int len;
7292{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293#define AKEYLEN 200
7294 char_u buf[AKEYLEN];
7295 char_u *akey;
7296 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 if (len < 0)
7300 akey = key;
7301 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 tofree = akey = vim_strnsave(key, len);
7304 if (akey == NULL)
7305 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007306 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 else
7308 {
7309 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007310 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 akey = buf;
7312 }
7313
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 vim_free(tofree);
7316 if (HASHITEM_EMPTY(hi))
7317 return NULL;
7318 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319}
7320
7321/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007322 * Get a string item from a dictionary.
7323 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007324 * Returns NULL if the entry doesn't exist or out of memory.
7325 */
7326 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007327get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007328 dict_T *d;
7329 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007330 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007331{
7332 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007333 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007334
7335 di = dict_find(d, key, -1);
7336 if (di == NULL)
7337 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007338 s = get_tv_string(&di->di_tv);
7339 if (save && s != NULL)
7340 s = vim_strsave(s);
7341 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007342}
7343
7344/*
7345 * Get a number item from a dictionary.
7346 * Returns 0 if the entry doesn't exist or out of memory.
7347 */
7348 long
7349get_dict_number(d, key)
7350 dict_T *d;
7351 char_u *key;
7352{
7353 dictitem_T *di;
7354
7355 di = dict_find(d, key, -1);
7356 if (di == NULL)
7357 return 0;
7358 return get_tv_number(&di->di_tv);
7359}
7360
7361/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362 * Return an allocated string with the string representation of a Dictionary.
7363 * May return NULL.
7364 */
7365 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007366dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007368 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369{
7370 garray_T ga;
7371 int first = TRUE;
7372 char_u *tofree;
7373 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007379 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380 return NULL;
7381 ga_init2(&ga, (int)sizeof(char), 80);
7382 ga_append(&ga, '{');
7383
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007384 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 --todo;
7390
7391 if (first)
7392 first = FALSE;
7393 else
7394 ga_concat(&ga, (char_u *)", ");
7395
7396 tofree = string_quote(hi->hi_key, FALSE);
7397 if (tofree != NULL)
7398 {
7399 ga_concat(&ga, tofree);
7400 vim_free(tofree);
7401 }
7402 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007403 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 if (s != NULL)
7405 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 if (s == NULL)
7408 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007411 if (todo > 0)
7412 {
7413 vim_free(ga.ga_data);
7414 return NULL;
7415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416
7417 ga_append(&ga, '}');
7418 ga_append(&ga, NUL);
7419 return (char_u *)ga.ga_data;
7420}
7421
7422/*
7423 * Allocate a variable for a Dictionary and fill it from "*arg".
7424 * Return OK or FAIL. Returns NOTDONE for {expr}.
7425 */
7426 static int
7427get_dict_tv(arg, rettv, evaluate)
7428 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 int evaluate;
7431{
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 dict_T *d = NULL;
7433 typval_T tvkey;
7434 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007435 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439
7440 /*
7441 * First check if it's not a curly-braces thing: {expr}.
7442 * Must do this without evaluating, otherwise a function may be called
7443 * twice. Unfortunately this means we need to call eval1() twice for the
7444 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007445 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007447 if (*start != '}')
7448 {
7449 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7450 return FAIL;
7451 if (*start == '}')
7452 return NOTDONE;
7453 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454
7455 if (evaluate)
7456 {
7457 d = dict_alloc();
7458 if (d == NULL)
7459 return FAIL;
7460 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007461 tvkey.v_type = VAR_UNKNOWN;
7462 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463
7464 *arg = skipwhite(*arg + 1);
7465 while (**arg != '}' && **arg != NUL)
7466 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007467 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 goto failret;
7469 if (**arg != ':')
7470 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007471 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007472 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473 goto failret;
7474 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007475 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007477 key = get_tv_string_buf_chk(&tvkey, buf);
7478 if (key == NULL || *key == NUL)
7479 {
7480 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7481 if (key != NULL)
7482 EMSG(_(e_emptykey));
7483 clear_tv(&tvkey);
7484 goto failret;
7485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487
7488 *arg = skipwhite(*arg + 1);
7489 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7490 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007491 if (evaluate)
7492 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 goto failret;
7494 }
7495 if (evaluate)
7496 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007497 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 if (item != NULL)
7499 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007500 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 clear_tv(&tv);
7503 goto failret;
7504 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 item = dictitem_alloc(key);
7506 clear_tv(&tvkey);
7507 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007510 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 if (dict_add(d, item) == FAIL)
7512 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 }
7514 }
7515
7516 if (**arg == '}')
7517 break;
7518 if (**arg != ',')
7519 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007520 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 goto failret;
7522 }
7523 *arg = skipwhite(*arg + 1);
7524 }
7525
7526 if (**arg != '}')
7527 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007528 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529failret:
7530 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007531 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 return FAIL;
7533 }
7534
7535 *arg = skipwhite(*arg + 1);
7536 if (evaluate)
7537 {
7538 rettv->v_type = VAR_DICT;
7539 rettv->vval.v_dict = d;
7540 ++d->dv_refcount;
7541 }
7542
7543 return OK;
7544}
7545
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007547 * Return a string with the string representation of a variable.
7548 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007549 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007550 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007551 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007552 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007553 */
7554 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007555echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007556 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007557 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007558 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007559 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007560{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 static int recurse = 0;
7562 char_u *r = NULL;
7563
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007566 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567 *tofree = NULL;
7568 return NULL;
7569 }
7570 ++recurse;
7571
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007572 switch (tv->v_type)
7573 {
7574 case VAR_FUNC:
7575 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007576 r = tv->vval.v_string;
7577 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007578
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007579 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007580 if (tv->vval.v_list == NULL)
7581 {
7582 *tofree = NULL;
7583 r = NULL;
7584 }
7585 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7586 {
7587 *tofree = NULL;
7588 r = (char_u *)"[...]";
7589 }
7590 else
7591 {
7592 tv->vval.v_list->lv_copyID = copyID;
7593 *tofree = list2string(tv, copyID);
7594 r = *tofree;
7595 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007596 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007597
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599 if (tv->vval.v_dict == NULL)
7600 {
7601 *tofree = NULL;
7602 r = NULL;
7603 }
7604 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7605 {
7606 *tofree = NULL;
7607 r = (char_u *)"{...}";
7608 }
7609 else
7610 {
7611 tv->vval.v_dict->dv_copyID = copyID;
7612 *tofree = dict2string(tv, copyID);
7613 r = *tofree;
7614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007616
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617 case VAR_STRING:
7618 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 *tofree = NULL;
7620 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007622
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007623#ifdef FEAT_FLOAT
7624 case VAR_FLOAT:
7625 *tofree = NULL;
7626 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7627 r = numbuf;
7628 break;
7629#endif
7630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007631 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007632 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007634 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635
7636 --recurse;
7637 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007638}
7639
7640/*
7641 * Return a string with the string representation of a variable.
7642 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7643 * "numbuf" is used for a number.
7644 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007645 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007646 */
7647 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007648tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007650 char_u **tofree;
7651 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007652 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653{
7654 switch (tv->v_type)
7655 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 case VAR_FUNC:
7657 *tofree = string_quote(tv->vval.v_string, TRUE);
7658 return *tofree;
7659 case VAR_STRING:
7660 *tofree = string_quote(tv->vval.v_string, FALSE);
7661 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007662#ifdef FEAT_FLOAT
7663 case VAR_FLOAT:
7664 *tofree = NULL;
7665 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7666 return numbuf;
7667#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007669 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007672 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007674 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007675 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007676}
7677
7678/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 * Return string "str" in ' quotes, doubling ' characters.
7680 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 */
7683 static char_u *
7684string_quote(str, function)
7685 char_u *str;
7686 int function;
7687{
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007689 char_u *p, *r, *s;
7690
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 len = (function ? 13 : 3);
7692 if (str != NULL)
7693 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007694 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 for (p = str; *p != NUL; mb_ptr_adv(p))
7696 if (*p == '\'')
7697 ++len;
7698 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007699 s = r = alloc(len);
7700 if (r != NULL)
7701 {
7702 if (function)
7703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 r += 10;
7706 }
7707 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 if (str != NULL)
7710 for (p = str; *p != NUL; )
7711 {
7712 if (*p == '\'')
7713 *r++ = '\'';
7714 MB_COPY_CHAR(p, r);
7715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 if (function)
7718 *r++ = ')';
7719 *r++ = NUL;
7720 }
7721 return s;
7722}
7723
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007724#ifdef FEAT_FLOAT
7725/*
7726 * Convert the string "text" to a floating point number.
7727 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7728 * this always uses a decimal point.
7729 * Returns the length of the text that was consumed.
7730 */
7731 static int
7732string2float(text, value)
7733 char_u *text;
7734 float_T *value; /* result stored here */
7735{
7736 char *s = (char *)text;
7737 float_T f;
7738
7739 f = strtod(s, &s);
7740 *value = f;
7741 return (int)((char_u *)s - text);
7742}
7743#endif
7744
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 * Get the value of an environment variable.
7747 * "arg" is pointing to the '$'. It is advanced to after the name.
7748 * If the environment variable was not set, silently assume it is empty.
7749 * Always return OK.
7750 */
7751 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007752get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 int evaluate;
7756{
7757 char_u *string = NULL;
7758 int len;
7759 int cc;
7760 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007761 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
7763 ++*arg;
7764 name = *arg;
7765 len = get_env_len(arg);
7766 if (evaluate)
7767 {
7768 if (len != 0)
7769 {
7770 cc = name[len];
7771 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007772 /* first try vim_getenv(), fast for normal environment vars */
7773 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007775 {
7776 if (!mustfree)
7777 string = vim_strsave(string);
7778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 else
7780 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007781 if (mustfree)
7782 vim_free(string);
7783
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 /* next try expanding things like $VIM and ${HOME} */
7785 string = expand_env_save(name - 1);
7786 if (string != NULL && *string == '$')
7787 {
7788 vim_free(string);
7789 string = NULL;
7790 }
7791 }
7792 name[len] = cc;
7793 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->v_type = VAR_STRING;
7795 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 }
7797
7798 return OK;
7799}
7800
7801/*
7802 * Array with names and number of arguments of all internal functions
7803 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7804 */
7805static struct fst
7806{
7807 char *f_name; /* function name */
7808 char f_min_argc; /* minimal number of arguments */
7809 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007811 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812} functions[] =
7813{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007814#ifdef FEAT_FLOAT
7815 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007816 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007817#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007818 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007819 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {"append", 2, 2, f_append},
7821 {"argc", 0, 0, f_argc},
7822 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007823 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007824#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007825 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007826 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007827 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007830 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {"bufexists", 1, 1, f_bufexists},
7832 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7833 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7834 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7835 {"buflisted", 1, 1, f_buflisted},
7836 {"bufloaded", 1, 1, f_bufloaded},
7837 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007838 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"bufwinnr", 1, 1, f_bufwinnr},
7840 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007841 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007843#ifdef FEAT_FLOAT
7844 {"ceil", 1, 1, f_ceil},
7845#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007846 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"char2nr", 1, 1, f_char2nr},
7848 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007849 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007851#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007852 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007853 {"complete_add", 1, 1, f_complete_add},
7854 {"complete_check", 0, 0, f_complete_check},
7855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007858#ifdef FEAT_FLOAT
7859 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007860 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007861#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007862 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007864 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007865 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"delete", 1, 1, f_delete},
7867 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007868 {"diff_filler", 1, 1, f_diff_filler},
7869 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007870 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007872 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"eventhandler", 0, 0, f_eventhandler},
7874 {"executable", 1, 1, f_executable},
7875 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007876#ifdef FEAT_FLOAT
7877 {"exp", 1, 1, f_exp},
7878#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007879 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007880 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007881 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7883 {"filereadable", 1, 1, f_filereadable},
7884 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007885 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007886 {"finddir", 1, 3, f_finddir},
7887 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007888#ifdef FEAT_FLOAT
7889 {"float2nr", 1, 1, f_float2nr},
7890 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007891 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007893 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"fnamemodify", 2, 2, f_fnamemodify},
7895 {"foldclosed", 1, 1, f_foldclosed},
7896 {"foldclosedend", 1, 1, f_foldclosedend},
7897 {"foldlevel", 1, 1, f_foldlevel},
7898 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007899 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007902 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007903 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007904 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {"getbufvar", 2, 2, f_getbufvar},
7906 {"getchar", 0, 1, f_getchar},
7907 {"getcharmod", 0, 0, f_getcharmod},
7908 {"getcmdline", 0, 0, f_getcmdline},
7909 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007910 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007912 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007913 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"getfsize", 1, 1, f_getfsize},
7915 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007916 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007917 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007918 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007919 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007920 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007921 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007922 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007923 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007925 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007926 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"getwinposx", 0, 0, f_getwinposx},
7928 {"getwinposy", 0, 0, f_getwinposy},
7929 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007930 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007931 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007933 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007934 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007935 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7937 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7938 {"histadd", 2, 2, f_histadd},
7939 {"histdel", 1, 2, f_histdel},
7940 {"histget", 1, 2, f_histget},
7941 {"histnr", 1, 1, f_histnr},
7942 {"hlID", 1, 1, f_hlID},
7943 {"hlexists", 1, 1, f_hlexists},
7944 {"hostname", 0, 0, f_hostname},
7945 {"iconv", 3, 3, f_iconv},
7946 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007948 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007950 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"inputrestore", 0, 0, f_inputrestore},
7952 {"inputsave", 0, 0, f_inputsave},
7953 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007954 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007955 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007957 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007960 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"libcall", 3, 3, f_libcall},
7964 {"libcallnr", 3, 3, f_libcallnr},
7965 {"line", 1, 1, f_line},
7966 {"line2byte", 1, 1, f_line2byte},
7967 {"lispindent", 1, 1, f_lispindent},
7968 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007970 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007971 {"log10", 1, 1, f_log10},
7972#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007973#ifdef FEAT_LUA
7974 {"luaeval", 1, 2, f_luaeval},
7975#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007976 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007977 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007978 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007979 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007980 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007981 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007982 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007983 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007984 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007985 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007986 {"max", 1, 1, f_max},
7987 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007988#ifdef vim_mkdir
7989 {"mkdir", 1, 3, f_mkdir},
7990#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007991 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007992#ifdef FEAT_MZSCHEME
7993 {"mzeval", 1, 1, f_mzeval},
7994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {"nextnonblank", 1, 1, f_nextnonblank},
7996 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007997 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007998 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007999#ifdef FEAT_FLOAT
8000 {"pow", 2, 2, f_pow},
8001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008003 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008004 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008005#ifdef FEAT_PYTHON3
8006 {"py3eval", 1, 1, f_py3eval},
8007#endif
8008#ifdef FEAT_PYTHON
8009 {"pyeval", 1, 1, f_pyeval},
8010#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008011 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008012 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008013 {"reltime", 0, 2, f_reltime},
8014 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"remote_expr", 2, 3, f_remote_expr},
8016 {"remote_foreground", 1, 1, f_remote_foreground},
8017 {"remote_peek", 1, 2, f_remote_peek},
8018 {"remote_read", 1, 1, f_remote_read},
8019 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008020 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008022 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008024 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008025#ifdef FEAT_FLOAT
8026 {"round", 1, 1, f_round},
8027#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00008028 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008029 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008030 {"searchpair", 3, 7, f_searchpair},
8031 {"searchpairpos", 3, 7, f_searchpairpos},
8032 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"server2client", 2, 2, f_server2client},
8034 {"serverlist", 0, 0, f_serverlist},
8035 {"setbufvar", 3, 3, f_setbufvar},
8036 {"setcmdpos", 1, 1, f_setcmdpos},
8037 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008038 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008039 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008040 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008041 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008043 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008044 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008046 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008048#ifdef FEAT_FLOAT
8049 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008050 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008051#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008052 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008053 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008054 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008055 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008056 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008057#ifdef FEAT_FLOAT
8058 {"sqrt", 1, 1, f_sqrt},
8059 {"str2float", 1, 1, f_str2float},
8060#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008061 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008062 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008063 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064#ifdef HAVE_STRFTIME
8065 {"strftime", 1, 2, f_strftime},
8066#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"strlen", 1, 1, f_strlen},
8070 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008071 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008073 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {"submatch", 1, 1, f_submatch},
8075 {"substitute", 4, 4, f_substitute},
8076 {"synID", 3, 3, f_synID},
8077 {"synIDattr", 2, 3, f_synIDattr},
8078 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008079 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008080 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008081 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008082 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008083 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008084 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008085 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008086 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008087#ifdef FEAT_FLOAT
8088 {"tan", 1, 1, f_tan},
8089 {"tanh", 1, 1, f_tanh},
8090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008092 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"tolower", 1, 1, f_tolower},
8094 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008095 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096#ifdef FEAT_FLOAT
8097 {"trunc", 1, 1, f_trunc},
8098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008100 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008101 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008102 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"virtcol", 1, 1, f_virtcol},
8104 {"visualmode", 0, 1, f_visualmode},
8105 {"winbufnr", 1, 1, f_winbufnr},
8106 {"wincol", 0, 0, f_wincol},
8107 {"winheight", 1, 1, f_winheight},
8108 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008109 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008111 {"winrestview", 1, 1, f_winrestview},
8112 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008114 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008115 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116};
8117
8118#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8119
8120/*
8121 * Function given to ExpandGeneric() to obtain the list of internal
8122 * or user defined function names.
8123 */
8124 char_u *
8125get_function_name(xp, idx)
8126 expand_T *xp;
8127 int idx;
8128{
8129 static int intidx = -1;
8130 char_u *name;
8131
8132 if (idx == 0)
8133 intidx = -1;
8134 if (intidx < 0)
8135 {
8136 name = get_user_func_name(xp, idx);
8137 if (name != NULL)
8138 return name;
8139 }
8140 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8141 {
8142 STRCPY(IObuff, functions[intidx].f_name);
8143 STRCAT(IObuff, "(");
8144 if (functions[intidx].f_max_argc == 0)
8145 STRCAT(IObuff, ")");
8146 return IObuff;
8147 }
8148
8149 return NULL;
8150}
8151
8152/*
8153 * Function given to ExpandGeneric() to obtain the list of internal or
8154 * user defined variable or function names.
8155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 char_u *
8157get_expr_name(xp, idx)
8158 expand_T *xp;
8159 int idx;
8160{
8161 static int intidx = -1;
8162 char_u *name;
8163
8164 if (idx == 0)
8165 intidx = -1;
8166 if (intidx < 0)
8167 {
8168 name = get_function_name(xp, idx);
8169 if (name != NULL)
8170 return name;
8171 }
8172 return get_user_var_name(xp, ++intidx);
8173}
8174
8175#endif /* FEAT_CMDL_COMPL */
8176
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008177#if defined(EBCDIC) || defined(PROTO)
8178/*
8179 * Compare struct fst by function name.
8180 */
8181 static int
8182compare_func_name(s1, s2)
8183 const void *s1;
8184 const void *s2;
8185{
8186 struct fst *p1 = (struct fst *)s1;
8187 struct fst *p2 = (struct fst *)s2;
8188
8189 return STRCMP(p1->f_name, p2->f_name);
8190}
8191
8192/*
8193 * Sort the function table by function name.
8194 * The sorting of the table above is ASCII dependant.
8195 * On machines using EBCDIC we have to sort it.
8196 */
8197 static void
8198sortFunctions()
8199{
8200 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8201
8202 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8203}
8204#endif
8205
8206
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207/*
8208 * Find internal function in table above.
8209 * Return index, or -1 if not found
8210 */
8211 static int
8212find_internal_func(name)
8213 char_u *name; /* name of the function */
8214{
8215 int first = 0;
8216 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8217 int cmp;
8218 int x;
8219
8220 /*
8221 * Find the function name in the table. Binary search.
8222 */
8223 while (first <= last)
8224 {
8225 x = first + ((unsigned)(last - first) >> 1);
8226 cmp = STRCMP(name, functions[x].f_name);
8227 if (cmp < 0)
8228 last = x - 1;
8229 else if (cmp > 0)
8230 first = x + 1;
8231 else
8232 return x;
8233 }
8234 return -1;
8235}
8236
8237/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008238 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8239 * name it contains, otherwise return "name".
8240 */
8241 static char_u *
8242deref_func_name(name, lenp)
8243 char_u *name;
8244 int *lenp;
8245{
Bram Moolenaar33570922005-01-25 22:26:29 +00008246 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008247 int cc;
8248
8249 cc = name[*lenp];
8250 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008251 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008252 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008254 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008255 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008256 {
8257 *lenp = 0;
8258 return (char_u *)""; /* just in case */
8259 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008260 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008261 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008262 }
8263
8264 return name;
8265}
8266
8267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 * Allocate a variable for the result of a function.
8269 * Return OK or FAIL.
8270 */
8271 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008272get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8273 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 char_u *name; /* name of the function */
8275 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 char_u **arg; /* argument, pointing to the '(' */
8278 linenr_T firstline; /* first line of range */
8279 linenr_T lastline; /* last line of range */
8280 int *doesrange; /* return: function handled range */
8281 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008282 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 char_u *argp;
8285 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008286 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 int argcount = 0; /* number of arguments found */
8288
8289 /*
8290 * Get the arguments.
8291 */
8292 argp = *arg;
8293 while (argcount < MAX_FUNC_ARGS)
8294 {
8295 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8296 if (*argp == ')' || *argp == ',' || *argp == NUL)
8297 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8299 {
8300 ret = FAIL;
8301 break;
8302 }
8303 ++argcount;
8304 if (*argp != ',')
8305 break;
8306 }
8307 if (*argp == ')')
8308 ++argp;
8309 else
8310 ret = FAIL;
8311
8312 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008313 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008314 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 {
8317 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008318 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008320 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322
8323 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008324 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
8326 *arg = skipwhite(argp);
8327 return ret;
8328}
8329
8330
8331/*
8332 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008333 * Return OK when the function can't be called, FAIL otherwise.
8334 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 */
8336 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008337call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008338 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008339 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008343 typval_T *argvars; /* vars for arguments, must have "argcount"
8344 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 linenr_T firstline; /* first line of range */
8346 linenr_T lastline; /* last line of range */
8347 int *doesrange; /* return: function handled range */
8348 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352#define ERROR_UNKNOWN 0
8353#define ERROR_TOOMANY 1
8354#define ERROR_TOOFEW 2
8355#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008356#define ERROR_DICT 4
8357#define ERROR_NONE 5
8358#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 int error = ERROR_NONE;
8360 int i;
8361 int llen;
8362 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363#define FLEN_FIXED 40
8364 char_u fname_buf[FLEN_FIXED + 1];
8365 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008366 char_u *name;
8367
8368 /* Make a copy of the name, if it comes from a funcref variable it could
8369 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008370 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008371 if (name == NULL)
8372 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
8374 /*
8375 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8376 * Change <SNR>123_name() to K_SNR 123_name().
8377 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 llen = eval_fname_script(name);
8380 if (llen > 0)
8381 {
8382 fname_buf[0] = K_SPECIAL;
8383 fname_buf[1] = KS_EXTRA;
8384 fname_buf[2] = (int)KE_SNR;
8385 i = 3;
8386 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8387 {
8388 if (current_SID <= 0)
8389 error = ERROR_SCRIPT;
8390 else
8391 {
8392 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8393 i = (int)STRLEN(fname_buf);
8394 }
8395 }
8396 if (i + STRLEN(name + llen) < FLEN_FIXED)
8397 {
8398 STRCPY(fname_buf + i, name + llen);
8399 fname = fname_buf;
8400 }
8401 else
8402 {
8403 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8404 if (fname == NULL)
8405 error = ERROR_OTHER;
8406 else
8407 {
8408 mch_memmove(fname, fname_buf, (size_t)i);
8409 STRCPY(fname + i, name + llen);
8410 }
8411 }
8412 }
8413 else
8414 fname = name;
8415
8416 *doesrange = FALSE;
8417
8418
8419 /* execute the function if no errors detected and executing */
8420 if (evaluate && error == ERROR_NONE)
8421 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008422 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8423 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 error = ERROR_UNKNOWN;
8425
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008426 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {
8428 /*
8429 * User defined function.
8430 */
8431 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008434 /* Trigger FuncUndefined event, may load the function. */
8435 if (fp == NULL
8436 && apply_autocmds(EVENT_FUNCUNDEFINED,
8437 fname, fname, TRUE, NULL)
8438 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008440 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 fp = find_func(fname);
8442 }
8443#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008444 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008445 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008446 {
8447 /* loaded a package, search for the function again */
8448 fp = find_func(fname);
8449 }
8450
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 if (fp != NULL)
8452 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008453 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008455 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008457 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008459 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008460 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 else
8462 {
8463 /*
8464 * Call the user function.
8465 * Save and restore search patterns, script variables and
8466 * redo buffer.
8467 */
8468 save_search_patterns();
8469 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008470 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008472 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008473 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8474 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8475 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008476 /* Function was unreferenced while being used, free it
8477 * now. */
8478 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 restoreRedobuff();
8480 restore_search_patterns();
8481 error = ERROR_NONE;
8482 }
8483 }
8484 }
8485 else
8486 {
8487 /*
8488 * Find the function name in the table, call its implementation.
8489 */
8490 i = find_internal_func(fname);
8491 if (i >= 0)
8492 {
8493 if (argcount < functions[i].f_min_argc)
8494 error = ERROR_TOOFEW;
8495 else if (argcount > functions[i].f_max_argc)
8496 error = ERROR_TOOMANY;
8497 else
8498 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008500 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 error = ERROR_NONE;
8502 }
8503 }
8504 }
8505 /*
8506 * The function call (or "FuncUndefined" autocommand sequence) might
8507 * have been aborted by an error, an interrupt, or an explicitly thrown
8508 * exception that has not been caught so far. This situation can be
8509 * tested for by calling aborting(). For an error in an internal
8510 * function or for the "E132" error in call_user_func(), however, the
8511 * throw point at which the "force_abort" flag (temporarily reset by
8512 * emsg()) is normally updated has not been reached yet. We need to
8513 * update that flag first to make aborting() reliable.
8514 */
8515 update_force_abort();
8516 }
8517 if (error == ERROR_NONE)
8518 ret = OK;
8519
8520 /*
8521 * Report an error unless the argument evaluation or function call has been
8522 * cancelled due to an aborting error, an interrupt, or an exception.
8523 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008524 if (!aborting())
8525 {
8526 switch (error)
8527 {
8528 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008529 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008530 break;
8531 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008532 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008533 break;
8534 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008535 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008536 name);
8537 break;
8538 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008539 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008540 name);
8541 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008542 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008543 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008544 name);
8545 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008546 }
8547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 if (fname != name && fname != fname_buf)
8550 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008551 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
8553 return ret;
8554}
8555
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008556/*
8557 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008558 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008559 */
8560 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008561emsg_funcname(ermsg, name)
8562 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 char_u *name;
8564{
8565 char_u *p;
8566
8567 if (*name == K_SPECIAL)
8568 p = concat_str((char_u *)"<SNR>", name + 3);
8569 else
8570 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008571 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008572 if (p != name)
8573 vim_free(p);
8574}
8575
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008576/*
8577 * Return TRUE for a non-zero Number and a non-empty String.
8578 */
8579 static int
8580non_zero_arg(argvars)
8581 typval_T *argvars;
8582{
8583 return ((argvars[0].v_type == VAR_NUMBER
8584 && argvars[0].vval.v_number != 0)
8585 || (argvars[0].v_type == VAR_STRING
8586 && argvars[0].vval.v_string != NULL
8587 && *argvars[0].vval.v_string != NUL));
8588}
8589
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590/*********************************************
8591 * Implementation of the built-in functions
8592 */
8593
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008594#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008595static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8596
8597/*
8598 * Get the float value of "argvars[0]" into "f".
8599 * Returns FAIL when the argument is not a Number or Float.
8600 */
8601 static int
8602get_float_arg(argvars, f)
8603 typval_T *argvars;
8604 float_T *f;
8605{
8606 if (argvars[0].v_type == VAR_FLOAT)
8607 {
8608 *f = argvars[0].vval.v_float;
8609 return OK;
8610 }
8611 if (argvars[0].v_type == VAR_NUMBER)
8612 {
8613 *f = (float_T)argvars[0].vval.v_number;
8614 return OK;
8615 }
8616 EMSG(_("E808: Number or Float required"));
8617 return FAIL;
8618}
8619
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008620/*
8621 * "abs(expr)" function
8622 */
8623 static void
8624f_abs(argvars, rettv)
8625 typval_T *argvars;
8626 typval_T *rettv;
8627{
8628 if (argvars[0].v_type == VAR_FLOAT)
8629 {
8630 rettv->v_type = VAR_FLOAT;
8631 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8632 }
8633 else
8634 {
8635 varnumber_T n;
8636 int error = FALSE;
8637
8638 n = get_tv_number_chk(&argvars[0], &error);
8639 if (error)
8640 rettv->vval.v_number = -1;
8641 else if (n > 0)
8642 rettv->vval.v_number = n;
8643 else
8644 rettv->vval.v_number = -n;
8645 }
8646}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008647
8648/*
8649 * "acos()" function
8650 */
8651 static void
8652f_acos(argvars, rettv)
8653 typval_T *argvars;
8654 typval_T *rettv;
8655{
8656 float_T f;
8657
8658 rettv->v_type = VAR_FLOAT;
8659 if (get_float_arg(argvars, &f) == OK)
8660 rettv->vval.v_float = acos(f);
8661 else
8662 rettv->vval.v_float = 0.0;
8663}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008664#endif
8665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008667 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 */
8669 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008670f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008671 typval_T *argvars;
8672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
Bram Moolenaar33570922005-01-25 22:26:29 +00008674 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008676 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008677 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008679 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008680 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008681 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008683 }
8684 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008685 EMSG(_(e_listreq));
8686}
8687
8688/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008689 * "and(expr, expr)" function
8690 */
8691 static void
8692f_and(argvars, rettv)
8693 typval_T *argvars;
8694 typval_T *rettv;
8695{
8696 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8697 & get_tv_number_chk(&argvars[1], NULL);
8698}
8699
8700/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008701 * "append(lnum, string/list)" function
8702 */
8703 static void
8704f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 typval_T *argvars;
8706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008707{
8708 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008709 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008710 list_T *l = NULL;
8711 listitem_T *li = NULL;
8712 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008713 long added = 0;
8714
Bram Moolenaar0d660222005-01-07 21:51:51 +00008715 lnum = get_tv_lnum(argvars);
8716 if (lnum >= 0
8717 && lnum <= curbuf->b_ml.ml_line_count
8718 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008719 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008721 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722 l = argvars[1].vval.v_list;
8723 if (l == NULL)
8724 return;
8725 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008726 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008727 for (;;)
8728 {
8729 if (l == NULL)
8730 tv = &argvars[1]; /* append a string */
8731 else if (li == NULL)
8732 break; /* end of list */
8733 else
8734 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008735 line = get_tv_string_chk(tv);
8736 if (line == NULL) /* type error */
8737 {
8738 rettv->vval.v_number = 1; /* Failed */
8739 break;
8740 }
8741 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008742 ++added;
8743 if (l == NULL)
8744 break;
8745 li = li->li_next;
8746 }
8747
8748 appended_lines_mark(lnum, added);
8749 if (curwin->w_cursor.lnum > lnum)
8750 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008752 else
8753 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754}
8755
8756/*
8757 * "argc()" function
8758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008761 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008764 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765}
8766
8767/*
8768 * "argidx()" function
8769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008772 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008775 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776}
8777
8778/*
8779 * "argv(nr)" function
8780 */
8781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 typval_T *argvars;
8784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
8786 int idx;
8787
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008788 if (argvars[0].v_type != VAR_UNKNOWN)
8789 {
8790 idx = get_tv_number_chk(&argvars[0], NULL);
8791 if (idx >= 0 && idx < ARGCOUNT)
8792 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8793 else
8794 rettv->vval.v_string = NULL;
8795 rettv->v_type = VAR_STRING;
8796 }
8797 else if (rettv_list_alloc(rettv) == OK)
8798 for (idx = 0; idx < ARGCOUNT; ++idx)
8799 list_append_string(rettv->vval.v_list,
8800 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801}
8802
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008803#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008804/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008805 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008806 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008807 static void
8808f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008809 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008810 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008811{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008812 float_T f;
8813
8814 rettv->v_type = VAR_FLOAT;
8815 if (get_float_arg(argvars, &f) == OK)
8816 rettv->vval.v_float = asin(f);
8817 else
8818 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008819}
8820
8821/*
8822 * "atan()" function
8823 */
8824 static void
8825f_atan(argvars, rettv)
8826 typval_T *argvars;
8827 typval_T *rettv;
8828{
8829 float_T f;
8830
8831 rettv->v_type = VAR_FLOAT;
8832 if (get_float_arg(argvars, &f) == OK)
8833 rettv->vval.v_float = atan(f);
8834 else
8835 rettv->vval.v_float = 0.0;
8836}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008837
8838/*
8839 * "atan2()" function
8840 */
8841 static void
8842f_atan2(argvars, rettv)
8843 typval_T *argvars;
8844 typval_T *rettv;
8845{
8846 float_T fx, fy;
8847
8848 rettv->v_type = VAR_FLOAT;
8849 if (get_float_arg(argvars, &fx) == OK
8850 && get_float_arg(&argvars[1], &fy) == OK)
8851 rettv->vval.v_float = atan2(fx, fy);
8852 else
8853 rettv->vval.v_float = 0.0;
8854}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008855#endif
8856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857/*
8858 * "browse(save, title, initdir, default)" function
8859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008862 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865#ifdef FEAT_BROWSE
8866 int save;
8867 char_u *title;
8868 char_u *initdir;
8869 char_u *defname;
8870 char_u buf[NUMBUFLEN];
8871 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 save = get_tv_number_chk(&argvars[0], &error);
8875 title = get_tv_string_chk(&argvars[1]);
8876 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8877 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008879 if (error || title == NULL || initdir == NULL || defname == NULL)
8880 rettv->vval.v_string = NULL;
8881 else
8882 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008883 do_browse(save ? BROWSE_SAVE : 0,
8884 title, defname, NULL, initdir, NULL, curbuf);
8885#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008886 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008887#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008889}
8890
8891/*
8892 * "browsedir(title, initdir)" function
8893 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008897 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008898{
8899#ifdef FEAT_BROWSE
8900 char_u *title;
8901 char_u *initdir;
8902 char_u buf[NUMBUFLEN];
8903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 title = get_tv_string_chk(&argvars[0]);
8905 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907 if (title == NULL || initdir == NULL)
8908 rettv->vval.v_string = NULL;
8909 else
8910 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008911 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916}
8917
Bram Moolenaar33570922005-01-25 22:26:29 +00008918static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920/*
8921 * Find a buffer by number or exact name.
8922 */
8923 static buf_T *
8924find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008929 if (avar->v_type == VAR_NUMBER)
8930 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008931 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008933 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008934 if (buf == NULL)
8935 {
8936 /* No full path name match, try a match with a URL or a "nofile"
8937 * buffer, these don't use the full path. */
8938 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8939 if (buf->b_fname != NULL
8940 && (path_with_url(buf->b_fname)
8941#ifdef FEAT_QUICKFIX
8942 || bt_nofile(buf)
8943#endif
8944 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008945 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008946 break;
8947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 }
8949 return buf;
8950}
8951
8952/*
8953 * "bufexists(expr)" function
8954 */
8955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008957 typval_T *argvars;
8958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961}
8962
8963/*
8964 * "buflisted(expr)" function
8965 */
8966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 typval_T *argvars;
8969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970{
8971 buf_T *buf;
8972
8973 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975}
8976
8977/*
8978 * "bufloaded(expr)" function
8979 */
8980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008982 typval_T *argvars;
8983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
8985 buf_T *buf;
8986
8987 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989}
8990
Bram Moolenaar33570922005-01-25 22:26:29 +00008991static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008992
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993/*
8994 * Get buffer by number or pattern.
8995 */
8996 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 int save_magic;
9002 char_u *save_cpo;
9003 buf_T *buf;
9004
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 if (tv->v_type == VAR_NUMBER)
9006 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009007 if (tv->v_type != VAR_STRING)
9008 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 if (name == NULL || *name == NUL)
9010 return curbuf;
9011 if (name[0] == '$' && name[1] == NUL)
9012 return lastbuf;
9013
9014 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9015 save_magic = p_magic;
9016 p_magic = TRUE;
9017 save_cpo = p_cpo;
9018 p_cpo = (char_u *)"";
9019
9020 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
9021 TRUE, FALSE));
9022
9023 p_magic = save_magic;
9024 p_cpo = save_cpo;
9025
9026 /* If not found, try expanding the name, like done for bufexists(). */
9027 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029
9030 return buf;
9031}
9032
9033/*
9034 * "bufname(expr)" function
9035 */
9036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *argvars;
9039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040{
9041 buf_T *buf;
9042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 buf = get_buf_tv(&argvars[0]);
9046 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 --emsg_off;
9052}
9053
9054/*
9055 * "bufnr(expr)" function
9056 */
9057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009063 int error = FALSE;
9064 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009066 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009069 --emsg_off;
9070
9071 /* If the buffer isn't found and the second argument is not zero create a
9072 * new buffer. */
9073 if (buf == NULL
9074 && argvars[1].v_type != VAR_UNKNOWN
9075 && get_tv_number_chk(&argvars[1], &error) != 0
9076 && !error
9077 && (name = get_tv_string_chk(&argvars[0])) != NULL
9078 && !error)
9079 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9080
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
9087/*
9088 * "bufwinnr(nr)" function
9089 */
9090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009092 typval_T *argvars;
9093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094{
9095#ifdef FEAT_WINDOWS
9096 win_T *wp;
9097 int winnr = 0;
9098#endif
9099 buf_T *buf;
9100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009101 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104#ifdef FEAT_WINDOWS
9105 for (wp = firstwin; wp; wp = wp->w_next)
9106 {
9107 ++winnr;
9108 if (wp->w_buffer == buf)
9109 break;
9110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114#endif
9115 --emsg_off;
9116}
9117
9118/*
9119 * "byte2line(byte)" function
9120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009123 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128#else
9129 long boff = 0;
9130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 (linenr_T)0, &boff);
9137#endif
9138}
9139
9140/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009141 * "byteidx()" function
9142 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009145 typval_T *argvars;
9146 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009147{
9148#ifdef FEAT_MBYTE
9149 char_u *t;
9150#endif
9151 char_u *str;
9152 long idx;
9153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 str = get_tv_string_chk(&argvars[0]);
9155 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009158 return;
9159
9160#ifdef FEAT_MBYTE
9161 t = str;
9162 for ( ; idx > 0; idx--)
9163 {
9164 if (*t == NUL) /* EOL reached */
9165 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009166 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009167 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009168 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009169#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009170 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009172#endif
9173}
9174
Bram Moolenaardb913952012-06-29 12:54:53 +02009175 int
9176func_call(name, args, selfdict, rettv)
9177 char_u *name;
9178 typval_T *args;
9179 dict_T *selfdict;
9180 typval_T *rettv;
9181{
9182 listitem_T *item;
9183 typval_T argv[MAX_FUNC_ARGS + 1];
9184 int argc = 0;
9185 int dummy;
9186 int r = 0;
9187
9188 for (item = args->vval.v_list->lv_first; item != NULL;
9189 item = item->li_next)
9190 {
9191 if (argc == MAX_FUNC_ARGS)
9192 {
9193 EMSG(_("E699: Too many arguments"));
9194 break;
9195 }
9196 /* Make a copy of each argument. This is needed to be able to set
9197 * v_lock to VAR_FIXED in the copy without changing the original list.
9198 */
9199 copy_tv(&item->li_tv, &argv[argc++]);
9200 }
9201
9202 if (item == NULL)
9203 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9204 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9205 &dummy, TRUE, selfdict);
9206
9207 /* Free the arguments. */
9208 while (argc > 0)
9209 clear_tv(&argv[--argc]);
9210
9211 return r;
9212}
9213
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009214/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009215 * "call(func, arglist)" function
9216 */
9217 static void
9218f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009219 typval_T *argvars;
9220 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009221{
9222 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009223 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009224
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009225 if (argvars[1].v_type != VAR_LIST)
9226 {
9227 EMSG(_(e_listreq));
9228 return;
9229 }
9230 if (argvars[1].vval.v_list == NULL)
9231 return;
9232
9233 if (argvars[0].v_type == VAR_FUNC)
9234 func = argvars[0].vval.v_string;
9235 else
9236 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 if (*func == NUL)
9238 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009239
Bram Moolenaare9a41262005-01-15 22:18:47 +00009240 if (argvars[2].v_type != VAR_UNKNOWN)
9241 {
9242 if (argvars[2].v_type != VAR_DICT)
9243 {
9244 EMSG(_(e_dictreq));
9245 return;
9246 }
9247 selfdict = argvars[2].vval.v_dict;
9248 }
9249
Bram Moolenaardb913952012-06-29 12:54:53 +02009250 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009251}
9252
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009253#ifdef FEAT_FLOAT
9254/*
9255 * "ceil({float})" function
9256 */
9257 static void
9258f_ceil(argvars, rettv)
9259 typval_T *argvars;
9260 typval_T *rettv;
9261{
9262 float_T f;
9263
9264 rettv->v_type = VAR_FLOAT;
9265 if (get_float_arg(argvars, &f) == OK)
9266 rettv->vval.v_float = ceil(f);
9267 else
9268 rettv->vval.v_float = 0.0;
9269}
9270#endif
9271
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009272/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009273 * "changenr()" function
9274 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009275 static void
9276f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009277 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009278 typval_T *rettv;
9279{
9280 rettv->vval.v_number = curbuf->b_u_seq_cur;
9281}
9282
9283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 * "char2nr(string)" function
9285 */
9286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009288 typval_T *argvars;
9289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290{
9291#ifdef FEAT_MBYTE
9292 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 else
9295#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297}
9298
9299/*
9300 * "cindent(lnum)" function
9301 */
9302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 typval_T *argvars;
9305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307#ifdef FEAT_CINDENT
9308 pos_T pos;
9309 linenr_T lnum;
9310
9311 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9314 {
9315 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009316 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 curwin->w_cursor = pos;
9318 }
9319 else
9320#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322}
9323
9324/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009325 * "clearmatches()" function
9326 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009327 static void
9328f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009329 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009330 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009331{
9332#ifdef FEAT_SEARCH_EXTRA
9333 clear_matches(curwin);
9334#endif
9335}
9336
9337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 * "col(string)" function
9339 */
9340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009341f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009342 typval_T *argvars;
9343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344{
9345 colnr_T col = 0;
9346 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009347 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009349 fp = var2fpos(&argvars[0], FALSE, &fnum);
9350 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 {
9352 if (fp->col == MAXCOL)
9353 {
9354 /* '> can be MAXCOL, get the length of the line then */
9355 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009356 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 else
9358 col = MAXCOL;
9359 }
9360 else
9361 {
9362 col = fp->col + 1;
9363#ifdef FEAT_VIRTUALEDIT
9364 /* col(".") when the cursor is on the NUL at the end of the line
9365 * because of "coladd" can be seen as an extra column. */
9366 if (virtual_active() && fp == &curwin->w_cursor)
9367 {
9368 char_u *p = ml_get_cursor();
9369
9370 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9371 curwin->w_virtcol - curwin->w_cursor.coladd))
9372 {
9373# ifdef FEAT_MBYTE
9374 int l;
9375
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009376 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 col += l;
9378# else
9379 if (*p != NUL && p[1] == NUL)
9380 ++col;
9381# endif
9382 }
9383 }
9384#endif
9385 }
9386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388}
9389
Bram Moolenaar572cb562005-08-05 21:35:02 +00009390#if defined(FEAT_INS_EXPAND)
9391/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009392 * "complete()" function
9393 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009394 static void
9395f_complete(argvars, rettv)
9396 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009397 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009398{
9399 int startcol;
9400
9401 if ((State & INSERT) == 0)
9402 {
9403 EMSG(_("E785: complete() can only be used in Insert mode"));
9404 return;
9405 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009406
9407 /* Check for undo allowed here, because if something was already inserted
9408 * the line was already saved for undo and this check isn't done. */
9409 if (!undo_allowed())
9410 return;
9411
Bram Moolenaarade00832006-03-10 21:46:58 +00009412 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9413 {
9414 EMSG(_(e_invarg));
9415 return;
9416 }
9417
9418 startcol = get_tv_number_chk(&argvars[0], NULL);
9419 if (startcol <= 0)
9420 return;
9421
9422 set_completion(startcol - 1, argvars[1].vval.v_list);
9423}
9424
9425/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009426 * "complete_add()" function
9427 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009428 static void
9429f_complete_add(argvars, rettv)
9430 typval_T *argvars;
9431 typval_T *rettv;
9432{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009433 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009434}
9435
9436/*
9437 * "complete_check()" function
9438 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009439 static void
9440f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009441 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009442 typval_T *rettv;
9443{
9444 int saved = RedrawingDisabled;
9445
9446 RedrawingDisabled = 0;
9447 ins_compl_check_keys(0);
9448 rettv->vval.v_number = compl_interrupted;
9449 RedrawingDisabled = saved;
9450}
9451#endif
9452
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453/*
9454 * "confirm(message, buttons[, default [, type]])" function
9455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009458 typval_T *argvars UNUSED;
9459 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
9461#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9462 char_u *message;
9463 char_u *buttons = NULL;
9464 char_u buf[NUMBUFLEN];
9465 char_u buf2[NUMBUFLEN];
9466 int def = 1;
9467 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009468 char_u *typestr;
9469 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471 message = get_tv_string_chk(&argvars[0]);
9472 if (message == NULL)
9473 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009474 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9477 if (buttons == NULL)
9478 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009479 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009481 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009482 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009484 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9485 if (typestr == NULL)
9486 error = TRUE;
9487 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009489 switch (TOUPPER_ASC(*typestr))
9490 {
9491 case 'E': type = VIM_ERROR; break;
9492 case 'Q': type = VIM_QUESTION; break;
9493 case 'I': type = VIM_INFO; break;
9494 case 'W': type = VIM_WARNING; break;
9495 case 'G': type = VIM_GENERIC; break;
9496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 }
9498 }
9499 }
9500 }
9501
9502 if (buttons == NULL || *buttons == NUL)
9503 buttons = (char_u *)_("&Ok");
9504
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009505 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009507 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508#endif
9509}
9510
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009511/*
9512 * "copy()" function
9513 */
9514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 typval_T *argvars;
9517 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009518{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009519 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009522#ifdef FEAT_FLOAT
9523/*
9524 * "cos()" function
9525 */
9526 static void
9527f_cos(argvars, rettv)
9528 typval_T *argvars;
9529 typval_T *rettv;
9530{
9531 float_T f;
9532
9533 rettv->v_type = VAR_FLOAT;
9534 if (get_float_arg(argvars, &f) == OK)
9535 rettv->vval.v_float = cos(f);
9536 else
9537 rettv->vval.v_float = 0.0;
9538}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009539
9540/*
9541 * "cosh()" function
9542 */
9543 static void
9544f_cosh(argvars, rettv)
9545 typval_T *argvars;
9546 typval_T *rettv;
9547{
9548 float_T f;
9549
9550 rettv->v_type = VAR_FLOAT;
9551 if (get_float_arg(argvars, &f) == OK)
9552 rettv->vval.v_float = cosh(f);
9553 else
9554 rettv->vval.v_float = 0.0;
9555}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009556#endif
9557
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009559 * "count()" function
9560 */
9561 static void
9562f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009563 typval_T *argvars;
9564 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009565{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009566 long n = 0;
9567 int ic = FALSE;
9568
Bram Moolenaare9a41262005-01-15 22:18:47 +00009569 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009570 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009571 listitem_T *li;
9572 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009573 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009574
Bram Moolenaare9a41262005-01-15 22:18:47 +00009575 if ((l = argvars[0].vval.v_list) != NULL)
9576 {
9577 li = l->lv_first;
9578 if (argvars[2].v_type != VAR_UNKNOWN)
9579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009580 int error = FALSE;
9581
9582 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009583 if (argvars[3].v_type != VAR_UNKNOWN)
9584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 idx = get_tv_number_chk(&argvars[3], &error);
9586 if (!error)
9587 {
9588 li = list_find(l, idx);
9589 if (li == NULL)
9590 EMSGN(_(e_listidx), idx);
9591 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 if (error)
9594 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009595 }
9596
9597 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009598 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009599 ++n;
9600 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009601 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009602 else if (argvars[0].v_type == VAR_DICT)
9603 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009604 int todo;
9605 dict_T *d;
9606 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009608 if ((d = argvars[0].vval.v_dict) != NULL)
9609 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009610 int error = FALSE;
9611
Bram Moolenaare9a41262005-01-15 22:18:47 +00009612 if (argvars[2].v_type != VAR_UNKNOWN)
9613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 if (argvars[3].v_type != VAR_UNKNOWN)
9616 EMSG(_(e_invarg));
9617 }
9618
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009619 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009620 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009621 {
9622 if (!HASHITEM_EMPTY(hi))
9623 {
9624 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009625 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009626 ++n;
9627 }
9628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 }
9630 }
9631 else
9632 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009633 rettv->vval.v_number = n;
9634}
9635
9636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9638 *
9639 * Checks the existence of a cscope connection.
9640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009642f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009643 typval_T *argvars UNUSED;
9644 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646#ifdef FEAT_CSCOPE
9647 int num = 0;
9648 char_u *dbpath = NULL;
9649 char_u *prepend = NULL;
9650 char_u buf[NUMBUFLEN];
9651
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009652 if (argvars[0].v_type != VAR_UNKNOWN
9653 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 num = (int)get_tv_number(&argvars[0]);
9656 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009657 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 }
9660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662#endif
9663}
9664
9665/*
9666 * "cursor(lnum, col)" function
9667 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009668 * Moves the cursor to the specified line and column.
9669 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009673 typval_T *argvars;
9674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675{
9676 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009677#ifdef FEAT_VIRTUALEDIT
9678 long coladd = 0;
9679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009681 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009682 if (argvars[1].v_type == VAR_UNKNOWN)
9683 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009684 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009685
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009686 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009687 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009688 line = pos.lnum;
9689 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009690#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009691 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009692#endif
9693 }
9694 else
9695 {
9696 line = get_tv_lnum(argvars);
9697 col = get_tv_number_chk(&argvars[1], NULL);
9698#ifdef FEAT_VIRTUALEDIT
9699 if (argvars[2].v_type != VAR_UNKNOWN)
9700 coladd = get_tv_number_chk(&argvars[2], NULL);
9701#endif
9702 }
9703 if (line < 0 || col < 0
9704#ifdef FEAT_VIRTUALEDIT
9705 || coladd < 0
9706#endif
9707 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009708 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 if (line > 0)
9710 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 if (col > 0)
9712 curwin->w_cursor.col = col - 1;
9713#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009714 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715#endif
9716
9717 /* Make sure the cursor is in a valid position. */
9718 check_cursor();
9719#ifdef FEAT_MBYTE
9720 /* Correct cursor for multi-byte character. */
9721 if (has_mbyte)
9722 mb_adjust_cursor();
9723#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009724
9725 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009726 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727}
9728
9729/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009730 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 */
9732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 typval_T *argvars;
9735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009737 int noref = 0;
9738
9739 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009740 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009741 if (noref < 0 || noref > 1)
9742 EMSG(_(e_invarg));
9743 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009744 {
9745 current_copyID += COPYID_INC;
9746 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748}
9749
9750/*
9751 * "delete()" function
9752 */
9753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009755 typval_T *argvars;
9756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757{
9758 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762}
9763
9764/*
9765 * "did_filetype()" function
9766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009769 typval_T *argvars UNUSED;
9770 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
9772#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774#endif
9775}
9776
9777/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009778 * "diff_filler()" function
9779 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009782 typval_T *argvars UNUSED;
9783 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009784{
9785#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009787#endif
9788}
9789
9790/*
9791 * "diff_hlID()" function
9792 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009795 typval_T *argvars UNUSED;
9796 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009797{
9798#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009800 static linenr_T prev_lnum = 0;
9801 static int changedtick = 0;
9802 static int fnum = 0;
9803 static int change_start = 0;
9804 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009805 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009806 int filler_lines;
9807 int col;
9808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 if (lnum < 0) /* ignore type error in {lnum} arg */
9810 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009811 if (lnum != prev_lnum
9812 || changedtick != curbuf->b_changedtick
9813 || fnum != curbuf->b_fnum)
9814 {
9815 /* New line, buffer, change: need to get the values. */
9816 filler_lines = diff_check(curwin, lnum);
9817 if (filler_lines < 0)
9818 {
9819 if (filler_lines == -1)
9820 {
9821 change_start = MAXCOL;
9822 change_end = -1;
9823 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9824 hlID = HLF_ADD; /* added line */
9825 else
9826 hlID = HLF_CHD; /* changed line */
9827 }
9828 else
9829 hlID = HLF_ADD; /* added line */
9830 }
9831 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009832 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009833 prev_lnum = lnum;
9834 changedtick = curbuf->b_changedtick;
9835 fnum = curbuf->b_fnum;
9836 }
9837
9838 if (hlID == HLF_CHD || hlID == HLF_TXD)
9839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009841 if (col >= change_start && col <= change_end)
9842 hlID = HLF_TXD; /* changed text */
9843 else
9844 hlID = HLF_CHD; /* changed line */
9845 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009846 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009847#endif
9848}
9849
9850/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009851 * "empty({expr})" function
9852 */
9853 static void
9854f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009855 typval_T *argvars;
9856 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009857{
9858 int n;
9859
9860 switch (argvars[0].v_type)
9861 {
9862 case VAR_STRING:
9863 case VAR_FUNC:
9864 n = argvars[0].vval.v_string == NULL
9865 || *argvars[0].vval.v_string == NUL;
9866 break;
9867 case VAR_NUMBER:
9868 n = argvars[0].vval.v_number == 0;
9869 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009870#ifdef FEAT_FLOAT
9871 case VAR_FLOAT:
9872 n = argvars[0].vval.v_float == 0.0;
9873 break;
9874#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009875 case VAR_LIST:
9876 n = argvars[0].vval.v_list == NULL
9877 || argvars[0].vval.v_list->lv_first == NULL;
9878 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009879 case VAR_DICT:
9880 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009881 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009882 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009883 default:
9884 EMSG2(_(e_intern2), "f_empty()");
9885 n = 0;
9886 }
9887
9888 rettv->vval.v_number = n;
9889}
9890
9891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 * "escape({string}, {chars})" function
9893 */
9894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009895f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 typval_T *argvars;
9897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898{
9899 char_u buf[NUMBUFLEN];
9900
Bram Moolenaar758711c2005-02-02 23:11:38 +00009901 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9902 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904}
9905
9906/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009907 * "eval()" function
9908 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009909 static void
9910f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 typval_T *argvars;
9912 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009913{
9914 char_u *s;
9915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009916 s = get_tv_string_chk(&argvars[0]);
9917 if (s != NULL)
9918 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9921 {
9922 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009923 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009925 else if (*s != NUL)
9926 EMSG(_(e_trailing));
9927}
9928
9929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 * "eventhandler()" function
9931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938}
9939
9940/*
9941 * "executable()" function
9942 */
9943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 typval_T *argvars;
9946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949}
9950
9951/*
9952 * "exists()" function
9953 */
9954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009956 typval_T *argvars;
9957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958{
9959 char_u *p;
9960 char_u *name;
9961 int n = FALSE;
9962 int len = 0;
9963
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009964 no_autoload = TRUE;
9965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 if (*p == '$') /* environment variable */
9968 {
9969 /* first try "normal" environment variables (fast) */
9970 if (mch_getenv(p + 1) != NULL)
9971 n = TRUE;
9972 else
9973 {
9974 /* try expanding things like $VIM and ${HOME} */
9975 p = expand_env_save(p);
9976 if (p != NULL && *p != '$')
9977 n = TRUE;
9978 vim_free(p);
9979 }
9980 }
9981 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009984 if (*skipwhite(p) != NUL)
9985 n = FALSE; /* trailing garbage */
9986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 else if (*p == '*') /* internal or user defined function */
9988 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009989 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 }
9991 else if (*p == ':')
9992 {
9993 n = cmd_exists(p + 1);
9994 }
9995 else if (*p == '#')
9996 {
9997#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009998 if (p[1] == '#')
9999 n = autocmd_supported(p + 2);
10000 else
10001 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002#endif
10003 }
10004 else /* internal variable */
10005 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010006 char_u *tofree;
10007 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010009 /* get_name_len() takes care of expanding curly braces */
10010 name = p;
10011 len = get_name_len(&p, &tofree, TRUE, FALSE);
10012 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010014 if (tofree != NULL)
10015 name = tofree;
10016 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10017 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010019 /* handle d.key, l[idx], f(expr) */
10020 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10021 if (n)
10022 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 }
10024 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010025 if (*p != NUL)
10026 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010028 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 }
10030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010032
10033 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034}
10035
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010036#ifdef FEAT_FLOAT
10037/*
10038 * "exp()" function
10039 */
10040 static void
10041f_exp(argvars, rettv)
10042 typval_T *argvars;
10043 typval_T *rettv;
10044{
10045 float_T f;
10046
10047 rettv->v_type = VAR_FLOAT;
10048 if (get_float_arg(argvars, &f) == OK)
10049 rettv->vval.v_float = exp(f);
10050 else
10051 rettv->vval.v_float = 0.0;
10052}
10053#endif
10054
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055/*
10056 * "expand()" function
10057 */
10058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 typval_T *argvars;
10061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
10063 char_u *s;
10064 int len;
10065 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010066 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010069 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010072 if (argvars[1].v_type != VAR_UNKNOWN
10073 && argvars[2].v_type != VAR_UNKNOWN
10074 && get_tv_number_chk(&argvars[2], &error)
10075 && !error)
10076 {
10077 rettv->v_type = VAR_LIST;
10078 rettv->vval.v_list = NULL;
10079 }
10080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 if (*s == '%' || *s == '#' || *s == '<')
10083 {
10084 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010085 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010087 if (rettv->v_type == VAR_LIST)
10088 {
10089 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10090 list_append_string(rettv->vval.v_list, result, -1);
10091 else
10092 vim_free(result);
10093 }
10094 else
10095 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 }
10097 else
10098 {
10099 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010100 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 if (argvars[1].v_type != VAR_UNKNOWN
10102 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010103 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010104 if (!error)
10105 {
10106 ExpandInit(&xpc);
10107 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010108 if (p_wic)
10109 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010110 if (rettv->v_type == VAR_STRING)
10111 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10112 options, WILD_ALL);
10113 else if (rettv_list_alloc(rettv) != FAIL)
10114 {
10115 int i;
10116
10117 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10118 for (i = 0; i < xpc.xp_numfiles; i++)
10119 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10120 ExpandCleanup(&xpc);
10121 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 }
10123 else
10124 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 }
10126}
10127
10128/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010129 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010130 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010131 */
10132 static void
10133f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010134 typval_T *argvars;
10135 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010136{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010137 char *arg_errmsg = N_("extend() argument");
10138
Bram Moolenaare9a41262005-01-15 22:18:47 +000010139 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010140 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010141 list_T *l1, *l2;
10142 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010143 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010144 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010145
Bram Moolenaare9a41262005-01-15 22:18:47 +000010146 l1 = argvars[0].vval.v_list;
10147 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010148 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010149 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010150 {
10151 if (argvars[2].v_type != VAR_UNKNOWN)
10152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 before = get_tv_number_chk(&argvars[2], &error);
10154 if (error)
10155 return; /* type error; errmsg already given */
10156
Bram Moolenaar758711c2005-02-02 23:11:38 +000010157 if (before == l1->lv_len)
10158 item = NULL;
10159 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010160 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010161 item = list_find(l1, before);
10162 if (item == NULL)
10163 {
10164 EMSGN(_(e_listidx), before);
10165 return;
10166 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010167 }
10168 }
10169 else
10170 item = NULL;
10171 list_extend(l1, l2, item);
10172
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 copy_tv(&argvars[0], rettv);
10174 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010175 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010176 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010178 dict_T *d1, *d2;
10179 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 char_u *action;
10181 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010182 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010183 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010184
10185 d1 = argvars[0].vval.v_dict;
10186 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010187 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010188 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 {
10190 /* Check the third argument. */
10191 if (argvars[2].v_type != VAR_UNKNOWN)
10192 {
10193 static char *(av[]) = {"keep", "force", "error"};
10194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 action = get_tv_string_chk(&argvars[2]);
10196 if (action == NULL)
10197 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 for (i = 0; i < 3; ++i)
10199 if (STRCMP(action, av[i]) == 0)
10200 break;
10201 if (i == 3)
10202 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010203 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010204 return;
10205 }
10206 }
10207 else
10208 action = (char_u *)"force";
10209
10210 /* Go over all entries in the second dict and add them to the
10211 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010212 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010213 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010215 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010217 --todo;
10218 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010219 if (d1->dv_scope != 0)
10220 {
10221 /* Disallow replacing a builtin function in l: and g:.
10222 * Check the key to be valid when adding to any
10223 * scope. */
10224 if (d1->dv_scope == VAR_DEF_SCOPE
10225 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10226 && var_check_func_name(hi2->hi_key,
10227 di1 == NULL))
10228 break;
10229 if (!valid_varname(hi2->hi_key))
10230 break;
10231 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010232 if (di1 == NULL)
10233 {
10234 di1 = dictitem_copy(HI2DI(hi2));
10235 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10236 dictitem_free(di1);
10237 }
10238 else if (*action == 'e')
10239 {
10240 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10241 break;
10242 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010243 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010244 {
10245 clear_tv(&di1->di_tv);
10246 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 }
10249 }
10250
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251 copy_tv(&argvars[0], rettv);
10252 }
10253 }
10254 else
10255 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010256}
10257
10258/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010259 * "feedkeys()" function
10260 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010261 static void
10262f_feedkeys(argvars, rettv)
10263 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010264 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010265{
10266 int remap = TRUE;
10267 char_u *keys, *flags;
10268 char_u nbuf[NUMBUFLEN];
10269 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010270 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010271
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010272 /* This is not allowed in the sandbox. If the commands would still be
10273 * executed in the sandbox it would be OK, but it probably happens later,
10274 * when "sandbox" is no longer set. */
10275 if (check_secure())
10276 return;
10277
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010278 keys = get_tv_string(&argvars[0]);
10279 if (*keys != NUL)
10280 {
10281 if (argvars[1].v_type != VAR_UNKNOWN)
10282 {
10283 flags = get_tv_string_buf(&argvars[1], nbuf);
10284 for ( ; *flags != NUL; ++flags)
10285 {
10286 switch (*flags)
10287 {
10288 case 'n': remap = FALSE; break;
10289 case 'm': remap = TRUE; break;
10290 case 't': typed = TRUE; break;
10291 }
10292 }
10293 }
10294
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010295 /* Need to escape K_SPECIAL and CSI before putting the string in the
10296 * typeahead buffer. */
10297 keys_esc = vim_strsave_escape_csi(keys);
10298 if (keys_esc != NULL)
10299 {
10300 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010301 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010302 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010303 if (vgetc_busy)
10304 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010305 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010306 }
10307}
10308
10309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 * "filereadable()" function
10311 */
10312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010314 typval_T *argvars;
10315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010317 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 char_u *p;
10319 int n;
10320
Bram Moolenaarc236c162008-07-13 17:41:49 +000010321#ifndef O_NONBLOCK
10322# define O_NONBLOCK 0
10323#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010325 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10326 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 {
10328 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010329 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 }
10331 else
10332 n = FALSE;
10333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010334 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335}
10336
10337/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010338 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 * rights to write into.
10340 */
10341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010342f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010343 typval_T *argvars;
10344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010346 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347}
10348
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010349static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010350
10351 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010352findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010353 typval_T *argvars;
10354 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010355 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010356{
10357#ifdef FEAT_SEARCHPATH
10358 char_u *fname;
10359 char_u *fresult = NULL;
10360 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10361 char_u *p;
10362 char_u pathbuf[NUMBUFLEN];
10363 int count = 1;
10364 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010365 int error = FALSE;
10366#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010367
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010368 rettv->vval.v_string = NULL;
10369 rettv->v_type = VAR_STRING;
10370
10371#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010372 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010373
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010374 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10377 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010378 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010379 else
10380 {
10381 if (*p != NUL)
10382 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010384 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010385 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010387 }
10388
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010389 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10390 error = TRUE;
10391
10392 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010394 do
10395 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010396 if (rettv->v_type == VAR_STRING)
10397 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010398 fresult = find_file_in_path_option(first ? fname : NULL,
10399 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010400 0, first, path,
10401 find_what,
10402 curbuf->b_ffname,
10403 find_what == FINDFILE_DIR
10404 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010406
10407 if (fresult != NULL && rettv->v_type == VAR_LIST)
10408 list_append_string(rettv->vval.v_list, fresult, -1);
10409
10410 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010412
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010413 if (rettv->v_type == VAR_STRING)
10414 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010415#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010416}
10417
Bram Moolenaar33570922005-01-25 22:26:29 +000010418static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10419static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010420
10421/*
10422 * Implementation of map() and filter().
10423 */
10424 static void
10425filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 typval_T *argvars;
10427 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010428 int map;
10429{
10430 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010431 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010432 listitem_T *li, *nli;
10433 list_T *l = NULL;
10434 dictitem_T *di;
10435 hashtab_T *ht;
10436 hashitem_T *hi;
10437 dict_T *d = NULL;
10438 typval_T save_val;
10439 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010440 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010441 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010442 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10443 char *arg_errmsg = (map ? N_("map() argument")
10444 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010445 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010446 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010447
Bram Moolenaare9a41262005-01-15 22:18:47 +000010448 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010449 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010450 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010451 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010452 return;
10453 }
10454 else if (argvars[0].v_type == VAR_DICT)
10455 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010456 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010457 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010458 return;
10459 }
10460 else
10461 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010462 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010463 return;
10464 }
10465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 expr = get_tv_string_buf_chk(&argvars[1], buf);
10467 /* On type errors, the preceding call has already displayed an error
10468 * message. Avoid a misleading error message for an empty string that
10469 * was not passed as argument. */
10470 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 prepare_vimvar(VV_VAL, &save_val);
10473 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010474
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010475 /* We reset "did_emsg" to be able to detect whether an error
10476 * occurred during evaluation of the expression. */
10477 save_did_emsg = did_emsg;
10478 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010479
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010480 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010483 vimvars[VV_KEY].vv_type = VAR_STRING;
10484
10485 ht = &d->dv_hashtab;
10486 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010487 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 if (!HASHITEM_EMPTY(hi))
10491 {
10492 --todo;
10493 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010494 if (tv_check_lock(di->di_tv.v_lock,
10495 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010496 break;
10497 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010498 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010499 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010500 break;
10501 if (!map && rem)
10502 dictitem_remove(d, di);
10503 clear_tv(&vimvars[VV_KEY].vv_tv);
10504 }
10505 }
10506 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 }
10508 else
10509 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010510 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 for (li = l->lv_first; li != NULL; li = nli)
10513 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010514 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010515 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010517 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010518 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010519 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010520 break;
10521 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010523 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010524 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010526
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010527 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010528 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010529
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010530 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010531 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010532
10533 copy_tv(&argvars[0], rettv);
10534}
10535
10536 static int
10537filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010538 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010539 char_u *expr;
10540 int map;
10541 int *remp;
10542{
Bram Moolenaar33570922005-01-25 22:26:29 +000010543 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010545 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010546
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010548 s = expr;
10549 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010550 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010551 if (*s != NUL) /* check for trailing chars after expr */
10552 {
10553 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010554 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010555 }
10556 if (map)
10557 {
10558 /* map(): replace the list item value */
10559 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010560 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561 *tv = rettv;
10562 }
10563 else
10564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010565 int error = FALSE;
10566
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010569 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 /* On type error, nothing has been removed; return FAIL to stop the
10571 * loop. The error message was given by get_tv_number_chk(). */
10572 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010573 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010575 retval = OK;
10576theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010578 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579}
10580
10581/*
10582 * "filter()" function
10583 */
10584 static void
10585f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010586 typval_T *argvars;
10587 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588{
10589 filter_map(argvars, rettv, FALSE);
10590}
10591
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010592/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010593 * "finddir({fname}[, {path}[, {count}]])" function
10594 */
10595 static void
10596f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010597 typval_T *argvars;
10598 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010599{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010600 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010601}
10602
10603/*
10604 * "findfile({fname}[, {path}[, {count}]])" function
10605 */
10606 static void
10607f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010608 typval_T *argvars;
10609 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010610{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010611 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010612}
10613
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010614#ifdef FEAT_FLOAT
10615/*
10616 * "float2nr({float})" function
10617 */
10618 static void
10619f_float2nr(argvars, rettv)
10620 typval_T *argvars;
10621 typval_T *rettv;
10622{
10623 float_T f;
10624
10625 if (get_float_arg(argvars, &f) == OK)
10626 {
10627 if (f < -0x7fffffff)
10628 rettv->vval.v_number = -0x7fffffff;
10629 else if (f > 0x7fffffff)
10630 rettv->vval.v_number = 0x7fffffff;
10631 else
10632 rettv->vval.v_number = (varnumber_T)f;
10633 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010634}
10635
10636/*
10637 * "floor({float})" function
10638 */
10639 static void
10640f_floor(argvars, rettv)
10641 typval_T *argvars;
10642 typval_T *rettv;
10643{
10644 float_T f;
10645
10646 rettv->v_type = VAR_FLOAT;
10647 if (get_float_arg(argvars, &f) == OK)
10648 rettv->vval.v_float = floor(f);
10649 else
10650 rettv->vval.v_float = 0.0;
10651}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010652
10653/*
10654 * "fmod()" function
10655 */
10656 static void
10657f_fmod(argvars, rettv)
10658 typval_T *argvars;
10659 typval_T *rettv;
10660{
10661 float_T fx, fy;
10662
10663 rettv->v_type = VAR_FLOAT;
10664 if (get_float_arg(argvars, &fx) == OK
10665 && get_float_arg(&argvars[1], &fy) == OK)
10666 rettv->vval.v_float = fmod(fx, fy);
10667 else
10668 rettv->vval.v_float = 0.0;
10669}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010670#endif
10671
Bram Moolenaar0d660222005-01-07 21:51:51 +000010672/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010673 * "fnameescape({string})" function
10674 */
10675 static void
10676f_fnameescape(argvars, rettv)
10677 typval_T *argvars;
10678 typval_T *rettv;
10679{
10680 rettv->vval.v_string = vim_strsave_fnameescape(
10681 get_tv_string(&argvars[0]), FALSE);
10682 rettv->v_type = VAR_STRING;
10683}
10684
10685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 * "fnamemodify({fname}, {mods})" function
10687 */
10688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010690 typval_T *argvars;
10691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692{
10693 char_u *fname;
10694 char_u *mods;
10695 int usedlen = 0;
10696 int len;
10697 char_u *fbuf = NULL;
10698 char_u buf[NUMBUFLEN];
10699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 fname = get_tv_string_chk(&argvars[0]);
10701 mods = get_tv_string_buf_chk(&argvars[1], buf);
10702 if (fname == NULL || mods == NULL)
10703 fname = NULL;
10704 else
10705 {
10706 len = (int)STRLEN(fname);
10707 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 vim_free(fbuf);
10716}
10717
Bram Moolenaar33570922005-01-25 22:26:29 +000010718static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719
10720/*
10721 * "foldclosed()" function
10722 */
10723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 typval_T *argvars;
10726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 int end;
10728{
10729#ifdef FEAT_FOLDING
10730 linenr_T lnum;
10731 linenr_T first, last;
10732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10735 {
10736 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10737 {
10738 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 return;
10743 }
10744 }
10745#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747}
10748
10749/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010750 * "foldclosed()" function
10751 */
10752 static void
10753f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010754 typval_T *argvars;
10755 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010756{
10757 foldclosed_both(argvars, rettv, FALSE);
10758}
10759
10760/*
10761 * "foldclosedend()" function
10762 */
10763 static void
10764f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010765 typval_T *argvars;
10766 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010767{
10768 foldclosed_both(argvars, rettv, TRUE);
10769}
10770
10771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 * "foldlevel()" function
10773 */
10774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010776 typval_T *argvars;
10777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778{
10779#ifdef FEAT_FOLDING
10780 linenr_T lnum;
10781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786}
10787
10788/*
10789 * "foldtext()" function
10790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010792f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010793 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795{
10796#ifdef FEAT_FOLDING
10797 linenr_T lnum;
10798 char_u *s;
10799 char_u *r;
10800 int len;
10801 char *txt;
10802#endif
10803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 rettv->v_type = VAR_STRING;
10805 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010807 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10808 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10809 <= curbuf->b_ml.ml_line_count
10810 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 {
10812 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010813 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10814 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 {
10816 if (!linewhite(lnum))
10817 break;
10818 ++lnum;
10819 }
10820
10821 /* Find interesting text in this line. */
10822 s = skipwhite(ml_get(lnum));
10823 /* skip C comment-start */
10824 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010827 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010828 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010829 {
10830 s = skipwhite(ml_get(lnum + 1));
10831 if (*s == '*')
10832 s = skipwhite(s + 1);
10833 }
10834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 txt = _("+-%s%3ld lines: ");
10836 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 + 20 /* for %3ld */
10839 + STRLEN(s))); /* concatenated */
10840 if (r != NULL)
10841 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10843 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10844 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 len = (int)STRLEN(r);
10846 STRCAT(r, s);
10847 /* remove 'foldmarker' and 'commentstring' */
10848 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010849 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 }
10851 }
10852#endif
10853}
10854
10855/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010856 * "foldtextresult(lnum)" function
10857 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010860 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010861 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010862{
10863#ifdef FEAT_FOLDING
10864 linenr_T lnum;
10865 char_u *text;
10866 char_u buf[51];
10867 foldinfo_T foldinfo;
10868 int fold_count;
10869#endif
10870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 rettv->v_type = VAR_STRING;
10872 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010873#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 /* treat illegal types and illegal string values for {lnum} the same */
10876 if (lnum < 0)
10877 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010878 fold_count = foldedCount(curwin, lnum, &foldinfo);
10879 if (fold_count > 0)
10880 {
10881 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10882 &foldinfo, buf);
10883 if (text == buf)
10884 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010886 }
10887#endif
10888}
10889
10890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 * "foreground()" function
10892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010895 typval_T *argvars UNUSED;
10896 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898#ifdef FEAT_GUI
10899 if (gui.in_use)
10900 gui_mch_set_foreground();
10901#else
10902# ifdef WIN32
10903 win32_set_foreground();
10904# endif
10905#endif
10906}
10907
10908/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010909 * "function()" function
10910 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010913 typval_T *argvars;
10914 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010915{
10916 char_u *s;
10917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010919 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010920 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010921 /* Don't check an autoload name for existence here. */
10922 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010923 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010924 else
10925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->vval.v_string = vim_strsave(s);
10927 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010928 }
10929}
10930
10931/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010932 * "garbagecollect()" function
10933 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010934 static void
10935f_garbagecollect(argvars, rettv)
10936 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010937 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010938{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010939 /* This is postponed until we are back at the toplevel, because we may be
10940 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10941 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010942
10943 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10944 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010945}
10946
10947/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010948 * "get()" function
10949 */
10950 static void
10951f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010952 typval_T *argvars;
10953 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010954{
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 listitem_T *li;
10956 list_T *l;
10957 dictitem_T *di;
10958 dict_T *d;
10959 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010960
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010962 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 int error = FALSE;
10966
10967 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10968 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010971 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010972 else if (argvars[0].v_type == VAR_DICT)
10973 {
10974 if ((d = argvars[0].vval.v_dict) != NULL)
10975 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010976 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010977 if (di != NULL)
10978 tv = &di->di_tv;
10979 }
10980 }
10981 else
10982 EMSG2(_(e_listdictarg), "get()");
10983
10984 if (tv == NULL)
10985 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010987 copy_tv(&argvars[2], rettv);
10988 }
10989 else
10990 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010991}
10992
Bram Moolenaar342337a2005-07-21 21:11:17 +000010993static 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 +000010994
10995/*
10996 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010997 * Return a range (from start to end) of lines in rettv from the specified
10998 * buffer.
10999 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011000 */
11001 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011002get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011003 buf_T *buf;
11004 linenr_T start;
11005 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011006 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011007 typval_T *rettv;
11008{
11009 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011010
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011011 if (retlist && rettv_list_alloc(rettv) == FAIL)
11012 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011013
11014 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11015 return;
11016
11017 if (!retlist)
11018 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011019 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11020 p = ml_get_buf(buf, start, FALSE);
11021 else
11022 p = (char_u *)"";
11023
11024 rettv->v_type = VAR_STRING;
11025 rettv->vval.v_string = vim_strsave(p);
11026 }
11027 else
11028 {
11029 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011030 return;
11031
11032 if (start < 1)
11033 start = 1;
11034 if (end > buf->b_ml.ml_line_count)
11035 end = buf->b_ml.ml_line_count;
11036 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011037 if (list_append_string(rettv->vval.v_list,
11038 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011039 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011040 }
11041}
11042
11043/*
11044 * "getbufline()" function
11045 */
11046 static void
11047f_getbufline(argvars, rettv)
11048 typval_T *argvars;
11049 typval_T *rettv;
11050{
11051 linenr_T lnum;
11052 linenr_T end;
11053 buf_T *buf;
11054
11055 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11056 ++emsg_off;
11057 buf = get_buf_tv(&argvars[0]);
11058 --emsg_off;
11059
Bram Moolenaar661b1822005-07-28 22:36:45 +000011060 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011061 if (argvars[2].v_type == VAR_UNKNOWN)
11062 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011063 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011064 end = get_tv_lnum_buf(&argvars[2], buf);
11065
Bram Moolenaar342337a2005-07-21 21:11:17 +000011066 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011067}
11068
Bram Moolenaar0d660222005-01-07 21:51:51 +000011069/*
11070 * "getbufvar()" function
11071 */
11072 static void
11073f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011074 typval_T *argvars;
11075 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011076{
11077 buf_T *buf;
11078 buf_T *save_curbuf;
11079 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011080 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011082 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11083 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011084 ++emsg_off;
11085 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011086
11087 rettv->v_type = VAR_STRING;
11088 rettv->vval.v_string = NULL;
11089
11090 if (buf != NULL && varname != NULL)
11091 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011092 /* set curbuf to be our buf, temporarily */
11093 save_curbuf = curbuf;
11094 curbuf = buf;
11095
Bram Moolenaar0d660222005-01-07 21:51:51 +000011096 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000011097 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar445edda2011-01-22 01:13:39 +010011098 else if (STRCMP(varname, "changedtick") == 0)
11099 {
11100 rettv->v_type = VAR_NUMBER;
11101 rettv->vval.v_number = curbuf->b_changedtick;
11102 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011103 else
11104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 if (*varname == NUL)
11106 /* let getbufvar({nr}, "") return the "b:" dictionary. The
11107 * scope prefix before the NUL byte is required by
11108 * find_var_in_ht(). */
11109 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011110 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000011111 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011114 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011115
11116 /* restore previous notion of curbuf */
11117 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118 }
11119
11120 --emsg_off;
11121}
11122
11123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 * "getchar()" function
11125 */
11126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *argvars;
11129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
11131 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011132 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011134 /* Position the cursor. Needed after a message that ends in a space. */
11135 windgoto(msg_row, msg_col);
11136
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 ++no_mapping;
11138 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011139 for (;;)
11140 {
11141 if (argvars[0].v_type == VAR_UNKNOWN)
11142 /* getchar(): blocking wait. */
11143 n = safe_vgetc();
11144 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11145 /* getchar(1): only check if char avail */
11146 n = vpeekc();
11147 else if (error || vpeekc() == NUL)
11148 /* illegal argument or getchar(0) and no char avail: return zero */
11149 n = 0;
11150 else
11151 /* getchar(0) and char avail: return char */
11152 n = safe_vgetc();
11153 if (n == K_IGNORE)
11154 continue;
11155 break;
11156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 --no_mapping;
11158 --allow_keys;
11159
Bram Moolenaar219b8702006-11-01 14:32:36 +000011160 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11161 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11162 vimvars[VV_MOUSE_COL].vv_nr = 0;
11163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 if (IS_SPECIAL(n) || mod_mask != 0)
11166 {
11167 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11168 int i = 0;
11169
11170 /* Turn a special key into three bytes, plus modifier. */
11171 if (mod_mask != 0)
11172 {
11173 temp[i++] = K_SPECIAL;
11174 temp[i++] = KS_MODIFIER;
11175 temp[i++] = mod_mask;
11176 }
11177 if (IS_SPECIAL(n))
11178 {
11179 temp[i++] = K_SPECIAL;
11180 temp[i++] = K_SECOND(n);
11181 temp[i++] = K_THIRD(n);
11182 }
11183#ifdef FEAT_MBYTE
11184 else if (has_mbyte)
11185 i += (*mb_char2bytes)(n, temp + i);
11186#endif
11187 else
11188 temp[i++] = n;
11189 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 rettv->v_type = VAR_STRING;
11191 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011192
11193#ifdef FEAT_MOUSE
11194 if (n == K_LEFTMOUSE
11195 || n == K_LEFTMOUSE_NM
11196 || n == K_LEFTDRAG
11197 || n == K_LEFTRELEASE
11198 || n == K_LEFTRELEASE_NM
11199 || n == K_MIDDLEMOUSE
11200 || n == K_MIDDLEDRAG
11201 || n == K_MIDDLERELEASE
11202 || n == K_RIGHTMOUSE
11203 || n == K_RIGHTDRAG
11204 || n == K_RIGHTRELEASE
11205 || n == K_X1MOUSE
11206 || n == K_X1DRAG
11207 || n == K_X1RELEASE
11208 || n == K_X2MOUSE
11209 || n == K_X2DRAG
11210 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020011211 || n == K_MOUSELEFT
11212 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000011213 || n == K_MOUSEDOWN
11214 || n == K_MOUSEUP)
11215 {
11216 int row = mouse_row;
11217 int col = mouse_col;
11218 win_T *win;
11219 linenr_T lnum;
11220# ifdef FEAT_WINDOWS
11221 win_T *wp;
11222# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011223 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011224
11225 if (row >= 0 && col >= 0)
11226 {
11227 /* Find the window at the mouse coordinates and compute the
11228 * text position. */
11229 win = mouse_find_win(&row, &col);
11230 (void)mouse_comp_pos(win, &row, &col, &lnum);
11231# ifdef FEAT_WINDOWS
11232 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011233 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011234# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011235 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011236 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11237 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11238 }
11239 }
11240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 }
11242}
11243
11244/*
11245 * "getcharmod()" function
11246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011249 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253}
11254
11255/*
11256 * "getcmdline()" function
11257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011260 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 rettv->v_type = VAR_STRING;
11264 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265}
11266
11267/*
11268 * "getcmdpos()" function
11269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011272 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276}
11277
11278/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011279 * "getcmdtype()" function
11280 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011281 static void
11282f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011283 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011284 typval_T *rettv;
11285{
11286 rettv->v_type = VAR_STRING;
11287 rettv->vval.v_string = alloc(2);
11288 if (rettv->vval.v_string != NULL)
11289 {
11290 rettv->vval.v_string[0] = get_cmdline_type();
11291 rettv->vval.v_string[1] = NUL;
11292 }
11293}
11294
11295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 * "getcwd()" function
11297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011300 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011303 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011306 rettv->vval.v_string = NULL;
11307 cwd = alloc(MAXPATHL);
11308 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011310 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11311 {
11312 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011314 if (rettv->vval.v_string != NULL)
11315 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011317 }
11318 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 }
11320}
11321
11322/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011323 * "getfontname()" function
11324 */
11325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011327 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011328 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011329{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 rettv->v_type = VAR_STRING;
11331 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011332#ifdef FEAT_GUI
11333 if (gui.in_use)
11334 {
11335 GuiFont font;
11336 char_u *name = NULL;
11337
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011338 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011339 {
11340 /* Get the "Normal" font. Either the name saved by
11341 * hl_set_font_name() or from the font ID. */
11342 font = gui.norm_font;
11343 name = hl_get_font_name();
11344 }
11345 else
11346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011348 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11349 return;
11350 font = gui_mch_get_font(name, FALSE);
11351 if (font == NOFONT)
11352 return; /* Invalid font name, return empty string. */
11353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011355 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011356 gui_mch_free_font(font);
11357 }
11358#endif
11359}
11360
11361/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011362 * "getfperm({fname})" function
11363 */
11364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011366 typval_T *argvars;
11367 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011368{
11369 char_u *fname;
11370 struct stat st;
11371 char_u *perm = NULL;
11372 char_u flags[] = "rwx";
11373 int i;
11374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011378 if (mch_stat((char *)fname, &st) >= 0)
11379 {
11380 perm = vim_strsave((char_u *)"---------");
11381 if (perm != NULL)
11382 {
11383 for (i = 0; i < 9; i++)
11384 {
11385 if (st.st_mode & (1 << (8 - i)))
11386 perm[i] = flags[i % 3];
11387 }
11388 }
11389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011391}
11392
11393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 * "getfsize({fname})" function
11395 */
11396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011398 typval_T *argvars;
11399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400{
11401 char_u *fname;
11402 struct stat st;
11403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407
11408 if (mch_stat((char *)fname, &st) >= 0)
11409 {
11410 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011415
11416 /* non-perfect check for overflow */
11417 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11418 rettv->vval.v_number = -2;
11419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 }
11421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423}
11424
11425/*
11426 * "getftime({fname})" function
11427 */
11428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011430 typval_T *argvars;
11431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432{
11433 char_u *fname;
11434 struct stat st;
11435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437
11438 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442}
11443
11444/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011445 * "getftype({fname})" function
11446 */
11447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 typval_T *argvars;
11450 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011451{
11452 char_u *fname;
11453 struct stat st;
11454 char_u *type = NULL;
11455 char *t;
11456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011460 if (mch_lstat((char *)fname, &st) >= 0)
11461 {
11462#ifdef S_ISREG
11463 if (S_ISREG(st.st_mode))
11464 t = "file";
11465 else if (S_ISDIR(st.st_mode))
11466 t = "dir";
11467# ifdef S_ISLNK
11468 else if (S_ISLNK(st.st_mode))
11469 t = "link";
11470# endif
11471# ifdef S_ISBLK
11472 else if (S_ISBLK(st.st_mode))
11473 t = "bdev";
11474# endif
11475# ifdef S_ISCHR
11476 else if (S_ISCHR(st.st_mode))
11477 t = "cdev";
11478# endif
11479# ifdef S_ISFIFO
11480 else if (S_ISFIFO(st.st_mode))
11481 t = "fifo";
11482# endif
11483# ifdef S_ISSOCK
11484 else if (S_ISSOCK(st.st_mode))
11485 t = "fifo";
11486# endif
11487 else
11488 t = "other";
11489#else
11490# ifdef S_IFMT
11491 switch (st.st_mode & S_IFMT)
11492 {
11493 case S_IFREG: t = "file"; break;
11494 case S_IFDIR: t = "dir"; break;
11495# ifdef S_IFLNK
11496 case S_IFLNK: t = "link"; break;
11497# endif
11498# ifdef S_IFBLK
11499 case S_IFBLK: t = "bdev"; break;
11500# endif
11501# ifdef S_IFCHR
11502 case S_IFCHR: t = "cdev"; break;
11503# endif
11504# ifdef S_IFIFO
11505 case S_IFIFO: t = "fifo"; break;
11506# endif
11507# ifdef S_IFSOCK
11508 case S_IFSOCK: t = "socket"; break;
11509# endif
11510 default: t = "other";
11511 }
11512# else
11513 if (mch_isdir(fname))
11514 t = "dir";
11515 else
11516 t = "file";
11517# endif
11518#endif
11519 type = vim_strsave((char_u *)t);
11520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011522}
11523
11524/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011525 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011526 */
11527 static void
11528f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011529 typval_T *argvars;
11530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531{
11532 linenr_T lnum;
11533 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011534 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535
11536 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011537 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011538 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011539 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011540 retlist = FALSE;
11541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011543 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011545 retlist = TRUE;
11546 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011547
Bram Moolenaar342337a2005-07-21 21:11:17 +000011548 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549}
11550
11551/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011552 * "getmatches()" function
11553 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011554 static void
11555f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011556 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011557 typval_T *rettv;
11558{
11559#ifdef FEAT_SEARCH_EXTRA
11560 dict_T *dict;
11561 matchitem_T *cur = curwin->w_match_head;
11562
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011563 if (rettv_list_alloc(rettv) == OK)
11564 {
11565 while (cur != NULL)
11566 {
11567 dict = dict_alloc();
11568 if (dict == NULL)
11569 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011570 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11571 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11572 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11573 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11574 list_append_dict(rettv->vval.v_list, dict);
11575 cur = cur->next;
11576 }
11577 }
11578#endif
11579}
11580
11581/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011582 * "getpid()" function
11583 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011584 static void
11585f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011586 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011587 typval_T *rettv;
11588{
11589 rettv->vval.v_number = mch_get_pid();
11590}
11591
11592/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011593 * "getpos(string)" function
11594 */
11595 static void
11596f_getpos(argvars, rettv)
11597 typval_T *argvars;
11598 typval_T *rettv;
11599{
11600 pos_T *fp;
11601 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011602 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011603
11604 if (rettv_list_alloc(rettv) == OK)
11605 {
11606 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011607 fp = var2fpos(&argvars[0], TRUE, &fnum);
11608 if (fnum != -1)
11609 list_append_number(l, (varnumber_T)fnum);
11610 else
11611 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011612 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11613 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011614 list_append_number(l, (fp != NULL)
11615 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011616 : (varnumber_T)0);
11617 list_append_number(l,
11618#ifdef FEAT_VIRTUALEDIT
11619 (fp != NULL) ? (varnumber_T)fp->coladd :
11620#endif
11621 (varnumber_T)0);
11622 }
11623 else
11624 rettv->vval.v_number = FALSE;
11625}
11626
11627/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011628 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011629 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011630 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011631f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011632 typval_T *argvars UNUSED;
11633 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011634{
11635#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011636 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011637#endif
11638
Bram Moolenaar2641f772005-03-25 21:58:17 +000011639#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011640 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011641 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011642 wp = NULL;
11643 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11644 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011645 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011646 if (wp == NULL)
11647 return;
11648 }
11649
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011650 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011651 }
11652#endif
11653}
11654
11655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 * "getreg()" function
11657 */
11658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011660 typval_T *argvars;
11661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
11663 char_u *strregname;
11664 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011665 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011666 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011668 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670 strregname = get_tv_string_chk(&argvars[0]);
11671 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011676 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 regname = (strregname == NULL ? '"' : *strregname);
11678 if (regname == 0)
11679 regname = '"';
11680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 rettv->vval.v_string = error ? NULL :
11683 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684}
11685
11686/*
11687 * "getregtype()" function
11688 */
11689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011691 typval_T *argvars;
11692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693{
11694 char_u *strregname;
11695 int regname;
11696 char_u buf[NUMBUFLEN + 2];
11697 long reglen = 0;
11698
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011699 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011700 {
11701 strregname = get_tv_string_chk(&argvars[0]);
11702 if (strregname == NULL) /* type error; errmsg already given */
11703 {
11704 rettv->v_type = VAR_STRING;
11705 rettv->vval.v_string = NULL;
11706 return;
11707 }
11708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 else
11710 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011711 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712
11713 regname = (strregname == NULL ? '"' : *strregname);
11714 if (regname == 0)
11715 regname = '"';
11716
11717 buf[0] = NUL;
11718 buf[1] = NUL;
11719 switch (get_reg_type(regname, &reglen))
11720 {
11721 case MLINE: buf[0] = 'V'; break;
11722 case MCHAR: buf[0] = 'v'; break;
11723#ifdef FEAT_VISUAL
11724 case MBLOCK:
11725 buf[0] = Ctrl_V;
11726 sprintf((char *)buf + 1, "%ld", reglen + 1);
11727 break;
11728#endif
11729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->v_type = VAR_STRING;
11731 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732}
11733
11734/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011735 * "gettabvar()" function
11736 */
11737 static void
11738f_gettabvar(argvars, rettv)
11739 typval_T *argvars;
11740 typval_T *rettv;
11741{
11742 tabpage_T *tp;
11743 dictitem_T *v;
11744 char_u *varname;
11745
11746 rettv->v_type = VAR_STRING;
11747 rettv->vval.v_string = NULL;
11748
11749 varname = get_tv_string_chk(&argvars[1]);
11750 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11751 if (tp != NULL && varname != NULL)
11752 {
11753 /* look up the variable */
11754 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11755 if (v != NULL)
11756 copy_tv(&v->di_tv, rettv);
11757 }
11758}
11759
11760/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011761 * "gettabwinvar()" function
11762 */
11763 static void
11764f_gettabwinvar(argvars, rettv)
11765 typval_T *argvars;
11766 typval_T *rettv;
11767{
11768 getwinvar(argvars, rettv, 1);
11769}
11770
11771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 * "getwinposx()" function
11773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011775f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011776 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780#ifdef FEAT_GUI
11781 if (gui.in_use)
11782 {
11783 int x, y;
11784
11785 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 }
11788#endif
11789}
11790
11791/*
11792 * "getwinposy()" function
11793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011796 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800#ifdef FEAT_GUI
11801 if (gui.in_use)
11802 {
11803 int x, y;
11804
11805 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 }
11808#endif
11809}
11810
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011811/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011812 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011813 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011814 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011815find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011816 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011817 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011818{
11819#ifdef FEAT_WINDOWS
11820 win_T *wp;
11821#endif
11822 int nr;
11823
11824 nr = get_tv_number_chk(vp, NULL);
11825
11826#ifdef FEAT_WINDOWS
11827 if (nr < 0)
11828 return NULL;
11829 if (nr == 0)
11830 return curwin;
11831
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011832 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11833 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011834 if (--nr <= 0)
11835 break;
11836 return wp;
11837#else
11838 if (nr == 0 || nr == 1)
11839 return curwin;
11840 return NULL;
11841#endif
11842}
11843
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844/*
11845 * "getwinvar()" function
11846 */
11847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011849 typval_T *argvars;
11850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011852 getwinvar(argvars, rettv, 0);
11853}
11854
11855/*
11856 * getwinvar() and gettabwinvar()
11857 */
11858 static void
11859getwinvar(argvars, rettv, off)
11860 typval_T *argvars;
11861 typval_T *rettv;
11862 int off; /* 1 for gettabwinvar() */
11863{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 win_T *win, *oldcurwin;
11865 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011866 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011867 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011869#ifdef FEAT_WINDOWS
11870 if (off == 1)
11871 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11872 else
11873 tp = curtab;
11874#endif
11875 win = find_win_by_nr(&argvars[off], tp);
11876 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011877 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->v_type = VAR_STRING;
11880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881
11882 if (win != NULL && varname != NULL)
11883 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011884 /* Set curwin to be our win, temporarily. Also set curbuf, so
11885 * that we can get buffer-local options. */
11886 oldcurwin = curwin;
11887 curwin = win;
11888 curbuf = win->w_buffer;
11889
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011891 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 else
11893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011894 if (*varname == NUL)
11895 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11896 * scope prefix before the NUL byte is required by
11897 * find_var_in_ht(). */
11898 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011900 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011904
11905 /* restore previous notion of curwin */
11906 curwin = oldcurwin;
11907 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 }
11909
11910 --emsg_off;
11911}
11912
11913/*
11914 * "glob()" function
11915 */
11916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011917f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *argvars;
11919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011921 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011923 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011925 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011926 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011928 if (argvars[1].v_type != VAR_UNKNOWN)
11929 {
11930 if (get_tv_number_chk(&argvars[1], &error))
11931 options |= WILD_KEEP_ALL;
11932 if (argvars[2].v_type != VAR_UNKNOWN
11933 && get_tv_number_chk(&argvars[2], &error))
11934 {
11935 rettv->v_type = VAR_LIST;
11936 rettv->vval.v_list = NULL;
11937 }
11938 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011939 if (!error)
11940 {
11941 ExpandInit(&xpc);
11942 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011943 if (p_wic)
11944 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011945 if (rettv->v_type == VAR_STRING)
11946 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011947 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011948 else if (rettv_list_alloc(rettv) != FAIL)
11949 {
11950 int i;
11951
11952 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11953 NULL, options, WILD_ALL_KEEP);
11954 for (i = 0; i < xpc.xp_numfiles; i++)
11955 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11956
11957 ExpandCleanup(&xpc);
11958 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011959 }
11960 else
11961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962}
11963
11964/*
11965 * "globpath()" function
11966 */
11967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 typval_T *argvars;
11970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011972 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011975 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011977 /* When the optional second argument is non-zero, don't remove matches
11978 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11979 if (argvars[2].v_type != VAR_UNKNOWN
11980 && get_tv_number_chk(&argvars[2], &error))
11981 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011983 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 rettv->vval.v_string = NULL;
11985 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011986 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11987 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988}
11989
11990/*
11991 * "has()" function
11992 */
11993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011995 typval_T *argvars;
11996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997{
11998 int i;
11999 char_u *name;
12000 int n = FALSE;
12001 static char *(has_list[]) =
12002 {
12003#ifdef AMIGA
12004 "amiga",
12005# ifdef FEAT_ARP
12006 "arp",
12007# endif
12008#endif
12009#ifdef __BEOS__
12010 "beos",
12011#endif
12012#ifdef MSDOS
12013# ifdef DJGPP
12014 "dos32",
12015# else
12016 "dos16",
12017# endif
12018#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012019#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 "mac",
12021#endif
12022#if defined(MACOS_X_UNIX)
12023 "macunix",
12024#endif
12025#ifdef OS2
12026 "os2",
12027#endif
12028#ifdef __QNX__
12029 "qnx",
12030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031#ifdef UNIX
12032 "unix",
12033#endif
12034#ifdef VMS
12035 "vms",
12036#endif
12037#ifdef WIN16
12038 "win16",
12039#endif
12040#ifdef WIN32
12041 "win32",
12042#endif
12043#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12044 "win32unix",
12045#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012046#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 "win64",
12048#endif
12049#ifdef EBCDIC
12050 "ebcdic",
12051#endif
12052#ifndef CASE_INSENSITIVE_FILENAME
12053 "fname_case",
12054#endif
12055#ifdef FEAT_ARABIC
12056 "arabic",
12057#endif
12058#ifdef FEAT_AUTOCMD
12059 "autocmd",
12060#endif
12061#ifdef FEAT_BEVAL
12062 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012063# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12064 "balloon_multiline",
12065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066#endif
12067#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12068 "builtin_terms",
12069# ifdef ALL_BUILTIN_TCAPS
12070 "all_builtin_terms",
12071# endif
12072#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012073#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12074 || defined(FEAT_GUI_W32) \
12075 || defined(FEAT_GUI_MOTIF))
12076 "browsefilter",
12077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078#ifdef FEAT_BYTEOFF
12079 "byte_offset",
12080#endif
12081#ifdef FEAT_CINDENT
12082 "cindent",
12083#endif
12084#ifdef FEAT_CLIENTSERVER
12085 "clientserver",
12086#endif
12087#ifdef FEAT_CLIPBOARD
12088 "clipboard",
12089#endif
12090#ifdef FEAT_CMDL_COMPL
12091 "cmdline_compl",
12092#endif
12093#ifdef FEAT_CMDHIST
12094 "cmdline_hist",
12095#endif
12096#ifdef FEAT_COMMENTS
12097 "comments",
12098#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012099#ifdef FEAT_CONCEAL
12100 "conceal",
12101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102#ifdef FEAT_CRYPT
12103 "cryptv",
12104#endif
12105#ifdef FEAT_CSCOPE
12106 "cscope",
12107#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012108#ifdef FEAT_CURSORBIND
12109 "cursorbind",
12110#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012111#ifdef CURSOR_SHAPE
12112 "cursorshape",
12113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114#ifdef DEBUG
12115 "debug",
12116#endif
12117#ifdef FEAT_CON_DIALOG
12118 "dialog_con",
12119#endif
12120#ifdef FEAT_GUI_DIALOG
12121 "dialog_gui",
12122#endif
12123#ifdef FEAT_DIFF
12124 "diff",
12125#endif
12126#ifdef FEAT_DIGRAPHS
12127 "digraphs",
12128#endif
12129#ifdef FEAT_DND
12130 "dnd",
12131#endif
12132#ifdef FEAT_EMACS_TAGS
12133 "emacs_tags",
12134#endif
12135 "eval", /* always present, of course! */
12136#ifdef FEAT_EX_EXTRA
12137 "ex_extra",
12138#endif
12139#ifdef FEAT_SEARCH_EXTRA
12140 "extra_search",
12141#endif
12142#ifdef FEAT_FKMAP
12143 "farsi",
12144#endif
12145#ifdef FEAT_SEARCHPATH
12146 "file_in_path",
12147#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012148#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012149 "filterpipe",
12150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151#ifdef FEAT_FIND_ID
12152 "find_in_path",
12153#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012154#ifdef FEAT_FLOAT
12155 "float",
12156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157#ifdef FEAT_FOLDING
12158 "folding",
12159#endif
12160#ifdef FEAT_FOOTER
12161 "footer",
12162#endif
12163#if !defined(USE_SYSTEM) && defined(UNIX)
12164 "fork",
12165#endif
12166#ifdef FEAT_GETTEXT
12167 "gettext",
12168#endif
12169#ifdef FEAT_GUI
12170 "gui",
12171#endif
12172#ifdef FEAT_GUI_ATHENA
12173# ifdef FEAT_GUI_NEXTAW
12174 "gui_neXtaw",
12175# else
12176 "gui_athena",
12177# endif
12178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179#ifdef FEAT_GUI_GTK
12180 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012183#ifdef FEAT_GUI_GNOME
12184 "gui_gnome",
12185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186#ifdef FEAT_GUI_MAC
12187 "gui_mac",
12188#endif
12189#ifdef FEAT_GUI_MOTIF
12190 "gui_motif",
12191#endif
12192#ifdef FEAT_GUI_PHOTON
12193 "gui_photon",
12194#endif
12195#ifdef FEAT_GUI_W16
12196 "gui_win16",
12197#endif
12198#ifdef FEAT_GUI_W32
12199 "gui_win32",
12200#endif
12201#ifdef FEAT_HANGULIN
12202 "hangul_input",
12203#endif
12204#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12205 "iconv",
12206#endif
12207#ifdef FEAT_INS_EXPAND
12208 "insert_expand",
12209#endif
12210#ifdef FEAT_JUMPLIST
12211 "jumplist",
12212#endif
12213#ifdef FEAT_KEYMAP
12214 "keymap",
12215#endif
12216#ifdef FEAT_LANGMAP
12217 "langmap",
12218#endif
12219#ifdef FEAT_LIBCALL
12220 "libcall",
12221#endif
12222#ifdef FEAT_LINEBREAK
12223 "linebreak",
12224#endif
12225#ifdef FEAT_LISP
12226 "lispindent",
12227#endif
12228#ifdef FEAT_LISTCMDS
12229 "listcmds",
12230#endif
12231#ifdef FEAT_LOCALMAP
12232 "localmap",
12233#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012234#ifdef FEAT_LUA
12235# ifndef DYNAMIC_LUA
12236 "lua",
12237# endif
12238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239#ifdef FEAT_MENU
12240 "menu",
12241#endif
12242#ifdef FEAT_SESSION
12243 "mksession",
12244#endif
12245#ifdef FEAT_MODIFY_FNAME
12246 "modify_fname",
12247#endif
12248#ifdef FEAT_MOUSE
12249 "mouse",
12250#endif
12251#ifdef FEAT_MOUSESHAPE
12252 "mouseshape",
12253#endif
12254#if defined(UNIX) || defined(VMS)
12255# ifdef FEAT_MOUSE_DEC
12256 "mouse_dec",
12257# endif
12258# ifdef FEAT_MOUSE_GPM
12259 "mouse_gpm",
12260# endif
12261# ifdef FEAT_MOUSE_JSB
12262 "mouse_jsbterm",
12263# endif
12264# ifdef FEAT_MOUSE_NET
12265 "mouse_netterm",
12266# endif
12267# ifdef FEAT_MOUSE_PTERM
12268 "mouse_pterm",
12269# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012270# ifdef FEAT_SYSMOUSE
12271 "mouse_sysmouse",
12272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273# ifdef FEAT_MOUSE_XTERM
12274 "mouse_xterm",
12275# endif
12276#endif
12277#ifdef FEAT_MBYTE
12278 "multi_byte",
12279#endif
12280#ifdef FEAT_MBYTE_IME
12281 "multi_byte_ime",
12282#endif
12283#ifdef FEAT_MULTI_LANG
12284 "multi_lang",
12285#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012286#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012287#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012288 "mzscheme",
12289#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291#ifdef FEAT_OLE
12292 "ole",
12293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294#ifdef FEAT_PATH_EXTRA
12295 "path_extra",
12296#endif
12297#ifdef FEAT_PERL
12298#ifndef DYNAMIC_PERL
12299 "perl",
12300#endif
12301#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012302#ifdef FEAT_PERSISTENT_UNDO
12303 "persistent_undo",
12304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305#ifdef FEAT_PYTHON
12306#ifndef DYNAMIC_PYTHON
12307 "python",
12308#endif
12309#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012310#ifdef FEAT_PYTHON3
12311#ifndef DYNAMIC_PYTHON3
12312 "python3",
12313#endif
12314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315#ifdef FEAT_POSTSCRIPT
12316 "postscript",
12317#endif
12318#ifdef FEAT_PRINTER
12319 "printer",
12320#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012321#ifdef FEAT_PROFILE
12322 "profile",
12323#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012324#ifdef FEAT_RELTIME
12325 "reltime",
12326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327#ifdef FEAT_QUICKFIX
12328 "quickfix",
12329#endif
12330#ifdef FEAT_RIGHTLEFT
12331 "rightleft",
12332#endif
12333#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12334 "ruby",
12335#endif
12336#ifdef FEAT_SCROLLBIND
12337 "scrollbind",
12338#endif
12339#ifdef FEAT_CMDL_INFO
12340 "showcmd",
12341 "cmdline_info",
12342#endif
12343#ifdef FEAT_SIGNS
12344 "signs",
12345#endif
12346#ifdef FEAT_SMARTINDENT
12347 "smartindent",
12348#endif
12349#ifdef FEAT_SNIFF
12350 "sniff",
12351#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012352#ifdef STARTUPTIME
12353 "startuptime",
12354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355#ifdef FEAT_STL_OPT
12356 "statusline",
12357#endif
12358#ifdef FEAT_SUN_WORKSHOP
12359 "sun_workshop",
12360#endif
12361#ifdef FEAT_NETBEANS_INTG
12362 "netbeans_intg",
12363#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012364#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012365 "spell",
12366#endif
12367#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 "syntax",
12369#endif
12370#if defined(USE_SYSTEM) || !defined(UNIX)
12371 "system",
12372#endif
12373#ifdef FEAT_TAG_BINS
12374 "tag_binary",
12375#endif
12376#ifdef FEAT_TAG_OLDSTATIC
12377 "tag_old_static",
12378#endif
12379#ifdef FEAT_TAG_ANYWHITE
12380 "tag_any_white",
12381#endif
12382#ifdef FEAT_TCL
12383# ifndef DYNAMIC_TCL
12384 "tcl",
12385# endif
12386#endif
12387#ifdef TERMINFO
12388 "terminfo",
12389#endif
12390#ifdef FEAT_TERMRESPONSE
12391 "termresponse",
12392#endif
12393#ifdef FEAT_TEXTOBJ
12394 "textobjects",
12395#endif
12396#ifdef HAVE_TGETENT
12397 "tgetent",
12398#endif
12399#ifdef FEAT_TITLE
12400 "title",
12401#endif
12402#ifdef FEAT_TOOLBAR
12403 "toolbar",
12404#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012405#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12406 "unnamedplus",
12407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#ifdef FEAT_USR_CMDS
12409 "user-commands", /* was accidentally included in 5.4 */
12410 "user_commands",
12411#endif
12412#ifdef FEAT_VIMINFO
12413 "viminfo",
12414#endif
12415#ifdef FEAT_VERTSPLIT
12416 "vertsplit",
12417#endif
12418#ifdef FEAT_VIRTUALEDIT
12419 "virtualedit",
12420#endif
12421#ifdef FEAT_VISUAL
12422 "visual",
12423#endif
12424#ifdef FEAT_VISUALEXTRA
12425 "visualextra",
12426#endif
12427#ifdef FEAT_VREPLACE
12428 "vreplace",
12429#endif
12430#ifdef FEAT_WILDIGN
12431 "wildignore",
12432#endif
12433#ifdef FEAT_WILDMENU
12434 "wildmenu",
12435#endif
12436#ifdef FEAT_WINDOWS
12437 "windows",
12438#endif
12439#ifdef FEAT_WAK
12440 "winaltkeys",
12441#endif
12442#ifdef FEAT_WRITEBACKUP
12443 "writebackup",
12444#endif
12445#ifdef FEAT_XIM
12446 "xim",
12447#endif
12448#ifdef FEAT_XFONTSET
12449 "xfontset",
12450#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012451#ifdef FEAT_XPM_W32
12452 "xpm_w32",
12453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454#ifdef USE_XSMP
12455 "xsmp",
12456#endif
12457#ifdef USE_XSMP_INTERACT
12458 "xsmp_interact",
12459#endif
12460#ifdef FEAT_XCLIPBOARD
12461 "xterm_clipboard",
12462#endif
12463#ifdef FEAT_XTERM_SAVE
12464 "xterm_save",
12465#endif
12466#if defined(UNIX) && defined(FEAT_X11)
12467 "X11",
12468#endif
12469 NULL
12470 };
12471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 for (i = 0; has_list[i] != NULL; ++i)
12474 if (STRICMP(name, has_list[i]) == 0)
12475 {
12476 n = TRUE;
12477 break;
12478 }
12479
12480 if (n == FALSE)
12481 {
12482 if (STRNICMP(name, "patch", 5) == 0)
12483 n = has_patch(atoi((char *)name + 5));
12484 else if (STRICMP(name, "vim_starting") == 0)
12485 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012486#ifdef FEAT_MBYTE
12487 else if (STRICMP(name, "multi_byte_encoding") == 0)
12488 n = has_mbyte;
12489#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012490#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12491 else if (STRICMP(name, "balloon_multiline") == 0)
12492 n = multiline_balloon_available();
12493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494#ifdef DYNAMIC_TCL
12495 else if (STRICMP(name, "tcl") == 0)
12496 n = tcl_enabled(FALSE);
12497#endif
12498#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12499 else if (STRICMP(name, "iconv") == 0)
12500 n = iconv_enabled(FALSE);
12501#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012502#ifdef DYNAMIC_LUA
12503 else if (STRICMP(name, "lua") == 0)
12504 n = lua_enabled(FALSE);
12505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012506#ifdef DYNAMIC_MZSCHEME
12507 else if (STRICMP(name, "mzscheme") == 0)
12508 n = mzscheme_enabled(FALSE);
12509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510#ifdef DYNAMIC_RUBY
12511 else if (STRICMP(name, "ruby") == 0)
12512 n = ruby_enabled(FALSE);
12513#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012514#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515#ifdef DYNAMIC_PYTHON
12516 else if (STRICMP(name, "python") == 0)
12517 n = python_enabled(FALSE);
12518#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012519#endif
12520#ifdef FEAT_PYTHON3
12521#ifdef DYNAMIC_PYTHON3
12522 else if (STRICMP(name, "python3") == 0)
12523 n = python3_enabled(FALSE);
12524#endif
12525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526#ifdef DYNAMIC_PERL
12527 else if (STRICMP(name, "perl") == 0)
12528 n = perl_enabled(FALSE);
12529#endif
12530#ifdef FEAT_GUI
12531 else if (STRICMP(name, "gui_running") == 0)
12532 n = (gui.in_use || gui.starting);
12533# ifdef FEAT_GUI_W32
12534 else if (STRICMP(name, "gui_win32s") == 0)
12535 n = gui_is_win32s();
12536# endif
12537# ifdef FEAT_BROWSE
12538 else if (STRICMP(name, "browse") == 0)
12539 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12540# endif
12541#endif
12542#ifdef FEAT_SYN_HL
12543 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012544 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545#endif
12546#if defined(WIN3264)
12547 else if (STRICMP(name, "win95") == 0)
12548 n = mch_windows95();
12549#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012550#ifdef FEAT_NETBEANS_INTG
12551 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012552 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 }
12555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012556 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557}
12558
12559/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012560 * "has_key()" function
12561 */
12562 static void
12563f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012564 typval_T *argvars;
12565 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012566{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012567 if (argvars[0].v_type != VAR_DICT)
12568 {
12569 EMSG(_(e_dictreq));
12570 return;
12571 }
12572 if (argvars[0].vval.v_dict == NULL)
12573 return;
12574
12575 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012576 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012577}
12578
12579/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012580 * "haslocaldir()" function
12581 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012582 static void
12583f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012584 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012585 typval_T *rettv;
12586{
12587 rettv->vval.v_number = (curwin->w_localdir != NULL);
12588}
12589
12590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 * "hasmapto()" function
12592 */
12593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012594f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012595 typval_T *argvars;
12596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597{
12598 char_u *name;
12599 char_u *mode;
12600 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012601 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012604 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 mode = (char_u *)"nvo";
12606 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012608 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012609 if (argvars[2].v_type != VAR_UNKNOWN)
12610 abbr = get_tv_number(&argvars[2]);
12611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612
Bram Moolenaar2c932302006-03-18 21:42:09 +000012613 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012614 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617}
12618
12619/*
12620 * "histadd()" function
12621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012623f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626{
12627#ifdef FEAT_CMDHIST
12628 int histype;
12629 char_u *str;
12630 char_u buf[NUMBUFLEN];
12631#endif
12632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012633 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 if (check_restricted() || check_secure())
12635 return;
12636#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012637 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12638 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639 if (histype >= 0)
12640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012641 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642 if (*str != NUL)
12643 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012644 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647 return;
12648 }
12649 }
12650#endif
12651}
12652
12653/*
12654 * "histdel()" function
12655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012658 typval_T *argvars UNUSED;
12659 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660{
12661#ifdef FEAT_CMDHIST
12662 int n;
12663 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012664 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12667 if (str == NULL)
12668 n = 0;
12669 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012671 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012672 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 else
12677 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012678 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 get_tv_string_buf(&argvars[1], buf));
12680 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681#endif
12682}
12683
12684/*
12685 * "histget()" function
12686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691{
12692#ifdef FEAT_CMDHIST
12693 int type;
12694 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012695 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012697 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12698 if (str == NULL)
12699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 {
12702 type = get_histtype(str);
12703 if (argvars[1].v_type == VAR_UNKNOWN)
12704 idx = get_history_idx(type);
12705 else
12706 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12707 /* -1 on type error */
12708 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012713 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714}
12715
12716/*
12717 * "histnr()" function
12718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012721 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723{
12724 int i;
12725
12726#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 char_u *history = get_tv_string_chk(&argvars[0]);
12728
12729 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 if (i >= HIST_CMD && i < HIST_COUNT)
12731 i = get_history_idx(i);
12732 else
12733#endif
12734 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736}
12737
12738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 * "highlightID(name)" function
12740 */
12741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012743 typval_T *argvars;
12744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747}
12748
12749/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012750 * "highlight_exists()" function
12751 */
12752 static void
12753f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012754 typval_T *argvars;
12755 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012756{
12757 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12758}
12759
12760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 * "hostname()" function
12762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012765 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767{
12768 char_u hostname[256];
12769
12770 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->v_type = VAR_STRING;
12772 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773}
12774
12775/*
12776 * iconv() function
12777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782{
12783#ifdef FEAT_MBYTE
12784 char_u buf1[NUMBUFLEN];
12785 char_u buf2[NUMBUFLEN];
12786 char_u *from, *to, *str;
12787 vimconv_T vimconv;
12788#endif
12789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->v_type = VAR_STRING;
12791 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792
12793#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 str = get_tv_string(&argvars[0]);
12795 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12796 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 vimconv.vc_type = CONV_NONE;
12798 convert_setup(&vimconv, from, to);
12799
12800 /* If the encodings are equal, no conversion needed. */
12801 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805
12806 convert_setup(&vimconv, NULL, NULL);
12807 vim_free(from);
12808 vim_free(to);
12809#endif
12810}
12811
12812/*
12813 * "indent()" function
12814 */
12815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012817 typval_T *argvars;
12818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819{
12820 linenr_T lnum;
12821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827}
12828
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012829/*
12830 * "index()" function
12831 */
12832 static void
12833f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012834 typval_T *argvars;
12835 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012836{
Bram Moolenaar33570922005-01-25 22:26:29 +000012837 list_T *l;
12838 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012839 long idx = 0;
12840 int ic = FALSE;
12841
12842 rettv->vval.v_number = -1;
12843 if (argvars[0].v_type != VAR_LIST)
12844 {
12845 EMSG(_(e_listreq));
12846 return;
12847 }
12848 l = argvars[0].vval.v_list;
12849 if (l != NULL)
12850 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012851 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012852 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012854 int error = FALSE;
12855
Bram Moolenaar758711c2005-02-02 23:11:38 +000012856 /* Start at specified item. Use the cached index that list_find()
12857 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012858 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012859 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012860 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012861 ic = get_tv_number_chk(&argvars[3], &error);
12862 if (error)
12863 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012864 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012865
Bram Moolenaar758711c2005-02-02 23:11:38 +000012866 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012867 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012868 {
12869 rettv->vval.v_number = idx;
12870 break;
12871 }
12872 }
12873}
12874
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875static int inputsecret_flag = 0;
12876
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012877static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12878
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012880 * This function is used by f_input() and f_inputdialog() functions. The third
12881 * argument to f_input() specifies the type of completion to use at the
12882 * prompt. The third argument to f_inputdialog() specifies the value to return
12883 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 */
12885 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012886get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 typval_T *argvars;
12888 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012889 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012891 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 char_u *p = NULL;
12893 int c;
12894 char_u buf[NUMBUFLEN];
12895 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012896 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012897 int xp_type = EXPAND_NOTHING;
12898 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012901 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902
12903#ifdef NO_CONSOLE_INPUT
12904 /* While starting up, there is no place to enter text. */
12905 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907#endif
12908
12909 cmd_silent = FALSE; /* Want to see the prompt. */
12910 if (prompt != NULL)
12911 {
12912 /* Only the part of the message after the last NL is considered as
12913 * prompt for the command line */
12914 p = vim_strrchr(prompt, '\n');
12915 if (p == NULL)
12916 p = prompt;
12917 else
12918 {
12919 ++p;
12920 c = *p;
12921 *p = NUL;
12922 msg_start();
12923 msg_clr_eos();
12924 msg_puts_attr(prompt, echo_attr);
12925 msg_didout = FALSE;
12926 msg_starthere();
12927 *p = c;
12928 }
12929 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012931 if (argvars[1].v_type != VAR_UNKNOWN)
12932 {
12933 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12934 if (defstr != NULL)
12935 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012937 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012938 {
12939 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012940 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012941 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012942
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012943 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012944 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012945
Bram Moolenaar4463f292005-09-25 22:20:24 +000012946 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12947 if (xp_name == NULL)
12948 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012949
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012950 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012951
Bram Moolenaar4463f292005-09-25 22:20:24 +000012952 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12953 &xp_arg) == FAIL)
12954 return;
12955 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012956 }
12957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012958 if (defstr != NULL)
12959 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012960 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12961 xp_type, xp_arg);
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012962 if (rettv->vval.v_string == NULL
12963 && argvars[1].v_type != VAR_UNKNOWN
12964 && argvars[2].v_type != VAR_UNKNOWN)
12965 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
12966 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012967
12968 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012970 /* since the user typed this, no need to wait for return */
12971 need_wait_return = FALSE;
12972 msg_didout = FALSE;
12973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 cmd_silent = cmd_silent_save;
12975}
12976
12977/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012978 * "input()" function
12979 * Also handles inputsecret() when inputsecret is set.
12980 */
12981 static void
12982f_input(argvars, rettv)
12983 typval_T *argvars;
12984 typval_T *rettv;
12985{
12986 get_user_input(argvars, rettv, FALSE);
12987}
12988
12989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 * "inputdialog()" function
12991 */
12992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012994 typval_T *argvars;
12995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996{
12997#if defined(FEAT_GUI_TEXTDIALOG)
12998 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12999 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13000 {
13001 char_u *message;
13002 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013003 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013005 message = get_tv_string_chk(&argvars[0]);
13006 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013007 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013008 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009 else
13010 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013011 if (message != NULL && defstr != NULL
13012 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013013 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013014 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015 else
13016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017 if (message != NULL && defstr != NULL
13018 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013019 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013020 rettv->vval.v_string = vim_strsave(
13021 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013025 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026 }
13027 else
13028#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013029 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030}
13031
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013032/*
13033 * "inputlist()" function
13034 */
13035 static void
13036f_inputlist(argvars, rettv)
13037 typval_T *argvars;
13038 typval_T *rettv;
13039{
13040 listitem_T *li;
13041 int selected;
13042 int mouse_used;
13043
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013044#ifdef NO_CONSOLE_INPUT
13045 /* While starting up, there is no place to enter text. */
13046 if (no_console_input())
13047 return;
13048#endif
13049 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13050 {
13051 EMSG2(_(e_listarg), "inputlist()");
13052 return;
13053 }
13054
13055 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013056 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013057 lines_left = Rows; /* avoid more prompt */
13058 msg_scroll = TRUE;
13059 msg_clr_eos();
13060
13061 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13062 {
13063 msg_puts(get_tv_string(&li->li_tv));
13064 msg_putchar('\n');
13065 }
13066
13067 /* Ask for choice. */
13068 selected = prompt_for_number(&mouse_used);
13069 if (mouse_used)
13070 selected -= lines_left;
13071
13072 rettv->vval.v_number = selected;
13073}
13074
13075
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13077
13078/*
13079 * "inputrestore()" function
13080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013083 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085{
13086 if (ga_userinput.ga_len > 0)
13087 {
13088 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13090 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013091 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 }
13093 else if (p_verbose > 1)
13094 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013095 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013096 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097 }
13098}
13099
13100/*
13101 * "inputsave()" function
13102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013105 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013108 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109 if (ga_grow(&ga_userinput, 1) == OK)
13110 {
13111 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13112 + ga_userinput.ga_len);
13113 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013114 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 }
13116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118}
13119
13120/*
13121 * "inputsecret()" function
13122 */
13123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013124f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013125 typval_T *argvars;
13126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127{
13128 ++cmdline_star;
13129 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131 --cmdline_star;
13132 --inputsecret_flag;
13133}
13134
13135/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013136 * "insert()" function
13137 */
13138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013140 typval_T *argvars;
13141 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013142{
13143 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013144 listitem_T *item;
13145 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013147
13148 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013149 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013150 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013151 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013152 {
13153 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013154 before = get_tv_number_chk(&argvars[2], &error);
13155 if (error)
13156 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013157
Bram Moolenaar758711c2005-02-02 23:11:38 +000013158 if (before == l->lv_len)
13159 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013160 else
13161 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013162 item = list_find(l, before);
13163 if (item == NULL)
13164 {
13165 EMSGN(_(e_listidx), before);
13166 l = NULL;
13167 }
13168 }
13169 if (l != NULL)
13170 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013171 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013172 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013173 }
13174 }
13175}
13176
13177/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013178 * "invert(expr)" function
13179 */
13180 static void
13181f_invert(argvars, rettv)
13182 typval_T *argvars;
13183 typval_T *rettv;
13184{
13185 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13186}
13187
13188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 * "isdirectory()" function
13190 */
13191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013193 typval_T *argvars;
13194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197}
13198
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013199/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013200 * "islocked()" function
13201 */
13202 static void
13203f_islocked(argvars, rettv)
13204 typval_T *argvars;
13205 typval_T *rettv;
13206{
13207 lval_T lv;
13208 char_u *end;
13209 dictitem_T *di;
13210
13211 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013212 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13213 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013214 if (end != NULL && lv.ll_name != NULL)
13215 {
13216 if (*end != NUL)
13217 EMSG(_(e_trailing));
13218 else
13219 {
13220 if (lv.ll_tv == NULL)
13221 {
13222 if (check_changedtick(lv.ll_name))
13223 rettv->vval.v_number = 1; /* always locked */
13224 else
13225 {
13226 di = find_var(lv.ll_name, NULL);
13227 if (di != NULL)
13228 {
13229 /* Consider a variable locked when:
13230 * 1. the variable itself is locked
13231 * 2. the value of the variable is locked.
13232 * 3. the List or Dict value is locked.
13233 */
13234 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13235 || tv_islocked(&di->di_tv));
13236 }
13237 }
13238 }
13239 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013240 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013241 else if (lv.ll_newkey != NULL)
13242 EMSG2(_(e_dictkey), lv.ll_newkey);
13243 else if (lv.ll_list != NULL)
13244 /* List item. */
13245 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13246 else
13247 /* Dictionary item. */
13248 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13249 }
13250 }
13251
13252 clear_lval(&lv);
13253}
13254
Bram Moolenaar33570922005-01-25 22:26:29 +000013255static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013256
13257/*
13258 * Turn a dict into a list:
13259 * "what" == 0: list of keys
13260 * "what" == 1: list of values
13261 * "what" == 2: list of items
13262 */
13263 static void
13264dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013265 typval_T *argvars;
13266 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013267 int what;
13268{
Bram Moolenaar33570922005-01-25 22:26:29 +000013269 list_T *l2;
13270 dictitem_T *di;
13271 hashitem_T *hi;
13272 listitem_T *li;
13273 listitem_T *li2;
13274 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013275 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013276
Bram Moolenaar8c711452005-01-14 21:53:12 +000013277 if (argvars[0].v_type != VAR_DICT)
13278 {
13279 EMSG(_(e_dictreq));
13280 return;
13281 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013282 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013283 return;
13284
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013285 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013286 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013287
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013288 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013289 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013290 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013291 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013293 --todo;
13294 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013295
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013296 li = listitem_alloc();
13297 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013298 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013299 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013300
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013301 if (what == 0)
13302 {
13303 /* keys() */
13304 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013305 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013306 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13307 }
13308 else if (what == 1)
13309 {
13310 /* values() */
13311 copy_tv(&di->di_tv, &li->li_tv);
13312 }
13313 else
13314 {
13315 /* items() */
13316 l2 = list_alloc();
13317 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013318 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013319 li->li_tv.vval.v_list = l2;
13320 if (l2 == NULL)
13321 break;
13322 ++l2->lv_refcount;
13323
13324 li2 = listitem_alloc();
13325 if (li2 == NULL)
13326 break;
13327 list_append(l2, li2);
13328 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013329 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013330 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13331
13332 li2 = listitem_alloc();
13333 if (li2 == NULL)
13334 break;
13335 list_append(l2, li2);
13336 copy_tv(&di->di_tv, &li2->li_tv);
13337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013338 }
13339 }
13340}
13341
13342/*
13343 * "items(dict)" function
13344 */
13345 static void
13346f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 typval_T *argvars;
13348 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013349{
13350 dict_list(argvars, rettv, 2);
13351}
13352
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013354 * "join()" function
13355 */
13356 static void
13357f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013358 typval_T *argvars;
13359 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013360{
13361 garray_T ga;
13362 char_u *sep;
13363
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013364 if (argvars[0].v_type != VAR_LIST)
13365 {
13366 EMSG(_(e_listreq));
13367 return;
13368 }
13369 if (argvars[0].vval.v_list == NULL)
13370 return;
13371 if (argvars[1].v_type == VAR_UNKNOWN)
13372 sep = (char_u *)" ";
13373 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013375
13376 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013377
13378 if (sep != NULL)
13379 {
13380 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013381 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 ga_append(&ga, NUL);
13383 rettv->vval.v_string = (char_u *)ga.ga_data;
13384 }
13385 else
13386 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013387}
13388
13389/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013390 * "keys()" function
13391 */
13392 static void
13393f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013394 typval_T *argvars;
13395 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013396{
13397 dict_list(argvars, rettv, 0);
13398}
13399
13400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 * "last_buffer_nr()" function.
13402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013405 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407{
13408 int n = 0;
13409 buf_T *buf;
13410
13411 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13412 if (n < buf->b_fnum)
13413 n = buf->b_fnum;
13414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013415 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013416}
13417
13418/*
13419 * "len()" function
13420 */
13421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *argvars;
13424 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013425{
13426 switch (argvars[0].v_type)
13427 {
13428 case VAR_STRING:
13429 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013430 rettv->vval.v_number = (varnumber_T)STRLEN(
13431 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013432 break;
13433 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013435 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013436 case VAR_DICT:
13437 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13438 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013439 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013440 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013441 break;
13442 }
13443}
13444
Bram Moolenaar33570922005-01-25 22:26:29 +000013445static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013446
13447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013448libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013449 typval_T *argvars;
13450 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013451 int type;
13452{
13453#ifdef FEAT_LIBCALL
13454 char_u *string_in;
13455 char_u **string_result;
13456 int nr_result;
13457#endif
13458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013460 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013462
13463 if (check_restricted() || check_secure())
13464 return;
13465
13466#ifdef FEAT_LIBCALL
13467 /* The first two args must be strings, otherwise its meaningless */
13468 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13469 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013470 string_in = NULL;
13471 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013472 string_in = argvars[2].vval.v_string;
13473 if (type == VAR_NUMBER)
13474 string_result = NULL;
13475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013477 if (mch_libcall(argvars[0].vval.v_string,
13478 argvars[1].vval.v_string,
13479 string_in,
13480 argvars[2].vval.v_number,
13481 string_result,
13482 &nr_result) == OK
13483 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013484 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013485 }
13486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487}
13488
13489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013490 * "libcall()" function
13491 */
13492 static void
13493f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 typval_T *argvars;
13495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013496{
13497 libcall_common(argvars, rettv, VAR_STRING);
13498}
13499
13500/*
13501 * "libcallnr()" function
13502 */
13503 static void
13504f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 typval_T *argvars;
13506 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013507{
13508 libcall_common(argvars, rettv, VAR_NUMBER);
13509}
13510
13511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 * "line(string)" function
13513 */
13514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013515f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013516 typval_T *argvars;
13517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518{
13519 linenr_T lnum = 0;
13520 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013521 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013523 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 if (fp != NULL)
13525 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013526 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527}
13528
13529/*
13530 * "line2byte(lnum)" function
13531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013534 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536{
13537#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539#else
13540 linenr_T lnum;
13541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013544 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13547 if (rettv->vval.v_number >= 0)
13548 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549#endif
13550}
13551
13552/*
13553 * "lispindent(lnum)" function
13554 */
13555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013556f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013557 typval_T *argvars;
13558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559{
13560#ifdef FEAT_LISP
13561 pos_T pos;
13562 linenr_T lnum;
13563
13564 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13567 {
13568 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 curwin->w_cursor = pos;
13571 }
13572 else
13573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575}
13576
13577/*
13578 * "localtime()" function
13579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013582 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
Bram Moolenaar33570922005-01-25 22:26:29 +000013588static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589
13590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 typval_T *argvars;
13593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 int exact;
13595{
13596 char_u *keys;
13597 char_u *which;
13598 char_u buf[NUMBUFLEN];
13599 char_u *keys_buf = NULL;
13600 char_u *rhs;
13601 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013602 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013603 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013604 mapblock_T *mp;
13605 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606
13607 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 rettv->v_type = VAR_STRING;
13609 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013611 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 if (*keys == NUL)
13613 return;
13614
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013615 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013618 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013619 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013620 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013621 if (argvars[3].v_type != VAR_UNKNOWN)
13622 get_dict = get_tv_number(&argvars[3]);
13623 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 else
13626 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 if (which == NULL)
13628 return;
13629
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 mode = get_map_mode(&which, 0);
13631
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013632 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013633 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013635
13636 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013638 /* Return a string. */
13639 if (rhs != NULL)
13640 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641
Bram Moolenaarbd743252010-10-20 21:23:33 +020013642 }
13643 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13644 {
13645 /* Return a dictionary. */
13646 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13647 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13648 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
Bram Moolenaarbd743252010-10-20 21:23:33 +020013650 dict_add_nr_str(dict, "lhs", 0L, lhs);
13651 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13652 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13653 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13654 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13655 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13656 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13657 dict_add_nr_str(dict, "mode", 0L, mapmode);
13658
13659 vim_free(lhs);
13660 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661 }
13662}
13663
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013664#ifdef FEAT_FLOAT
13665/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013666 * "log()" function
13667 */
13668 static void
13669f_log(argvars, rettv)
13670 typval_T *argvars;
13671 typval_T *rettv;
13672{
13673 float_T f;
13674
13675 rettv->v_type = VAR_FLOAT;
13676 if (get_float_arg(argvars, &f) == OK)
13677 rettv->vval.v_float = log(f);
13678 else
13679 rettv->vval.v_float = 0.0;
13680}
13681
13682/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013683 * "log10()" function
13684 */
13685 static void
13686f_log10(argvars, rettv)
13687 typval_T *argvars;
13688 typval_T *rettv;
13689{
13690 float_T f;
13691
13692 rettv->v_type = VAR_FLOAT;
13693 if (get_float_arg(argvars, &f) == OK)
13694 rettv->vval.v_float = log10(f);
13695 else
13696 rettv->vval.v_float = 0.0;
13697}
13698#endif
13699
Bram Moolenaar1dced572012-04-05 16:54:08 +020013700#ifdef FEAT_LUA
13701/*
13702 * "luaeval()" function
13703 */
13704 static void
13705f_luaeval(argvars, rettv)
13706 typval_T *argvars;
13707 typval_T *rettv;
13708{
13709 char_u *str;
13710 char_u buf[NUMBUFLEN];
13711
13712 str = get_tv_string_buf(&argvars[0], buf);
13713 do_luaeval(str, argvars + 1, rettv);
13714}
13715#endif
13716
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013718 * "map()" function
13719 */
13720 static void
13721f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013722 typval_T *argvars;
13723 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013724{
13725 filter_map(argvars, rettv, TRUE);
13726}
13727
13728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013729 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 */
13731 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013732f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013733 typval_T *argvars;
13734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013736 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737}
13738
13739/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013740 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 */
13742 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013743f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013744 typval_T *argvars;
13745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013747 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748}
13749
Bram Moolenaar33570922005-01-25 22:26:29 +000013750static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751
13752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 typval_T *argvars;
13755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 int type;
13757{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013758 char_u *str = NULL;
13759 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 char_u *pat;
13761 regmatch_T regmatch;
13762 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013763 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 char_u *save_cpo;
13765 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013766 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013767 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013768 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 list_T *l = NULL;
13770 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013771 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013772 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773
13774 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13775 save_cpo = p_cpo;
13776 p_cpo = (char_u *)"";
13777
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013778 rettv->vval.v_number = -1;
13779 if (type == 3)
13780 {
13781 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013782 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013783 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013784 }
13785 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013787 rettv->v_type = VAR_STRING;
13788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013791 if (argvars[0].v_type == VAR_LIST)
13792 {
13793 if ((l = argvars[0].vval.v_list) == NULL)
13794 goto theend;
13795 li = l->lv_first;
13796 }
13797 else
13798 expr = str = get_tv_string(&argvars[0]);
13799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13801 if (pat == NULL)
13802 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806 int error = FALSE;
13807
13808 start = get_tv_number_chk(&argvars[2], &error);
13809 if (error)
13810 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013811 if (l != NULL)
13812 {
13813 li = list_find(l, start);
13814 if (li == NULL)
13815 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013816 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013817 }
13818 else
13819 {
13820 if (start < 0)
13821 start = 0;
13822 if (start > (long)STRLEN(str))
13823 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013824 /* When "count" argument is there ignore matches before "start",
13825 * otherwise skip part of the string. Differs when pattern is "^"
13826 * or "\<". */
13827 if (argvars[3].v_type != VAR_UNKNOWN)
13828 startcol = start;
13829 else
13830 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013831 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013832
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013833 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013834 nth = get_tv_number_chk(&argvars[3], &error);
13835 if (error)
13836 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 }
13838
13839 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13840 if (regmatch.regprog != NULL)
13841 {
13842 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013843
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013844 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013845 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013846 if (l != NULL)
13847 {
13848 if (li == NULL)
13849 {
13850 match = FALSE;
13851 break;
13852 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013853 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013854 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013855 if (str == NULL)
13856 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013857 }
13858
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013859 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013860
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013861 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013862 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013863 if (l == NULL && !match)
13864 break;
13865
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013866 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013867 if (l != NULL)
13868 {
13869 li = li->li_next;
13870 ++idx;
13871 }
13872 else
13873 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013874#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013875 startcol = (colnr_T)(regmatch.startp[0]
13876 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013877#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013878 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013879#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013880 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013881 }
13882
13883 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013885 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013886 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013887 int i;
13888
13889 /* return list with matched string and submatches */
13890 for (i = 0; i < NSUBEXP; ++i)
13891 {
13892 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013893 {
13894 if (list_append_string(rettv->vval.v_list,
13895 (char_u *)"", 0) == FAIL)
13896 break;
13897 }
13898 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013899 regmatch.startp[i],
13900 (int)(regmatch.endp[i] - regmatch.startp[i]))
13901 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013902 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013903 }
13904 }
13905 else if (type == 2)
13906 {
13907 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013908 if (l != NULL)
13909 copy_tv(&li->li_tv, rettv);
13910 else
13911 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013913 }
13914 else if (l != NULL)
13915 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 else
13917 {
13918 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013919 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 (varnumber_T)(regmatch.startp[0] - str);
13921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013924 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 }
13926 }
13927 vim_free(regmatch.regprog);
13928 }
13929
13930theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013931 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 p_cpo = save_cpo;
13933}
13934
13935/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013936 * "match()" function
13937 */
13938 static void
13939f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013940 typval_T *argvars;
13941 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013942{
13943 find_some_match(argvars, rettv, 1);
13944}
13945
13946/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013947 * "matchadd()" function
13948 */
13949 static void
13950f_matchadd(argvars, rettv)
13951 typval_T *argvars;
13952 typval_T *rettv;
13953{
13954#ifdef FEAT_SEARCH_EXTRA
13955 char_u buf[NUMBUFLEN];
13956 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13957 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13958 int prio = 10; /* default priority */
13959 int id = -1;
13960 int error = FALSE;
13961
13962 rettv->vval.v_number = -1;
13963
13964 if (grp == NULL || pat == NULL)
13965 return;
13966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013967 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013968 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013969 if (argvars[3].v_type != VAR_UNKNOWN)
13970 id = get_tv_number_chk(&argvars[3], &error);
13971 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013972 if (error == TRUE)
13973 return;
13974 if (id >= 1 && id <= 3)
13975 {
13976 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13977 return;
13978 }
13979
13980 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13981#endif
13982}
13983
13984/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013985 * "matcharg()" function
13986 */
13987 static void
13988f_matcharg(argvars, rettv)
13989 typval_T *argvars;
13990 typval_T *rettv;
13991{
13992 if (rettv_list_alloc(rettv) == OK)
13993 {
13994#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013995 int id = get_tv_number(&argvars[0]);
13996 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013997
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013998 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013999 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014000 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14001 {
14002 list_append_string(rettv->vval.v_list,
14003 syn_id2name(m->hlg_id), -1);
14004 list_append_string(rettv->vval.v_list, m->pattern, -1);
14005 }
14006 else
14007 {
14008 list_append_string(rettv->vval.v_list, NUL, -1);
14009 list_append_string(rettv->vval.v_list, NUL, -1);
14010 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014011 }
14012#endif
14013 }
14014}
14015
14016/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014017 * "matchdelete()" function
14018 */
14019 static void
14020f_matchdelete(argvars, rettv)
14021 typval_T *argvars;
14022 typval_T *rettv;
14023{
14024#ifdef FEAT_SEARCH_EXTRA
14025 rettv->vval.v_number = match_delete(curwin,
14026 (int)get_tv_number(&argvars[0]), TRUE);
14027#endif
14028}
14029
14030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014031 * "matchend()" function
14032 */
14033 static void
14034f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014035 typval_T *argvars;
14036 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037{
14038 find_some_match(argvars, rettv, 0);
14039}
14040
14041/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014042 * "matchlist()" function
14043 */
14044 static void
14045f_matchlist(argvars, rettv)
14046 typval_T *argvars;
14047 typval_T *rettv;
14048{
14049 find_some_match(argvars, rettv, 3);
14050}
14051
14052/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014053 * "matchstr()" function
14054 */
14055 static void
14056f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014057 typval_T *argvars;
14058 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059{
14060 find_some_match(argvars, rettv, 2);
14061}
14062
Bram Moolenaar33570922005-01-25 22:26:29 +000014063static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014064
14065 static void
14066max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014067 typval_T *argvars;
14068 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014069 int domax;
14070{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014071 long n = 0;
14072 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014074
14075 if (argvars[0].v_type == VAR_LIST)
14076 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014077 list_T *l;
14078 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014079
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014080 l = argvars[0].vval.v_list;
14081 if (l != NULL)
14082 {
14083 li = l->lv_first;
14084 if (li != NULL)
14085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014087 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014088 {
14089 li = li->li_next;
14090 if (li == NULL)
14091 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014093 if (domax ? i > n : i < n)
14094 n = i;
14095 }
14096 }
14097 }
14098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014099 else if (argvars[0].v_type == VAR_DICT)
14100 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014102 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014103 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014104 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014105
14106 d = argvars[0].vval.v_dict;
14107 if (d != NULL)
14108 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014109 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014112 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014114 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014116 if (first)
14117 {
14118 n = i;
14119 first = FALSE;
14120 }
14121 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014122 n = i;
14123 }
14124 }
14125 }
14126 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014127 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014128 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014129 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014130}
14131
14132/*
14133 * "max()" function
14134 */
14135 static void
14136f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014137 typval_T *argvars;
14138 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014139{
14140 max_min(argvars, rettv, TRUE);
14141}
14142
14143/*
14144 * "min()" function
14145 */
14146 static void
14147f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014148 typval_T *argvars;
14149 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014150{
14151 max_min(argvars, rettv, FALSE);
14152}
14153
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014154static int mkdir_recurse __ARGS((char_u *dir, int prot));
14155
14156/*
14157 * Create the directory in which "dir" is located, and higher levels when
14158 * needed.
14159 */
14160 static int
14161mkdir_recurse(dir, prot)
14162 char_u *dir;
14163 int prot;
14164{
14165 char_u *p;
14166 char_u *updir;
14167 int r = FAIL;
14168
14169 /* Get end of directory name in "dir".
14170 * We're done when it's "/" or "c:/". */
14171 p = gettail_sep(dir);
14172 if (p <= get_past_head(dir))
14173 return OK;
14174
14175 /* If the directory exists we're done. Otherwise: create it.*/
14176 updir = vim_strnsave(dir, (int)(p - dir));
14177 if (updir == NULL)
14178 return FAIL;
14179 if (mch_isdir(updir))
14180 r = OK;
14181 else if (mkdir_recurse(updir, prot) == OK)
14182 r = vim_mkdir_emsg(updir, prot);
14183 vim_free(updir);
14184 return r;
14185}
14186
14187#ifdef vim_mkdir
14188/*
14189 * "mkdir()" function
14190 */
14191 static void
14192f_mkdir(argvars, rettv)
14193 typval_T *argvars;
14194 typval_T *rettv;
14195{
14196 char_u *dir;
14197 char_u buf[NUMBUFLEN];
14198 int prot = 0755;
14199
14200 rettv->vval.v_number = FAIL;
14201 if (check_restricted() || check_secure())
14202 return;
14203
14204 dir = get_tv_string_buf(&argvars[0], buf);
14205 if (argvars[1].v_type != VAR_UNKNOWN)
14206 {
14207 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 prot = get_tv_number_chk(&argvars[2], NULL);
14209 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014210 mkdir_recurse(dir, prot);
14211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014213}
14214#endif
14215
Bram Moolenaar0d660222005-01-07 21:51:51 +000014216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 * "mode()" function
14218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014220f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014221 typval_T *argvars;
14222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014224 char_u buf[3];
14225
14226 buf[1] = NUL;
14227 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228
14229#ifdef FEAT_VISUAL
14230 if (VIsual_active)
14231 {
14232 if (VIsual_select)
14233 buf[0] = VIsual_mode + 's' - 'v';
14234 else
14235 buf[0] = VIsual_mode;
14236 }
14237 else
14238#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014239 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14240 || State == CONFIRM)
14241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014243 if (State == ASKMORE)
14244 buf[1] = 'm';
14245 else if (State == CONFIRM)
14246 buf[1] = '?';
14247 }
14248 else if (State == EXTERNCMD)
14249 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250 else if (State & INSERT)
14251 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014252#ifdef FEAT_VREPLACE
14253 if (State & VREPLACE_FLAG)
14254 {
14255 buf[0] = 'R';
14256 buf[1] = 'v';
14257 }
14258 else
14259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 if (State & REPLACE_FLAG)
14261 buf[0] = 'R';
14262 else
14263 buf[0] = 'i';
14264 }
14265 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014268 if (exmode_active)
14269 buf[1] = 'v';
14270 }
14271 else if (exmode_active)
14272 {
14273 buf[0] = 'c';
14274 buf[1] = 'e';
14275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014279 if (finish_op)
14280 buf[1] = 'o';
14281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014283 /* Clear out the minor mode when the argument is not a non-zero number or
14284 * non-empty string. */
14285 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014286 buf[1] = NUL;
14287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 rettv->vval.v_string = vim_strsave(buf);
14289 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290}
14291
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014292#ifdef FEAT_MZSCHEME
14293/*
14294 * "mzeval()" function
14295 */
14296 static void
14297f_mzeval(argvars, rettv)
14298 typval_T *argvars;
14299 typval_T *rettv;
14300{
14301 char_u *str;
14302 char_u buf[NUMBUFLEN];
14303
14304 str = get_tv_string_buf(&argvars[0], buf);
14305 do_mzeval(str, rettv);
14306}
14307#endif
14308
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014310 * "nextnonblank()" function
14311 */
14312 static void
14313f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014314 typval_T *argvars;
14315 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316{
14317 linenr_T lnum;
14318
14319 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014322 {
14323 lnum = 0;
14324 break;
14325 }
14326 if (*skipwhite(ml_get(lnum)) != NUL)
14327 break;
14328 }
14329 rettv->vval.v_number = lnum;
14330}
14331
14332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 * "nr2char()" function
14334 */
14335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014337 typval_T *argvars;
14338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339{
14340 char_u buf[NUMBUFLEN];
14341
14342#ifdef FEAT_MBYTE
14343 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014344 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 else
14346#endif
14347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 buf[1] = NUL;
14350 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 rettv->v_type = VAR_STRING;
14352 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014353}
14354
14355/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014356 * "or(expr, expr)" function
14357 */
14358 static void
14359f_or(argvars, rettv)
14360 typval_T *argvars;
14361 typval_T *rettv;
14362{
14363 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14364 | get_tv_number_chk(&argvars[1], NULL);
14365}
14366
14367/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014368 * "pathshorten()" function
14369 */
14370 static void
14371f_pathshorten(argvars, rettv)
14372 typval_T *argvars;
14373 typval_T *rettv;
14374{
14375 char_u *p;
14376
14377 rettv->v_type = VAR_STRING;
14378 p = get_tv_string_chk(&argvars[0]);
14379 if (p == NULL)
14380 rettv->vval.v_string = NULL;
14381 else
14382 {
14383 p = vim_strsave(p);
14384 rettv->vval.v_string = p;
14385 if (p != NULL)
14386 shorten_dir(p);
14387 }
14388}
14389
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014390#ifdef FEAT_FLOAT
14391/*
14392 * "pow()" function
14393 */
14394 static void
14395f_pow(argvars, rettv)
14396 typval_T *argvars;
14397 typval_T *rettv;
14398{
14399 float_T fx, fy;
14400
14401 rettv->v_type = VAR_FLOAT;
14402 if (get_float_arg(argvars, &fx) == OK
14403 && get_float_arg(&argvars[1], &fy) == OK)
14404 rettv->vval.v_float = pow(fx, fy);
14405 else
14406 rettv->vval.v_float = 0.0;
14407}
14408#endif
14409
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014410/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014411 * "prevnonblank()" function
14412 */
14413 static void
14414f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014415 typval_T *argvars;
14416 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014417{
14418 linenr_T lnum;
14419
14420 lnum = get_tv_lnum(argvars);
14421 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14422 lnum = 0;
14423 else
14424 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14425 --lnum;
14426 rettv->vval.v_number = lnum;
14427}
14428
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014429#ifdef HAVE_STDARG_H
14430/* This dummy va_list is here because:
14431 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14432 * - locally in the function results in a "used before set" warning
14433 * - using va_start() to initialize it gives "function with fixed args" error */
14434static va_list ap;
14435#endif
14436
Bram Moolenaar8c711452005-01-14 21:53:12 +000014437/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014438 * "printf()" function
14439 */
14440 static void
14441f_printf(argvars, rettv)
14442 typval_T *argvars;
14443 typval_T *rettv;
14444{
14445 rettv->v_type = VAR_STRING;
14446 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014447#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014448 {
14449 char_u buf[NUMBUFLEN];
14450 int len;
14451 char_u *s;
14452 int saved_did_emsg = did_emsg;
14453 char *fmt;
14454
14455 /* Get the required length, allocate the buffer and do it for real. */
14456 did_emsg = FALSE;
14457 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014458 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014459 if (!did_emsg)
14460 {
14461 s = alloc(len + 1);
14462 if (s != NULL)
14463 {
14464 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014465 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014466 }
14467 }
14468 did_emsg |= saved_did_emsg;
14469 }
14470#endif
14471}
14472
14473/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014474 * "pumvisible()" function
14475 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014476 static void
14477f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014478 typval_T *argvars UNUSED;
14479 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014480{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014481#ifdef FEAT_INS_EXPAND
14482 if (pum_visible())
14483 rettv->vval.v_number = 1;
14484#endif
14485}
14486
Bram Moolenaardb913952012-06-29 12:54:53 +020014487#ifdef FEAT_PYTHON3
14488/*
14489 * "py3eval()" function
14490 */
14491 static void
14492f_py3eval(argvars, rettv)
14493 typval_T *argvars;
14494 typval_T *rettv;
14495{
14496 char_u *str;
14497 char_u buf[NUMBUFLEN];
14498
14499 str = get_tv_string_buf(&argvars[0], buf);
14500 do_py3eval(str, rettv);
14501}
14502#endif
14503
14504#ifdef FEAT_PYTHON
14505/*
14506 * "pyeval()" function
14507 */
14508 static void
14509f_pyeval(argvars, rettv)
14510 typval_T *argvars;
14511 typval_T *rettv;
14512{
14513 char_u *str;
14514 char_u buf[NUMBUFLEN];
14515
14516 str = get_tv_string_buf(&argvars[0], buf);
14517 do_pyeval(str, rettv);
14518}
14519#endif
14520
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014521/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014522 * "range()" function
14523 */
14524 static void
14525f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014526 typval_T *argvars;
14527 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014528{
14529 long start;
14530 long end;
14531 long stride = 1;
14532 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014536 if (argvars[1].v_type == VAR_UNKNOWN)
14537 {
14538 end = start - 1;
14539 start = 0;
14540 }
14541 else
14542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014543 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014544 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014545 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014546 }
14547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 if (error)
14549 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014550 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014551 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014552 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014553 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014554 else
14555 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014556 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014557 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014558 if (list_append_number(rettv->vval.v_list,
14559 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014560 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014561 }
14562}
14563
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014564/*
14565 * "readfile()" function
14566 */
14567 static void
14568f_readfile(argvars, rettv)
14569 typval_T *argvars;
14570 typval_T *rettv;
14571{
14572 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014573 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014574 char_u *fname;
14575 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014576 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14577 int io_size = sizeof(buf);
14578 int readlen; /* size of last fread() */
14579 char_u *prev = NULL; /* previously read bytes, if any */
14580 long prevlen = 0; /* length of data in prev */
14581 long prevsize = 0; /* size of prev buffer */
14582 long maxline = MAXLNUM;
14583 long cnt = 0;
14584 char_u *p; /* position in buf */
14585 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014586
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014587 if (argvars[1].v_type != VAR_UNKNOWN)
14588 {
14589 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14590 binary = TRUE;
14591 if (argvars[2].v_type != VAR_UNKNOWN)
14592 maxline = get_tv_number(&argvars[2]);
14593 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014594
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014595 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014596 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014597
14598 /* Always open the file in binary mode, library functions have a mind of
14599 * their own about CR-LF conversion. */
14600 fname = get_tv_string(&argvars[0]);
14601 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14602 {
14603 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14604 return;
14605 }
14606
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014607 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014608 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014609 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014610
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014611 /* This for loop processes what was read, but is also entered at end
14612 * of file so that either:
14613 * - an incomplete line gets written
14614 * - a "binary" file gets an empty line at the end if it ends in a
14615 * newline. */
14616 for (p = buf, start = buf;
14617 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14618 ++p)
14619 {
14620 if (*p == '\n' || readlen <= 0)
14621 {
14622 listitem_T *li;
14623 char_u *s = NULL;
14624 long_u len = p - start;
14625
14626 /* Finished a line. Remove CRs before NL. */
14627 if (readlen > 0 && !binary)
14628 {
14629 while (len > 0 && start[len - 1] == '\r')
14630 --len;
14631 /* removal may cross back to the "prev" string */
14632 if (len == 0)
14633 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14634 --prevlen;
14635 }
14636 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014637 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014638 else
14639 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014640 /* Change "prev" buffer to be the right size. This way
14641 * the bytes are only copied once, and very long lines are
14642 * allocated only once. */
14643 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014644 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014645 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014646 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014647 prev = NULL; /* the list will own the string */
14648 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014649 }
14650 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014651 if (s == NULL)
14652 {
14653 do_outofmem_msg((long_u) prevlen + len + 1);
14654 failed = TRUE;
14655 break;
14656 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014657
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014658 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014659 {
14660 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014661 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014662 break;
14663 }
14664 li->li_tv.v_type = VAR_STRING;
14665 li->li_tv.v_lock = 0;
14666 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014667 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014668
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014669 start = p + 1; /* step over newline */
14670 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014671 break;
14672 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014673 else if (*p == NUL)
14674 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014675#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014676 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14677 * when finding the BF and check the previous two bytes. */
14678 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014679 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014680 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14681 * + 1, these may be in the "prev" string. */
14682 char_u back1 = p >= buf + 1 ? p[-1]
14683 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14684 char_u back2 = p >= buf + 2 ? p[-2]
14685 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14686 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014687
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014688 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014689 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014690 char_u *dest = p - 2;
14691
14692 /* Usually a BOM is at the beginning of a file, and so at
14693 * the beginning of a line; then we can just step over it.
14694 */
14695 if (start == dest)
14696 start = p + 1;
14697 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014698 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014699 /* have to shuffle buf to close gap */
14700 int adjust_prevlen = 0;
14701
14702 if (dest < buf)
14703 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014704 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014705 dest = buf;
14706 }
14707 if (readlen > p - buf + 1)
14708 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14709 readlen -= 3 - adjust_prevlen;
14710 prevlen -= adjust_prevlen;
14711 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014712 }
14713 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014714 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014715#endif
14716 } /* for */
14717
14718 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14719 break;
14720 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014721 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014722 /* There's part of a line in buf, store it in "prev". */
14723 if (p - start + prevlen >= prevsize)
14724 {
14725 /* need bigger "prev" buffer */
14726 char_u *newprev;
14727
14728 /* A common use case is ordinary text files and "prev" gets a
14729 * fragment of a line, so the first allocation is made
14730 * small, to avoid repeatedly 'allocing' large and
14731 * 'reallocing' small. */
14732 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014733 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014734 else
14735 {
14736 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014737 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014738 prevsize = grow50pc > growmin ? grow50pc : growmin;
14739 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014740 newprev = prev == NULL ? alloc(prevsize)
14741 : vim_realloc(prev, prevsize);
14742 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014743 {
14744 do_outofmem_msg((long_u)prevsize);
14745 failed = TRUE;
14746 break;
14747 }
14748 prev = newprev;
14749 }
14750 /* Add the line part to end of "prev". */
14751 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014752 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014753 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014754 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014755
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014756 /*
14757 * For a negative line count use only the lines at the end of the file,
14758 * free the rest.
14759 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014760 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014761 while (cnt > -maxline)
14762 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014763 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014764 --cnt;
14765 }
14766
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014767 if (failed)
14768 {
14769 list_free(rettv->vval.v_list, TRUE);
14770 /* readfile doc says an empty list is returned on error */
14771 rettv->vval.v_list = list_alloc();
14772 }
14773
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014774 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014775 fclose(fd);
14776}
14777
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014778#if defined(FEAT_RELTIME)
14779static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14780
14781/*
14782 * Convert a List to proftime_T.
14783 * Return FAIL when there is something wrong.
14784 */
14785 static int
14786list2proftime(arg, tm)
14787 typval_T *arg;
14788 proftime_T *tm;
14789{
14790 long n1, n2;
14791 int error = FALSE;
14792
14793 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14794 || arg->vval.v_list->lv_len != 2)
14795 return FAIL;
14796 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14797 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14798# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014799 tm->HighPart = n1;
14800 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014801# else
14802 tm->tv_sec = n1;
14803 tm->tv_usec = n2;
14804# endif
14805 return error ? FAIL : OK;
14806}
14807#endif /* FEAT_RELTIME */
14808
14809/*
14810 * "reltime()" function
14811 */
14812 static void
14813f_reltime(argvars, rettv)
14814 typval_T *argvars;
14815 typval_T *rettv;
14816{
14817#ifdef FEAT_RELTIME
14818 proftime_T res;
14819 proftime_T start;
14820
14821 if (argvars[0].v_type == VAR_UNKNOWN)
14822 {
14823 /* No arguments: get current time. */
14824 profile_start(&res);
14825 }
14826 else if (argvars[1].v_type == VAR_UNKNOWN)
14827 {
14828 if (list2proftime(&argvars[0], &res) == FAIL)
14829 return;
14830 profile_end(&res);
14831 }
14832 else
14833 {
14834 /* Two arguments: compute the difference. */
14835 if (list2proftime(&argvars[0], &start) == FAIL
14836 || list2proftime(&argvars[1], &res) == FAIL)
14837 return;
14838 profile_sub(&res, &start);
14839 }
14840
14841 if (rettv_list_alloc(rettv) == OK)
14842 {
14843 long n1, n2;
14844
14845# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014846 n1 = res.HighPart;
14847 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014848# else
14849 n1 = res.tv_sec;
14850 n2 = res.tv_usec;
14851# endif
14852 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14853 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14854 }
14855#endif
14856}
14857
14858/*
14859 * "reltimestr()" function
14860 */
14861 static void
14862f_reltimestr(argvars, rettv)
14863 typval_T *argvars;
14864 typval_T *rettv;
14865{
14866#ifdef FEAT_RELTIME
14867 proftime_T tm;
14868#endif
14869
14870 rettv->v_type = VAR_STRING;
14871 rettv->vval.v_string = NULL;
14872#ifdef FEAT_RELTIME
14873 if (list2proftime(&argvars[0], &tm) == OK)
14874 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14875#endif
14876}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014877
Bram Moolenaar0d660222005-01-07 21:51:51 +000014878#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14879static void make_connection __ARGS((void));
14880static int check_connection __ARGS((void));
14881
14882 static void
14883make_connection()
14884{
14885 if (X_DISPLAY == NULL
14886# ifdef FEAT_GUI
14887 && !gui.in_use
14888# endif
14889 )
14890 {
14891 x_force_connect = TRUE;
14892 setup_term_clip();
14893 x_force_connect = FALSE;
14894 }
14895}
14896
14897 static int
14898check_connection()
14899{
14900 make_connection();
14901 if (X_DISPLAY == NULL)
14902 {
14903 EMSG(_("E240: No connection to Vim server"));
14904 return FAIL;
14905 }
14906 return OK;
14907}
14908#endif
14909
14910#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014911static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014912
14913 static void
14914remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014915 typval_T *argvars;
14916 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014917 int expr;
14918{
14919 char_u *server_name;
14920 char_u *keys;
14921 char_u *r = NULL;
14922 char_u buf[NUMBUFLEN];
14923# ifdef WIN32
14924 HWND w;
14925# else
14926 Window w;
14927# endif
14928
14929 if (check_restricted() || check_secure())
14930 return;
14931
14932# ifdef FEAT_X11
14933 if (check_connection() == FAIL)
14934 return;
14935# endif
14936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014937 server_name = get_tv_string_chk(&argvars[0]);
14938 if (server_name == NULL)
14939 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014940 keys = get_tv_string_buf(&argvars[1], buf);
14941# ifdef WIN32
14942 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14943# else
14944 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14945 < 0)
14946# endif
14947 {
14948 if (r != NULL)
14949 EMSG(r); /* sending worked but evaluation failed */
14950 else
14951 EMSG2(_("E241: Unable to send to %s"), server_name);
14952 return;
14953 }
14954
14955 rettv->vval.v_string = r;
14956
14957 if (argvars[2].v_type != VAR_UNKNOWN)
14958 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014959 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014960 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014961 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014963 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 v.di_tv.v_type = VAR_STRING;
14965 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014966 idvar = get_tv_string_chk(&argvars[2]);
14967 if (idvar != NULL)
14968 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014969 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014970 }
14971}
14972#endif
14973
14974/*
14975 * "remote_expr()" function
14976 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 static void
14978f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014979 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014980 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014981{
14982 rettv->v_type = VAR_STRING;
14983 rettv->vval.v_string = NULL;
14984#ifdef FEAT_CLIENTSERVER
14985 remote_common(argvars, rettv, TRUE);
14986#endif
14987}
14988
14989/*
14990 * "remote_foreground()" function
14991 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992 static void
14993f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014994 typval_T *argvars UNUSED;
14995 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997#ifdef FEAT_CLIENTSERVER
14998# ifdef WIN32
14999 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015000 {
15001 char_u *server_name = get_tv_string_chk(&argvars[0]);
15002
15003 if (server_name != NULL)
15004 serverForeground(server_name);
15005 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006# else
15007 /* Send a foreground() expression to the server. */
15008 argvars[1].v_type = VAR_STRING;
15009 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15010 argvars[2].v_type = VAR_UNKNOWN;
15011 remote_common(argvars, rettv, TRUE);
15012 vim_free(argvars[1].vval.v_string);
15013# endif
15014#endif
15015}
15016
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017 static void
15018f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015019 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015020 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015021{
15022#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 char_u *s = NULL;
15025# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015026 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015027# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015028 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015029
15030 if (check_restricted() || check_secure())
15031 {
15032 rettv->vval.v_number = -1;
15033 return;
15034 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015035 serverid = get_tv_string_chk(&argvars[0]);
15036 if (serverid == NULL)
15037 {
15038 rettv->vval.v_number = -1;
15039 return; /* type error; errmsg already given */
15040 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015041# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015042 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043 if (n == 0)
15044 rettv->vval.v_number = -1;
15045 else
15046 {
15047 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15048 rettv->vval.v_number = (s != NULL);
15049 }
15050# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051 if (check_connection() == FAIL)
15052 return;
15053
15054 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015055 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056# endif
15057
15058 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015060 char_u *retvar;
15061
Bram Moolenaar33570922005-01-25 22:26:29 +000015062 v.di_tv.v_type = VAR_STRING;
15063 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015064 retvar = get_tv_string_chk(&argvars[1]);
15065 if (retvar != NULL)
15066 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015067 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 }
15069#else
15070 rettv->vval.v_number = -1;
15071#endif
15072}
15073
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074 static void
15075f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015076 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078{
15079 char_u *r = NULL;
15080
15081#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015082 char_u *serverid = get_tv_string_chk(&argvars[0]);
15083
15084 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 {
15086# ifdef WIN32
15087 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015088 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015090 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091 if (n != 0)
15092 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15093 if (r == NULL)
15094# else
15095 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015096 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097# endif
15098 EMSG(_("E277: Unable to read a server reply"));
15099 }
15100#endif
15101 rettv->v_type = VAR_STRING;
15102 rettv->vval.v_string = r;
15103}
15104
15105/*
15106 * "remote_send()" function
15107 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108 static void
15109f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015110 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112{
15113 rettv->v_type = VAR_STRING;
15114 rettv->vval.v_string = NULL;
15115#ifdef FEAT_CLIENTSERVER
15116 remote_common(argvars, rettv, FALSE);
15117#endif
15118}
15119
15120/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015121 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015122 */
15123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015125 typval_T *argvars;
15126 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015127{
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 list_T *l;
15129 listitem_T *item, *item2;
15130 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015131 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015132 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015133 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015134 dict_T *d;
15135 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015136 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015137
Bram Moolenaar8c711452005-01-14 21:53:12 +000015138 if (argvars[0].v_type == VAR_DICT)
15139 {
15140 if (argvars[2].v_type != VAR_UNKNOWN)
15141 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015142 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015143 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015145 key = get_tv_string_chk(&argvars[1]);
15146 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 di = dict_find(d, key, -1);
15149 if (di == NULL)
15150 EMSG2(_(e_dictkey), key);
15151 else
15152 {
15153 *rettv = di->di_tv;
15154 init_tv(&di->di_tv);
15155 dictitem_remove(d, di);
15156 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015157 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015158 }
15159 }
15160 else if (argvars[0].v_type != VAR_LIST)
15161 EMSG2(_(e_listdictarg), "remove()");
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 *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015165 int error = FALSE;
15166
15167 idx = get_tv_number_chk(&argvars[1], &error);
15168 if (error)
15169 ; /* type error: do nothing, errmsg already given */
15170 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015171 EMSGN(_(e_listidx), idx);
15172 else
15173 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015174 if (argvars[2].v_type == VAR_UNKNOWN)
15175 {
15176 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015177 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015178 *rettv = item->li_tv;
15179 vim_free(item);
15180 }
15181 else
15182 {
15183 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015184 end = get_tv_number_chk(&argvars[2], &error);
15185 if (error)
15186 ; /* type error: do nothing */
15187 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015188 EMSGN(_(e_listidx), end);
15189 else
15190 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015191 int cnt = 0;
15192
15193 for (li = item; li != NULL; li = li->li_next)
15194 {
15195 ++cnt;
15196 if (li == item2)
15197 break;
15198 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015199 if (li == NULL) /* didn't find "item2" after "item" */
15200 EMSG(_(e_invrange));
15201 else
15202 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015203 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015204 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015205 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015206 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015207 l->lv_first = item;
15208 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015209 item->li_prev = NULL;
15210 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015211 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015212 }
15213 }
15214 }
15215 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015216 }
15217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218}
15219
15220/*
15221 * "rename({from}, {to})" function
15222 */
15223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015224f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015225 typval_T *argvars;
15226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227{
15228 char_u buf[NUMBUFLEN];
15229
15230 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015233 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15234 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235}
15236
15237/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015238 * "repeat()" function
15239 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015240 static void
15241f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015242 typval_T *argvars;
15243 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015244{
15245 char_u *p;
15246 int n;
15247 int slen;
15248 int len;
15249 char_u *r;
15250 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015251
15252 n = get_tv_number(&argvars[1]);
15253 if (argvars[0].v_type == VAR_LIST)
15254 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015255 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015256 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015257 if (list_extend(rettv->vval.v_list,
15258 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015259 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015260 }
15261 else
15262 {
15263 p = get_tv_string(&argvars[0]);
15264 rettv->v_type = VAR_STRING;
15265 rettv->vval.v_string = NULL;
15266
15267 slen = (int)STRLEN(p);
15268 len = slen * n;
15269 if (len <= 0)
15270 return;
15271
15272 r = alloc(len + 1);
15273 if (r != NULL)
15274 {
15275 for (i = 0; i < n; i++)
15276 mch_memmove(r + i * slen, p, (size_t)slen);
15277 r[len] = NUL;
15278 }
15279
15280 rettv->vval.v_string = r;
15281 }
15282}
15283
15284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285 * "resolve()" function
15286 */
15287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015289 typval_T *argvars;
15290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291{
15292 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015293#ifdef HAVE_READLINK
15294 char_u *buf = NULL;
15295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015297 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298#ifdef FEAT_SHORTCUT
15299 {
15300 char_u *v = NULL;
15301
15302 v = mch_resolve_shortcut(p);
15303 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307 }
15308#else
15309# ifdef HAVE_READLINK
15310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311 char_u *cpy;
15312 int len;
15313 char_u *remain = NULL;
15314 char_u *q;
15315 int is_relative_to_current = FALSE;
15316 int has_trailing_pathsep = FALSE;
15317 int limit = 100;
15318
15319 p = vim_strsave(p);
15320
15321 if (p[0] == '.' && (vim_ispathsep(p[1])
15322 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15323 is_relative_to_current = TRUE;
15324
15325 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015326 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015329 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331
15332 q = getnextcomp(p);
15333 if (*q != NUL)
15334 {
15335 /* Separate the first path component in "p", and keep the
15336 * remainder (beginning with the path separator). */
15337 remain = vim_strsave(q - 1);
15338 q[-1] = NUL;
15339 }
15340
Bram Moolenaard9462e32011-04-11 21:35:11 +020015341 buf = alloc(MAXPATHL + 1);
15342 if (buf == NULL)
15343 goto fail;
15344
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 for (;;)
15346 {
15347 for (;;)
15348 {
15349 len = readlink((char *)p, (char *)buf, MAXPATHL);
15350 if (len <= 0)
15351 break;
15352 buf[len] = NUL;
15353
15354 if (limit-- == 0)
15355 {
15356 vim_free(p);
15357 vim_free(remain);
15358 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 goto fail;
15361 }
15362
15363 /* Ensure that the result will have a trailing path separator
15364 * if the argument has one. */
15365 if (remain == NULL && has_trailing_pathsep)
15366 add_pathsep(buf);
15367
15368 /* Separate the first path component in the link value and
15369 * concatenate the remainders. */
15370 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15371 if (*q != NUL)
15372 {
15373 if (remain == NULL)
15374 remain = vim_strsave(q - 1);
15375 else
15376 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015377 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 if (cpy != NULL)
15379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 vim_free(remain);
15381 remain = cpy;
15382 }
15383 }
15384 q[-1] = NUL;
15385 }
15386
15387 q = gettail(p);
15388 if (q > p && *q == NUL)
15389 {
15390 /* Ignore trailing path separator. */
15391 q[-1] = NUL;
15392 q = gettail(p);
15393 }
15394 if (q > p && !mch_isFullName(buf))
15395 {
15396 /* symlink is relative to directory of argument */
15397 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15398 if (cpy != NULL)
15399 {
15400 STRCPY(cpy, p);
15401 STRCPY(gettail(cpy), buf);
15402 vim_free(p);
15403 p = cpy;
15404 }
15405 }
15406 else
15407 {
15408 vim_free(p);
15409 p = vim_strsave(buf);
15410 }
15411 }
15412
15413 if (remain == NULL)
15414 break;
15415
15416 /* Append the first path component of "remain" to "p". */
15417 q = getnextcomp(remain + 1);
15418 len = q - remain - (*q != NUL);
15419 cpy = vim_strnsave(p, STRLEN(p) + len);
15420 if (cpy != NULL)
15421 {
15422 STRNCAT(cpy, remain, len);
15423 vim_free(p);
15424 p = cpy;
15425 }
15426 /* Shorten "remain". */
15427 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015428 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 else
15430 {
15431 vim_free(remain);
15432 remain = NULL;
15433 }
15434 }
15435
15436 /* If the result is a relative path name, make it explicitly relative to
15437 * the current directory if and only if the argument had this form. */
15438 if (!vim_ispathsep(*p))
15439 {
15440 if (is_relative_to_current
15441 && *p != NUL
15442 && !(p[0] == '.'
15443 && (p[1] == NUL
15444 || vim_ispathsep(p[1])
15445 || (p[1] == '.'
15446 && (p[2] == NUL
15447 || vim_ispathsep(p[2]))))))
15448 {
15449 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015450 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 if (cpy != NULL)
15452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 vim_free(p);
15454 p = cpy;
15455 }
15456 }
15457 else if (!is_relative_to_current)
15458 {
15459 /* Strip leading "./". */
15460 q = p;
15461 while (q[0] == '.' && vim_ispathsep(q[1]))
15462 q += 2;
15463 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015464 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 }
15466 }
15467
15468 /* Ensure that the result will have no trailing path separator
15469 * if the argument had none. But keep "/" or "//". */
15470 if (!has_trailing_pathsep)
15471 {
15472 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015473 if (after_pathsep(p, q))
15474 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 }
15476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478 }
15479# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015480 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481# endif
15482#endif
15483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485
15486#ifdef HAVE_READLINK
15487fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015488 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491}
15492
15493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015494 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 */
15496 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015497f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015498 typval_T *argvars;
15499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500{
Bram Moolenaar33570922005-01-25 22:26:29 +000015501 list_T *l;
15502 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503
Bram Moolenaar0d660222005-01-07 21:51:51 +000015504 if (argvars[0].v_type != VAR_LIST)
15505 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015506 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015507 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015508 {
15509 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015510 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015511 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015512 while (li != NULL)
15513 {
15514 ni = li->li_prev;
15515 list_append(l, li);
15516 li = ni;
15517 }
15518 rettv->vval.v_list = l;
15519 rettv->v_type = VAR_LIST;
15520 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015521 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523}
15524
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015525#define SP_NOMOVE 0x01 /* don't move cursor */
15526#define SP_REPEAT 0x02 /* repeat to find outer pair */
15527#define SP_RETCOUNT 0x04 /* return matchcount */
15528#define SP_SETPCMARK 0x08 /* set previous context mark */
15529#define SP_START 0x10 /* accept match at start position */
15530#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15531#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015532
Bram Moolenaar33570922005-01-25 22:26:29 +000015533static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015534
15535/*
15536 * Get flags for a search function.
15537 * Possibly sets "p_ws".
15538 * Returns BACKWARD, FORWARD or zero (for an error).
15539 */
15540 static int
15541get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015542 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015543 int *flagsp;
15544{
15545 int dir = FORWARD;
15546 char_u *flags;
15547 char_u nbuf[NUMBUFLEN];
15548 int mask;
15549
15550 if (varp->v_type != VAR_UNKNOWN)
15551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015552 flags = get_tv_string_buf_chk(varp, nbuf);
15553 if (flags == NULL)
15554 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015555 while (*flags != NUL)
15556 {
15557 switch (*flags)
15558 {
15559 case 'b': dir = BACKWARD; break;
15560 case 'w': p_ws = TRUE; break;
15561 case 'W': p_ws = FALSE; break;
15562 default: mask = 0;
15563 if (flagsp != NULL)
15564 switch (*flags)
15565 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015566 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015567 case 'e': mask = SP_END; break;
15568 case 'm': mask = SP_RETCOUNT; break;
15569 case 'n': mask = SP_NOMOVE; break;
15570 case 'p': mask = SP_SUBPAT; break;
15571 case 'r': mask = SP_REPEAT; break;
15572 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015573 }
15574 if (mask == 0)
15575 {
15576 EMSG2(_(e_invarg2), flags);
15577 dir = 0;
15578 }
15579 else
15580 *flagsp |= mask;
15581 }
15582 if (dir == 0)
15583 break;
15584 ++flags;
15585 }
15586 }
15587 return dir;
15588}
15589
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015591 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015593 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015594search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015595 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015596 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015597 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015599 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600 char_u *pat;
15601 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015602 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 int save_p_ws = p_ws;
15604 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015605 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015606 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015607 proftime_T tm;
15608#ifdef FEAT_RELTIME
15609 long time_limit = 0;
15610#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015611 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015612 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015614 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015615 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015616 if (dir == 0)
15617 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015618 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015619 if (flags & SP_START)
15620 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015621 if (flags & SP_END)
15622 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015623
Bram Moolenaar76929292008-01-06 19:07:36 +000015624 /* Optional arguments: line number to stop searching and timeout. */
15625 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015626 {
15627 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15628 if (lnum_stop < 0)
15629 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015630#ifdef FEAT_RELTIME
15631 if (argvars[3].v_type != VAR_UNKNOWN)
15632 {
15633 time_limit = get_tv_number_chk(&argvars[3], NULL);
15634 if (time_limit < 0)
15635 goto theend;
15636 }
15637#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015638 }
15639
Bram Moolenaar76929292008-01-06 19:07:36 +000015640#ifdef FEAT_RELTIME
15641 /* Set the time limit, if there is one. */
15642 profile_setlimit(time_limit, &tm);
15643#endif
15644
Bram Moolenaar231334e2005-07-25 20:46:57 +000015645 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015646 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015647 * Check to make sure only those flags are set.
15648 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15649 * flags cannot be set. Check for that condition also.
15650 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015651 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015652 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015654 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015655 goto theend;
15656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015658 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015659 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015660 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015661 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015663 if (flags & SP_SUBPAT)
15664 retval = subpatnum;
15665 else
15666 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015667 if (flags & SP_SETPCMARK)
15668 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015670 if (match_pos != NULL)
15671 {
15672 /* Store the match cursor position */
15673 match_pos->lnum = pos.lnum;
15674 match_pos->col = pos.col + 1;
15675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 /* "/$" will put the cursor after the end of the line, may need to
15677 * correct that here */
15678 check_cursor();
15679 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015680
15681 /* If 'n' flag is used: restore cursor position. */
15682 if (flags & SP_NOMOVE)
15683 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015684 else
15685 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015686theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015688
15689 return retval;
15690}
15691
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015692#ifdef FEAT_FLOAT
15693/*
15694 * "round({float})" function
15695 */
15696 static void
15697f_round(argvars, rettv)
15698 typval_T *argvars;
15699 typval_T *rettv;
15700{
15701 float_T f;
15702
15703 rettv->v_type = VAR_FLOAT;
15704 if (get_float_arg(argvars, &f) == OK)
15705 /* round() is not in C90, use ceil() or floor() instead. */
15706 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15707 else
15708 rettv->vval.v_float = 0.0;
15709}
15710#endif
15711
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015712/*
15713 * "search()" function
15714 */
15715 static void
15716f_search(argvars, rettv)
15717 typval_T *argvars;
15718 typval_T *rettv;
15719{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015720 int flags = 0;
15721
15722 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723}
15724
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015726 * "searchdecl()" function
15727 */
15728 static void
15729f_searchdecl(argvars, rettv)
15730 typval_T *argvars;
15731 typval_T *rettv;
15732{
15733 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015734 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015735 int error = FALSE;
15736 char_u *name;
15737
15738 rettv->vval.v_number = 1; /* default: FAIL */
15739
15740 name = get_tv_string_chk(&argvars[0]);
15741 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015742 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015743 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015744 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15745 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15746 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015747 if (!error && name != NULL)
15748 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015749 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015750}
15751
15752/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015753 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015755 static int
15756searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015757 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015758 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759{
15760 char_u *spat, *mpat, *epat;
15761 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 int dir;
15764 int flags = 0;
15765 char_u nbuf1[NUMBUFLEN];
15766 char_u nbuf2[NUMBUFLEN];
15767 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015768 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015769 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015770 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015773 spat = get_tv_string_chk(&argvars[0]);
15774 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15775 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15776 if (spat == NULL || mpat == NULL || epat == NULL)
15777 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 /* Handle the optional fourth argument: flags */
15780 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015781 if (dir == 0)
15782 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015783
15784 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015785 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15786 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015787 if ((flags & (SP_END | SP_SUBPAT)) != 0
15788 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015789 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015790 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015791 goto theend;
15792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015794 /* Using 'r' implies 'W', otherwise it doesn't work. */
15795 if (flags & SP_REPEAT)
15796 p_ws = FALSE;
15797
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015798 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015799 if (argvars[3].v_type == VAR_UNKNOWN
15800 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801 skip = (char_u *)"";
15802 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015804 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015805 if (argvars[5].v_type != VAR_UNKNOWN)
15806 {
15807 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15808 if (lnum_stop < 0)
15809 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015810#ifdef FEAT_RELTIME
15811 if (argvars[6].v_type != VAR_UNKNOWN)
15812 {
15813 time_limit = get_tv_number_chk(&argvars[6], NULL);
15814 if (time_limit < 0)
15815 goto theend;
15816 }
15817#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015818 }
15819 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015820 if (skip == NULL)
15821 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015823 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015824 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015825
15826theend:
15827 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015828
15829 return retval;
15830}
15831
15832/*
15833 * "searchpair()" function
15834 */
15835 static void
15836f_searchpair(argvars, rettv)
15837 typval_T *argvars;
15838 typval_T *rettv;
15839{
15840 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15841}
15842
15843/*
15844 * "searchpairpos()" function
15845 */
15846 static void
15847f_searchpairpos(argvars, rettv)
15848 typval_T *argvars;
15849 typval_T *rettv;
15850{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015851 pos_T match_pos;
15852 int lnum = 0;
15853 int col = 0;
15854
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015855 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015856 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015857
15858 if (searchpair_cmn(argvars, &match_pos) > 0)
15859 {
15860 lnum = match_pos.lnum;
15861 col = match_pos.col;
15862 }
15863
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015864 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15865 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015866}
15867
15868/*
15869 * Search for a start/middle/end thing.
15870 * Used by searchpair(), see its documentation for the details.
15871 * Returns 0 or -1 for no match,
15872 */
15873 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015874do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15875 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015876 char_u *spat; /* start pattern */
15877 char_u *mpat; /* middle pattern */
15878 char_u *epat; /* end pattern */
15879 int dir; /* BACKWARD or FORWARD */
15880 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015881 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015882 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015883 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015884 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015885{
15886 char_u *save_cpo;
15887 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15888 long retval = 0;
15889 pos_T pos;
15890 pos_T firstpos;
15891 pos_T foundpos;
15892 pos_T save_cursor;
15893 pos_T save_pos;
15894 int n;
15895 int r;
15896 int nest = 1;
15897 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015898 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015899 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015900
15901 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15902 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015903 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015904
Bram Moolenaar76929292008-01-06 19:07:36 +000015905#ifdef FEAT_RELTIME
15906 /* Set the time limit, if there is one. */
15907 profile_setlimit(time_limit, &tm);
15908#endif
15909
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015910 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15911 * start/middle/end (pat3, for the top pair). */
15912 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15913 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15914 if (pat2 == NULL || pat3 == NULL)
15915 goto theend;
15916 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15917 if (*mpat == NUL)
15918 STRCPY(pat3, pat2);
15919 else
15920 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15921 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015922 if (flags & SP_START)
15923 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015924
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 save_cursor = curwin->w_cursor;
15926 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015927 clearpos(&firstpos);
15928 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 pat = pat3;
15930 for (;;)
15931 {
15932 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015933 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15935 /* didn't find it or found the first match again: FAIL */
15936 break;
15937
15938 if (firstpos.lnum == 0)
15939 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015940 if (equalpos(pos, foundpos))
15941 {
15942 /* Found the same position again. Can happen with a pattern that
15943 * has "\zs" at the end and searching backwards. Advance one
15944 * character and try again. */
15945 if (dir == BACKWARD)
15946 decl(&pos);
15947 else
15948 incl(&pos);
15949 }
15950 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015952 /* clear the start flag to avoid getting stuck here */
15953 options &= ~SEARCH_START;
15954
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955 /* If the skip pattern matches, ignore this match. */
15956 if (*skip != NUL)
15957 {
15958 save_pos = curwin->w_cursor;
15959 curwin->w_cursor = pos;
15960 r = eval_to_bool(skip, &err, NULL, FALSE);
15961 curwin->w_cursor = save_pos;
15962 if (err)
15963 {
15964 /* Evaluating {skip} caused an error, break here. */
15965 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015966 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 break;
15968 }
15969 if (r)
15970 continue;
15971 }
15972
15973 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15974 {
15975 /* Found end when searching backwards or start when searching
15976 * forward: nested pair. */
15977 ++nest;
15978 pat = pat2; /* nested, don't search for middle */
15979 }
15980 else
15981 {
15982 /* Found end when searching forward or start when searching
15983 * backward: end of (nested) pair; or found middle in outer pair. */
15984 if (--nest == 1)
15985 pat = pat3; /* outer level, search for middle */
15986 }
15987
15988 if (nest == 0)
15989 {
15990 /* Found the match: return matchcount or line number. */
15991 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015992 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015994 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015995 if (flags & SP_SETPCMARK)
15996 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 curwin->w_cursor = pos;
15998 if (!(flags & SP_REPEAT))
15999 break;
16000 nest = 1; /* search for next unmatched */
16001 }
16002 }
16003
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016004 if (match_pos != NULL)
16005 {
16006 /* Store the match cursor position */
16007 match_pos->lnum = curwin->w_cursor.lnum;
16008 match_pos->col = curwin->w_cursor.col + 1;
16009 }
16010
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016012 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 curwin->w_cursor = save_cursor;
16014
16015theend:
16016 vim_free(pat2);
16017 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016018 if (p_cpo == empty_option)
16019 p_cpo = save_cpo;
16020 else
16021 /* Darn, evaluating the {skip} expression changed the value. */
16022 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016023
16024 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025}
16026
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016027/*
16028 * "searchpos()" function
16029 */
16030 static void
16031f_searchpos(argvars, rettv)
16032 typval_T *argvars;
16033 typval_T *rettv;
16034{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016035 pos_T match_pos;
16036 int lnum = 0;
16037 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016038 int n;
16039 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016040
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016041 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016042 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016043
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016044 n = search_cmn(argvars, &match_pos, &flags);
16045 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016046 {
16047 lnum = match_pos.lnum;
16048 col = match_pos.col;
16049 }
16050
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016051 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16052 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016053 if (flags & SP_SUBPAT)
16054 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016055}
16056
16057
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058 static void
16059f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016060 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016063#ifdef FEAT_CLIENTSERVER
16064 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016065 char_u *server = get_tv_string_chk(&argvars[0]);
16066 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067
Bram Moolenaar0d660222005-01-07 21:51:51 +000016068 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016069 if (server == NULL || reply == NULL)
16070 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016071 if (check_restricted() || check_secure())
16072 return;
16073# ifdef FEAT_X11
16074 if (check_connection() == FAIL)
16075 return;
16076# endif
16077
16078 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016080 EMSG(_("E258: Unable to send to client"));
16081 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016083 rettv->vval.v_number = 0;
16084#else
16085 rettv->vval.v_number = -1;
16086#endif
16087}
16088
Bram Moolenaar0d660222005-01-07 21:51:51 +000016089 static void
16090f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016091 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016092 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093{
16094 char_u *r = NULL;
16095
16096#ifdef FEAT_CLIENTSERVER
16097# ifdef WIN32
16098 r = serverGetVimNames();
16099# else
16100 make_connection();
16101 if (X_DISPLAY != NULL)
16102 r = serverGetVimNames(X_DISPLAY);
16103# endif
16104#endif
16105 rettv->v_type = VAR_STRING;
16106 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107}
16108
16109/*
16110 * "setbufvar()" function
16111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016113f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016114 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016115 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116{
16117 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016120 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 char_u nbuf[NUMBUFLEN];
16122
16123 if (check_restricted() || check_secure())
16124 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016125 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16126 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016127 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 varp = &argvars[2];
16129
16130 if (buf != NULL && varname != NULL && varp != NULL)
16131 {
16132 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134
16135 if (*varname == '&')
16136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016137 long numval;
16138 char_u *strval;
16139 int error = FALSE;
16140
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016142 numval = get_tv_number_chk(varp, &error);
16143 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 if (!error && strval != NULL)
16145 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 }
16147 else
16148 {
16149 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16150 if (bufvarname != NULL)
16151 {
16152 STRCPY(bufvarname, "b:");
16153 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016154 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 vim_free(bufvarname);
16156 }
16157 }
16158
16159 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162}
16163
16164/*
16165 * "setcmdpos()" function
16166 */
16167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016168f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 typval_T *argvars;
16170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016172 int pos = (int)get_tv_number(&argvars[0]) - 1;
16173
16174 if (pos >= 0)
16175 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176}
16177
16178/*
16179 * "setline()" function
16180 */
16181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016183 typval_T *argvars;
16184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185{
16186 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016187 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016188 list_T *l = NULL;
16189 listitem_T *li = NULL;
16190 long added = 0;
16191 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016193 lnum = get_tv_lnum(&argvars[0]);
16194 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016196 l = argvars[1].vval.v_list;
16197 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016199 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016200 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016201
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016202 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016203 for (;;)
16204 {
16205 if (l != NULL)
16206 {
16207 /* list argument, get next string */
16208 if (li == NULL)
16209 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016210 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016211 li = li->li_next;
16212 }
16213
16214 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016215 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016216 break;
16217 if (lnum <= curbuf->b_ml.ml_line_count)
16218 {
16219 /* existing line, replace it */
16220 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16221 {
16222 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016223 if (lnum == curwin->w_cursor.lnum)
16224 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016225 rettv->vval.v_number = 0; /* OK */
16226 }
16227 }
16228 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16229 {
16230 /* lnum is one past the last line, append the line */
16231 ++added;
16232 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16233 rettv->vval.v_number = 0; /* OK */
16234 }
16235
16236 if (l == NULL) /* only one string argument */
16237 break;
16238 ++lnum;
16239 }
16240
16241 if (added > 0)
16242 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243}
16244
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016245static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16246
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016248 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016249 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016250 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016251set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016252 win_T *wp UNUSED;
16253 typval_T *list_arg UNUSED;
16254 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016255 typval_T *rettv;
16256{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016257#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016258 char_u *act;
16259 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016260#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016261
Bram Moolenaar2641f772005-03-25 21:58:17 +000016262 rettv->vval.v_number = -1;
16263
16264#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016265 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016266 EMSG(_(e_listreq));
16267 else
16268 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016269 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016270
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016271 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016272 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016273 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016274 if (act == NULL)
16275 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016276 if (*act == 'a' || *act == 'r')
16277 action = *act;
16278 }
16279
Bram Moolenaarbc226b62010-08-09 22:14:48 +020016280 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016281 rettv->vval.v_number = 0;
16282 }
16283#endif
16284}
16285
16286/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016287 * "setloclist()" function
16288 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016289 static void
16290f_setloclist(argvars, rettv)
16291 typval_T *argvars;
16292 typval_T *rettv;
16293{
16294 win_T *win;
16295
16296 rettv->vval.v_number = -1;
16297
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016298 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016299 if (win != NULL)
16300 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16301}
16302
16303/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016304 * "setmatches()" function
16305 */
16306 static void
16307f_setmatches(argvars, rettv)
16308 typval_T *argvars;
16309 typval_T *rettv;
16310{
16311#ifdef FEAT_SEARCH_EXTRA
16312 list_T *l;
16313 listitem_T *li;
16314 dict_T *d;
16315
16316 rettv->vval.v_number = -1;
16317 if (argvars[0].v_type != VAR_LIST)
16318 {
16319 EMSG(_(e_listreq));
16320 return;
16321 }
16322 if ((l = argvars[0].vval.v_list) != NULL)
16323 {
16324
16325 /* To some extent make sure that we are dealing with a list from
16326 * "getmatches()". */
16327 li = l->lv_first;
16328 while (li != NULL)
16329 {
16330 if (li->li_tv.v_type != VAR_DICT
16331 || (d = li->li_tv.vval.v_dict) == NULL)
16332 {
16333 EMSG(_(e_invarg));
16334 return;
16335 }
16336 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16337 && dict_find(d, (char_u *)"pattern", -1) != NULL
16338 && dict_find(d, (char_u *)"priority", -1) != NULL
16339 && dict_find(d, (char_u *)"id", -1) != NULL))
16340 {
16341 EMSG(_(e_invarg));
16342 return;
16343 }
16344 li = li->li_next;
16345 }
16346
16347 clear_matches(curwin);
16348 li = l->lv_first;
16349 while (li != NULL)
16350 {
16351 d = li->li_tv.vval.v_dict;
16352 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16353 get_dict_string(d, (char_u *)"pattern", FALSE),
16354 (int)get_dict_number(d, (char_u *)"priority"),
16355 (int)get_dict_number(d, (char_u *)"id"));
16356 li = li->li_next;
16357 }
16358 rettv->vval.v_number = 0;
16359 }
16360#endif
16361}
16362
16363/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016364 * "setpos()" function
16365 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016366 static void
16367f_setpos(argvars, rettv)
16368 typval_T *argvars;
16369 typval_T *rettv;
16370{
16371 pos_T pos;
16372 int fnum;
16373 char_u *name;
16374
Bram Moolenaar08250432008-02-13 11:42:46 +000016375 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016376 name = get_tv_string_chk(argvars);
16377 if (name != NULL)
16378 {
16379 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16380 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016381 if (--pos.col < 0)
16382 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016383 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016384 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016385 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016386 if (fnum == curbuf->b_fnum)
16387 {
16388 curwin->w_cursor = pos;
16389 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016390 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016391 }
16392 else
16393 EMSG(_(e_invarg));
16394 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016395 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16396 {
16397 /* set mark */
16398 if (setmark_pos(name[1], &pos, fnum) == OK)
16399 rettv->vval.v_number = 0;
16400 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016401 else
16402 EMSG(_(e_invarg));
16403 }
16404 }
16405}
16406
16407/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016408 * "setqflist()" function
16409 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016410 static void
16411f_setqflist(argvars, rettv)
16412 typval_T *argvars;
16413 typval_T *rettv;
16414{
16415 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16416}
16417
16418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 * "setreg()" function
16420 */
16421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016422f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016423 typval_T *argvars;
16424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425{
16426 int regname;
16427 char_u *strregname;
16428 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 int append;
16431 char_u yank_type;
16432 long block_len;
16433
16434 block_len = -1;
16435 yank_type = MAUTO;
16436 append = FALSE;
16437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 if (strregname == NULL)
16442 return; /* type error; errmsg already given */
16443 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 if (regname == 0 || regname == '@')
16445 regname = '"';
16446 else if (regname == '=')
16447 return;
16448
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 stropt = get_tv_string_chk(&argvars[2]);
16452 if (stropt == NULL)
16453 return; /* type error */
16454 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 switch (*stropt)
16456 {
16457 case 'a': case 'A': /* append */
16458 append = TRUE;
16459 break;
16460 case 'v': case 'c': /* character-wise selection */
16461 yank_type = MCHAR;
16462 break;
16463 case 'V': case 'l': /* line-wise selection */
16464 yank_type = MLINE;
16465 break;
16466#ifdef FEAT_VISUAL
16467 case 'b': case Ctrl_V: /* block-wise selection */
16468 yank_type = MBLOCK;
16469 if (VIM_ISDIGIT(stropt[1]))
16470 {
16471 ++stropt;
16472 block_len = getdigits(&stropt) - 1;
16473 --stropt;
16474 }
16475 break;
16476#endif
16477 }
16478 }
16479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016480 strval = get_tv_string_chk(&argvars[1]);
16481 if (strval != NULL)
16482 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016484 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485}
16486
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016487/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016488 * "settabvar()" function
16489 */
16490 static void
16491f_settabvar(argvars, rettv)
16492 typval_T *argvars;
16493 typval_T *rettv;
16494{
16495 tabpage_T *save_curtab;
16496 char_u *varname, *tabvarname;
16497 typval_T *varp;
16498 tabpage_T *tp;
16499
16500 rettv->vval.v_number = 0;
16501
16502 if (check_restricted() || check_secure())
16503 return;
16504
16505 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16506 varname = get_tv_string_chk(&argvars[1]);
16507 varp = &argvars[2];
16508
16509 if (tp != NULL && varname != NULL && varp != NULL)
16510 {
16511 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016512 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016513
16514 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16515 if (tabvarname != NULL)
16516 {
16517 STRCPY(tabvarname, "t:");
16518 STRCPY(tabvarname + 2, varname);
16519 set_var(tabvarname, varp, TRUE);
16520 vim_free(tabvarname);
16521 }
16522
16523 /* Restore current tabpage */
16524 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016525 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016526 }
16527}
16528
16529/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016530 * "settabwinvar()" function
16531 */
16532 static void
16533f_settabwinvar(argvars, rettv)
16534 typval_T *argvars;
16535 typval_T *rettv;
16536{
16537 setwinvar(argvars, rettv, 1);
16538}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539
16540/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016541 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016544f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016545 typval_T *argvars;
16546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016548 setwinvar(argvars, rettv, 0);
16549}
16550
16551/*
16552 * "setwinvar()" and "settabwinvar()" functions
16553 */
16554 static void
16555setwinvar(argvars, rettv, off)
16556 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016557 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016558 int off;
16559{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 win_T *win;
16561#ifdef FEAT_WINDOWS
16562 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016563 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564#endif
16565 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016566 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016568 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569
16570 if (check_restricted() || check_secure())
16571 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016572
16573#ifdef FEAT_WINDOWS
16574 if (off == 1)
16575 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16576 else
16577 tp = curtab;
16578#endif
16579 win = find_win_by_nr(&argvars[off], tp);
16580 varname = get_tv_string_chk(&argvars[off + 1]);
16581 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582
16583 if (win != NULL && varname != NULL && varp != NULL)
16584 {
16585#ifdef FEAT_WINDOWS
16586 /* set curwin to be our win, temporarily */
16587 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016588 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016589 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016590 if (!win_valid(win))
16591 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 curwin = win;
16593 curbuf = curwin->w_buffer;
16594#endif
16595
16596 if (*varname == '&')
16597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598 long numval;
16599 char_u *strval;
16600 int error = FALSE;
16601
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016603 numval = get_tv_number_chk(varp, &error);
16604 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016605 if (!error && strval != NULL)
16606 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607 }
16608 else
16609 {
16610 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16611 if (winvarname != NULL)
16612 {
16613 STRCPY(winvarname, "w:");
16614 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016615 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616 vim_free(winvarname);
16617 }
16618 }
16619
16620#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016621 /* Restore current tabpage and window, if still valid (autocomands can
16622 * make them invalid). */
16623 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016624 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 if (win_valid(save_curwin))
16626 {
16627 curwin = save_curwin;
16628 curbuf = curwin->w_buffer;
16629 }
16630#endif
16631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632}
16633
16634/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016635 * "shellescape({string})" function
16636 */
16637 static void
16638f_shellescape(argvars, rettv)
16639 typval_T *argvars;
16640 typval_T *rettv;
16641{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016642 rettv->vval.v_string = vim_strsave_shellescape(
16643 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016644 rettv->v_type = VAR_STRING;
16645}
16646
16647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016648 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 */
16650 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016651f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016652 typval_T *argvars;
16653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016655 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656
Bram Moolenaar0d660222005-01-07 21:51:51 +000016657 p = get_tv_string(&argvars[0]);
16658 rettv->vval.v_string = vim_strsave(p);
16659 simplify_filename(rettv->vval.v_string); /* simplify in place */
16660 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661}
16662
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016663#ifdef FEAT_FLOAT
16664/*
16665 * "sin()" function
16666 */
16667 static void
16668f_sin(argvars, rettv)
16669 typval_T *argvars;
16670 typval_T *rettv;
16671{
16672 float_T f;
16673
16674 rettv->v_type = VAR_FLOAT;
16675 if (get_float_arg(argvars, &f) == OK)
16676 rettv->vval.v_float = sin(f);
16677 else
16678 rettv->vval.v_float = 0.0;
16679}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016680
16681/*
16682 * "sinh()" function
16683 */
16684 static void
16685f_sinh(argvars, rettv)
16686 typval_T *argvars;
16687 typval_T *rettv;
16688{
16689 float_T f;
16690
16691 rettv->v_type = VAR_FLOAT;
16692 if (get_float_arg(argvars, &f) == OK)
16693 rettv->vval.v_float = sinh(f);
16694 else
16695 rettv->vval.v_float = 0.0;
16696}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016697#endif
16698
Bram Moolenaar0d660222005-01-07 21:51:51 +000016699static int
16700#ifdef __BORLANDC__
16701 _RTLENTRYF
16702#endif
16703 item_compare __ARGS((const void *s1, const void *s2));
16704static int
16705#ifdef __BORLANDC__
16706 _RTLENTRYF
16707#endif
16708 item_compare2 __ARGS((const void *s1, const void *s2));
16709
16710static int item_compare_ic;
16711static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016712static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016713static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016714#define ITEM_COMPARE_FAIL 999
16715
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016717 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016719 static int
16720#ifdef __BORLANDC__
16721_RTLENTRYF
16722#endif
16723item_compare(s1, s2)
16724 const void *s1;
16725 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016727 char_u *p1, *p2;
16728 char_u *tofree1, *tofree2;
16729 int res;
16730 char_u numbuf1[NUMBUFLEN];
16731 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016733 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16734 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016735 if (p1 == NULL)
16736 p1 = (char_u *)"";
16737 if (p2 == NULL)
16738 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016739 if (item_compare_ic)
16740 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016742 res = STRCMP(p1, p2);
16743 vim_free(tofree1);
16744 vim_free(tofree2);
16745 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746}
16747
16748 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016749#ifdef __BORLANDC__
16750_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016752item_compare2(s1, s2)
16753 const void *s1;
16754 const void *s2;
16755{
16756 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016757 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016758 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016759 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761 /* shortcut after failure in previous call; compare all items equal */
16762 if (item_compare_func_err)
16763 return 0;
16764
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016765 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16766 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016767 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16768 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769
16770 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016771 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016772 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16773 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016774 clear_tv(&argv[0]);
16775 clear_tv(&argv[1]);
16776
16777 if (res == FAIL)
16778 res = ITEM_COMPARE_FAIL;
16779 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016780 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16781 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016782 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783 clear_tv(&rettv);
16784 return res;
16785}
16786
16787/*
16788 * "sort({list})" function
16789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016791f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016792 typval_T *argvars;
16793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794{
Bram Moolenaar33570922005-01-25 22:26:29 +000016795 list_T *l;
16796 listitem_T *li;
16797 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016798 long len;
16799 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800
Bram Moolenaar0d660222005-01-07 21:51:51 +000016801 if (argvars[0].v_type != VAR_LIST)
16802 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 else
16804 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016805 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016806 if (l == NULL || tv_check_lock(l->lv_lock,
16807 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016808 return;
16809 rettv->vval.v_list = l;
16810 rettv->v_type = VAR_LIST;
16811 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812
Bram Moolenaar0d660222005-01-07 21:51:51 +000016813 len = list_len(l);
16814 if (len <= 1)
16815 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816
Bram Moolenaar0d660222005-01-07 21:51:51 +000016817 item_compare_ic = FALSE;
16818 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016819 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016820 if (argvars[1].v_type != VAR_UNKNOWN)
16821 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016822 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016823 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016824 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016825 else
16826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016827 int error = FALSE;
16828
16829 i = get_tv_number_chk(&argvars[1], &error);
16830 if (error)
16831 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016832 if (i == 1)
16833 item_compare_ic = TRUE;
16834 else
16835 item_compare_func = get_tv_string(&argvars[1]);
16836 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016837
16838 if (argvars[2].v_type != VAR_UNKNOWN)
16839 {
16840 /* optional third argument: {dict} */
16841 if (argvars[2].v_type != VAR_DICT)
16842 {
16843 EMSG(_(e_dictreq));
16844 return;
16845 }
16846 item_compare_selfdict = argvars[2].vval.v_dict;
16847 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849
Bram Moolenaar0d660222005-01-07 21:51:51 +000016850 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016851 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016852 if (ptrs == NULL)
16853 return;
16854 i = 0;
16855 for (li = l->lv_first; li != NULL; li = li->li_next)
16856 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016858 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859 /* test the compare function */
16860 if (item_compare_func != NULL
16861 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16862 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016863 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 {
16866 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016867 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016868 item_compare_func == NULL ? item_compare : item_compare2);
16869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016870 if (!item_compare_func_err)
16871 {
16872 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016873 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016874 l->lv_len = 0;
16875 for (i = 0; i < len; ++i)
16876 list_append(l, ptrs[i]);
16877 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878 }
16879
16880 vim_free(ptrs);
16881 }
16882}
16883
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016884/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016885 * "soundfold({word})" function
16886 */
16887 static void
16888f_soundfold(argvars, rettv)
16889 typval_T *argvars;
16890 typval_T *rettv;
16891{
16892 char_u *s;
16893
16894 rettv->v_type = VAR_STRING;
16895 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016896#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016897 rettv->vval.v_string = eval_soundfold(s);
16898#else
16899 rettv->vval.v_string = vim_strsave(s);
16900#endif
16901}
16902
16903/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016904 * "spellbadword()" function
16905 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016906 static void
16907f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016908 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016909 typval_T *rettv;
16910{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016911 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016912 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016913 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016914
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016915 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016916 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016917
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016918#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016919 if (argvars[0].v_type == VAR_UNKNOWN)
16920 {
16921 /* Find the start and length of the badly spelled word. */
16922 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16923 if (len != 0)
16924 word = ml_get_cursor();
16925 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016926 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016927 {
16928 char_u *str = get_tv_string_chk(&argvars[0]);
16929 int capcol = -1;
16930
16931 if (str != NULL)
16932 {
16933 /* Check the argument for spelling. */
16934 while (*str != NUL)
16935 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016936 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016937 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016938 {
16939 word = str;
16940 break;
16941 }
16942 str += len;
16943 }
16944 }
16945 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016946#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016947
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016948 list_append_string(rettv->vval.v_list, word, len);
16949 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016950 attr == HLF_SPB ? "bad" :
16951 attr == HLF_SPR ? "rare" :
16952 attr == HLF_SPL ? "local" :
16953 attr == HLF_SPC ? "caps" :
16954 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016955}
16956
16957/*
16958 * "spellsuggest()" function
16959 */
16960 static void
16961f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016962 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016963 typval_T *rettv;
16964{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016965#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016966 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016967 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016968 int maxcount;
16969 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016970 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016971 listitem_T *li;
16972 int need_capital = FALSE;
16973#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016974
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016975 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016976 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016977
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016978#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016979 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016980 {
16981 str = get_tv_string(&argvars[0]);
16982 if (argvars[1].v_type != VAR_UNKNOWN)
16983 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016984 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016985 if (maxcount <= 0)
16986 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016987 if (argvars[2].v_type != VAR_UNKNOWN)
16988 {
16989 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16990 if (typeerr)
16991 return;
16992 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016993 }
16994 else
16995 maxcount = 25;
16996
Bram Moolenaar4770d092006-01-12 23:22:24 +000016997 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016998
16999 for (i = 0; i < ga.ga_len; ++i)
17000 {
17001 str = ((char_u **)ga.ga_data)[i];
17002
17003 li = listitem_alloc();
17004 if (li == NULL)
17005 vim_free(str);
17006 else
17007 {
17008 li->li_tv.v_type = VAR_STRING;
17009 li->li_tv.v_lock = 0;
17010 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017011 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017012 }
17013 }
17014 ga_clear(&ga);
17015 }
17016#endif
17017}
17018
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017020f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 typval_T *argvars;
17022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023{
17024 char_u *str;
17025 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017026 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027 regmatch_T regmatch;
17028 char_u patbuf[NUMBUFLEN];
17029 char_u *save_cpo;
17030 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017032 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034
17035 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17036 save_cpo = p_cpo;
17037 p_cpo = (char_u *)"";
17038
17039 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017040 if (argvars[1].v_type != VAR_UNKNOWN)
17041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017042 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17043 if (pat == NULL)
17044 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017045 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017046 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017047 }
17048 if (pat == NULL || *pat == NUL)
17049 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017051 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 if (typeerr)
17054 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055
Bram Moolenaar0d660222005-01-07 21:51:51 +000017056 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17057 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017059 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017060 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017062 if (*str == NUL)
17063 match = FALSE; /* empty item at the end */
17064 else
17065 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066 if (match)
17067 end = regmatch.startp[0];
17068 else
17069 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017070 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17071 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017073 if (list_append_string(rettv->vval.v_list, str,
17074 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076 }
17077 if (!match)
17078 break;
17079 /* Advance to just after the match. */
17080 if (regmatch.endp[0] > str)
17081 col = 0;
17082 else
17083 {
17084 /* Don't get stuck at the same match. */
17085#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017086 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017087#else
17088 col = 1;
17089#endif
17090 }
17091 str = regmatch.endp[0];
17092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098}
17099
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017100#ifdef FEAT_FLOAT
17101/*
17102 * "sqrt()" function
17103 */
17104 static void
17105f_sqrt(argvars, rettv)
17106 typval_T *argvars;
17107 typval_T *rettv;
17108{
17109 float_T f;
17110
17111 rettv->v_type = VAR_FLOAT;
17112 if (get_float_arg(argvars, &f) == OK)
17113 rettv->vval.v_float = sqrt(f);
17114 else
17115 rettv->vval.v_float = 0.0;
17116}
17117
17118/*
17119 * "str2float()" function
17120 */
17121 static void
17122f_str2float(argvars, rettv)
17123 typval_T *argvars;
17124 typval_T *rettv;
17125{
17126 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17127
17128 if (*p == '+')
17129 p = skipwhite(p + 1);
17130 (void)string2float(p, &rettv->vval.v_float);
17131 rettv->v_type = VAR_FLOAT;
17132}
17133#endif
17134
Bram Moolenaar2c932302006-03-18 21:42:09 +000017135/*
17136 * "str2nr()" function
17137 */
17138 static void
17139f_str2nr(argvars, rettv)
17140 typval_T *argvars;
17141 typval_T *rettv;
17142{
17143 int base = 10;
17144 char_u *p;
17145 long n;
17146
17147 if (argvars[1].v_type != VAR_UNKNOWN)
17148 {
17149 base = get_tv_number(&argvars[1]);
17150 if (base != 8 && base != 10 && base != 16)
17151 {
17152 EMSG(_(e_invarg));
17153 return;
17154 }
17155 }
17156
17157 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017158 if (*p == '+')
17159 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017160 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17161 rettv->vval.v_number = n;
17162}
17163
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164#ifdef HAVE_STRFTIME
17165/*
17166 * "strftime({format}[, {time}])" function
17167 */
17168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017170 typval_T *argvars;
17171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172{
17173 char_u result_buf[256];
17174 struct tm *curtime;
17175 time_t seconds;
17176 char_u *p;
17177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017178 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017180 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017181 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 seconds = time(NULL);
17183 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017184 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 curtime = localtime(&seconds);
17186 /* MSVC returns NULL for an invalid value of seconds. */
17187 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 else
17190 {
17191# ifdef FEAT_MBYTE
17192 vimconv_T conv;
17193 char_u *enc;
17194
17195 conv.vc_type = CONV_NONE;
17196 enc = enc_locale();
17197 convert_setup(&conv, p_enc, enc);
17198 if (conv.vc_type != CONV_NONE)
17199 p = string_convert(&conv, p, NULL);
17200# endif
17201 if (p != NULL)
17202 (void)strftime((char *)result_buf, sizeof(result_buf),
17203 (char *)p, curtime);
17204 else
17205 result_buf[0] = NUL;
17206
17207# ifdef FEAT_MBYTE
17208 if (conv.vc_type != CONV_NONE)
17209 vim_free(p);
17210 convert_setup(&conv, enc, p_enc);
17211 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017212 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 else
17214# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017215 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216
17217# ifdef FEAT_MBYTE
17218 /* Release conversion descriptors */
17219 convert_setup(&conv, NULL, NULL);
17220 vim_free(enc);
17221# endif
17222 }
17223}
17224#endif
17225
17226/*
17227 * "stridx()" function
17228 */
17229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017230f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017231 typval_T *argvars;
17232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233{
17234 char_u buf[NUMBUFLEN];
17235 char_u *needle;
17236 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017237 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017239 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017241 needle = get_tv_string_chk(&argvars[1]);
17242 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017243 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017244 if (needle == NULL || haystack == NULL)
17245 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246
Bram Moolenaar33570922005-01-25 22:26:29 +000017247 if (argvars[2].v_type != VAR_UNKNOWN)
17248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017249 int error = FALSE;
17250
17251 start_idx = get_tv_number_chk(&argvars[2], &error);
17252 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017253 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017254 if (start_idx >= 0)
17255 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017256 }
17257
17258 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17259 if (pos != NULL)
17260 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261}
17262
17263/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017264 * "string()" function
17265 */
17266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017267f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017268 typval_T *argvars;
17269 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017270{
17271 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017272 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017274 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017275 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017276 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017277 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279}
17280
17281/*
17282 * "strlen()" function
17283 */
17284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017285f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017286 typval_T *argvars;
17287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017289 rettv->vval.v_number = (varnumber_T)(STRLEN(
17290 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291}
17292
17293/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017294 * "strchars()" function
17295 */
17296 static void
17297f_strchars(argvars, rettv)
17298 typval_T *argvars;
17299 typval_T *rettv;
17300{
17301 char_u *s = get_tv_string(&argvars[0]);
17302#ifdef FEAT_MBYTE
17303 varnumber_T len = 0;
17304
17305 while (*s != NUL)
17306 {
17307 mb_cptr2char_adv(&s);
17308 ++len;
17309 }
17310 rettv->vval.v_number = len;
17311#else
17312 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17313#endif
17314}
17315
17316/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017317 * "strdisplaywidth()" function
17318 */
17319 static void
17320f_strdisplaywidth(argvars, rettv)
17321 typval_T *argvars;
17322 typval_T *rettv;
17323{
17324 char_u *s = get_tv_string(&argvars[0]);
17325 int col = 0;
17326
17327 if (argvars[1].v_type != VAR_UNKNOWN)
17328 col = get_tv_number(&argvars[1]);
17329
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017330 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017331}
17332
17333/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017334 * "strwidth()" function
17335 */
17336 static void
17337f_strwidth(argvars, rettv)
17338 typval_T *argvars;
17339 typval_T *rettv;
17340{
17341 char_u *s = get_tv_string(&argvars[0]);
17342
17343 rettv->vval.v_number = (varnumber_T)(
17344#ifdef FEAT_MBYTE
17345 mb_string2cells(s, -1)
17346#else
17347 STRLEN(s)
17348#endif
17349 );
17350}
17351
17352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 * "strpart()" function
17354 */
17355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017356f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017357 typval_T *argvars;
17358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359{
17360 char_u *p;
17361 int n;
17362 int len;
17363 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017364 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 slen = (int)STRLEN(p);
17368
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 n = get_tv_number_chk(&argvars[1], &error);
17370 if (error)
17371 len = 0;
17372 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017373 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 else
17375 len = slen - n; /* default len: all bytes that are available. */
17376
17377 /*
17378 * Only return the overlap between the specified part and the actual
17379 * string.
17380 */
17381 if (n < 0)
17382 {
17383 len += n;
17384 n = 0;
17385 }
17386 else if (n > slen)
17387 n = slen;
17388 if (len < 0)
17389 len = 0;
17390 else if (n + len > slen)
17391 len = slen - n;
17392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393 rettv->v_type = VAR_STRING;
17394 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395}
17396
17397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017398 * "strridx()" function
17399 */
17400 static void
17401f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 typval_T *argvars;
17403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017404{
17405 char_u buf[NUMBUFLEN];
17406 char_u *needle;
17407 char_u *haystack;
17408 char_u *rest;
17409 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017410 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017412 needle = get_tv_string_chk(&argvars[1]);
17413 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017414
17415 rettv->vval.v_number = -1;
17416 if (needle == NULL || haystack == NULL)
17417 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017418
17419 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017420 if (argvars[2].v_type != VAR_UNKNOWN)
17421 {
17422 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017423 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017424 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017425 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017426 }
17427 else
17428 end_idx = haystack_len;
17429
Bram Moolenaar0d660222005-01-07 21:51:51 +000017430 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017431 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017432 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017433 lastmatch = haystack + end_idx;
17434 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017435 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017436 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017437 for (rest = haystack; *rest != '\0'; ++rest)
17438 {
17439 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017440 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017441 break;
17442 lastmatch = rest;
17443 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017444 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017445
17446 if (lastmatch == NULL)
17447 rettv->vval.v_number = -1;
17448 else
17449 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17450}
17451
17452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 * "strtrans()" function
17454 */
17455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 typval_T *argvars;
17458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017460 rettv->v_type = VAR_STRING;
17461 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462}
17463
17464/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017465 * "submatch()" function
17466 */
17467 static void
17468f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017469 typval_T *argvars;
17470 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017471{
17472 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017473 rettv->vval.v_string =
17474 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017475}
17476
17477/*
17478 * "substitute()" function
17479 */
17480 static void
17481f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017482 typval_T *argvars;
17483 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017484{
17485 char_u patbuf[NUMBUFLEN];
17486 char_u subbuf[NUMBUFLEN];
17487 char_u flagsbuf[NUMBUFLEN];
17488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017489 char_u *str = get_tv_string_chk(&argvars[0]);
17490 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17491 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17492 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17493
Bram Moolenaar0d660222005-01-07 21:51:51 +000017494 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017495 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17496 rettv->vval.v_string = NULL;
17497 else
17498 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017499}
17500
17501/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017502 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017506 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508{
17509 int id = 0;
17510#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017511 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 long col;
17513 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017514 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017516 lnum = get_tv_lnum(argvars); /* -1 on type error */
17517 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17518 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017520 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017521 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017522 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523#endif
17524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526}
17527
17528/*
17529 * "synIDattr(id, what [, mode])" function
17530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017532f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017533 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535{
17536 char_u *p = NULL;
17537#ifdef FEAT_SYN_HL
17538 int id;
17539 char_u *what;
17540 char_u *mode;
17541 char_u modebuf[NUMBUFLEN];
17542 int modec;
17543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017544 id = get_tv_number(&argvars[0]);
17545 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017546 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017548 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017550 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551 modec = 0; /* replace invalid with current */
17552 }
17553 else
17554 {
17555#ifdef FEAT_GUI
17556 if (gui.in_use)
17557 modec = 'g';
17558 else
17559#endif
17560 if (t_colors > 1)
17561 modec = 'c';
17562 else
17563 modec = 't';
17564 }
17565
17566
17567 switch (TOLOWER_ASC(what[0]))
17568 {
17569 case 'b':
17570 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17571 p = highlight_color(id, what, modec);
17572 else /* bold */
17573 p = highlight_has_attr(id, HL_BOLD, modec);
17574 break;
17575
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017576 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 p = highlight_color(id, what, modec);
17578 break;
17579
17580 case 'i':
17581 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17582 p = highlight_has_attr(id, HL_INVERSE, modec);
17583 else /* italic */
17584 p = highlight_has_attr(id, HL_ITALIC, modec);
17585 break;
17586
17587 case 'n': /* name */
17588 p = get_highlight_name(NULL, id - 1);
17589 break;
17590
17591 case 'r': /* reverse */
17592 p = highlight_has_attr(id, HL_INVERSE, modec);
17593 break;
17594
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017595 case 's':
17596 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17597 p = highlight_color(id, what, modec);
17598 else /* standout */
17599 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 break;
17601
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017602 case 'u':
17603 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17604 /* underline */
17605 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17606 else
17607 /* undercurl */
17608 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 break;
17610 }
17611
17612 if (p != NULL)
17613 p = vim_strsave(p);
17614#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017615 rettv->v_type = VAR_STRING;
17616 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617}
17618
17619/*
17620 * "synIDtrans(id)" function
17621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017623f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626{
17627 int id;
17628
17629#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631
17632 if (id > 0)
17633 id = syn_get_final_id(id);
17634 else
17635#endif
17636 id = 0;
17637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017638 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639}
17640
17641/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017642 * "synconcealed(lnum, col)" function
17643 */
17644 static void
17645f_synconcealed(argvars, rettv)
17646 typval_T *argvars UNUSED;
17647 typval_T *rettv;
17648{
17649#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17650 long lnum;
17651 long col;
17652 int syntax_flags = 0;
17653 int cchar;
17654 int matchid = 0;
17655 char_u str[NUMBUFLEN];
17656#endif
17657
17658 rettv->v_type = VAR_LIST;
17659 rettv->vval.v_list = NULL;
17660
17661#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17662 lnum = get_tv_lnum(argvars); /* -1 on type error */
17663 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17664
17665 vim_memset(str, NUL, sizeof(str));
17666
17667 if (rettv_list_alloc(rettv) != FAIL)
17668 {
17669 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17670 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17671 && curwin->w_p_cole > 0)
17672 {
17673 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17674 syntax_flags = get_syntax_info(&matchid);
17675
17676 /* get the conceal character */
17677 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17678 {
17679 cchar = syn_get_sub_char();
17680 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17681 cchar = lcs_conceal;
17682 if (cchar != NUL)
17683 {
17684# ifdef FEAT_MBYTE
17685 if (has_mbyte)
17686 (*mb_char2bytes)(cchar, str);
17687 else
17688# endif
17689 str[0] = cchar;
17690 }
17691 }
17692 }
17693
17694 list_append_number(rettv->vval.v_list,
17695 (syntax_flags & HL_CONCEAL) != 0);
17696 /* -1 to auto-determine strlen */
17697 list_append_string(rettv->vval.v_list, str, -1);
17698 list_append_number(rettv->vval.v_list, matchid);
17699 }
17700#endif
17701}
17702
17703/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017704 * "synstack(lnum, col)" function
17705 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017706 static void
17707f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017708 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017709 typval_T *rettv;
17710{
17711#ifdef FEAT_SYN_HL
17712 long lnum;
17713 long col;
17714 int i;
17715 int id;
17716#endif
17717
17718 rettv->v_type = VAR_LIST;
17719 rettv->vval.v_list = NULL;
17720
17721#ifdef FEAT_SYN_HL
17722 lnum = get_tv_lnum(argvars); /* -1 on type error */
17723 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17724
17725 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017726 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017727 && rettv_list_alloc(rettv) != FAIL)
17728 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017729 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017730 for (i = 0; ; ++i)
17731 {
17732 id = syn_get_stack_item(i);
17733 if (id < 0)
17734 break;
17735 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17736 break;
17737 }
17738 }
17739#endif
17740}
17741
17742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 * "system()" function
17744 */
17745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017746f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 typval_T *argvars;
17748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017750 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017752 char_u *infile = NULL;
17753 char_u buf[NUMBUFLEN];
17754 int err = FALSE;
17755 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017757 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017758 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017759
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017760 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017761 {
17762 /*
17763 * Write the string to a temp file, to be used for input of the shell
17764 * command.
17765 */
17766 if ((infile = vim_tempname('i')) == NULL)
17767 {
17768 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017769 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017770 }
17771
17772 fd = mch_fopen((char *)infile, WRITEBIN);
17773 if (fd == NULL)
17774 {
17775 EMSG2(_(e_notopen), infile);
17776 goto done;
17777 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017778 p = get_tv_string_buf_chk(&argvars[1], buf);
17779 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017780 {
17781 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017782 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017783 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017784 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17785 err = TRUE;
17786 if (fclose(fd) != 0)
17787 err = TRUE;
17788 if (err)
17789 {
17790 EMSG(_("E677: Error writing temp file"));
17791 goto done;
17792 }
17793 }
17794
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017795 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17796 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017797
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798#ifdef USE_CR
17799 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017800 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 {
17802 char_u *s;
17803
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017804 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 {
17806 if (*s == CAR)
17807 *s = NL;
17808 }
17809 }
17810#else
17811# ifdef USE_CRNL
17812 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017813 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 {
17815 char_u *s, *d;
17816
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017817 d = res;
17818 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 {
17820 if (s[0] == CAR && s[1] == NL)
17821 ++s;
17822 *d++ = *s;
17823 }
17824 *d = NUL;
17825 }
17826# endif
17827#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017828
17829done:
17830 if (infile != NULL)
17831 {
17832 mch_remove(infile);
17833 vim_free(infile);
17834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017835 rettv->v_type = VAR_STRING;
17836 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837}
17838
17839/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017840 * "tabpagebuflist()" function
17841 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017842 static void
17843f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017844 typval_T *argvars UNUSED;
17845 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017846{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017847#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017848 tabpage_T *tp;
17849 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017850
17851 if (argvars[0].v_type == VAR_UNKNOWN)
17852 wp = firstwin;
17853 else
17854 {
17855 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17856 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017857 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017858 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017859 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017860 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017861 for (; wp != NULL; wp = wp->w_next)
17862 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017863 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017864 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017865 }
17866#endif
17867}
17868
17869
17870/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017871 * "tabpagenr()" function
17872 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017873 static void
17874f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017875 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017876 typval_T *rettv;
17877{
17878 int nr = 1;
17879#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017880 char_u *arg;
17881
17882 if (argvars[0].v_type != VAR_UNKNOWN)
17883 {
17884 arg = get_tv_string_chk(&argvars[0]);
17885 nr = 0;
17886 if (arg != NULL)
17887 {
17888 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017889 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017890 else
17891 EMSG2(_(e_invexpr2), arg);
17892 }
17893 }
17894 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017895 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017896#endif
17897 rettv->vval.v_number = nr;
17898}
17899
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017900
17901#ifdef FEAT_WINDOWS
17902static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17903
17904/*
17905 * Common code for tabpagewinnr() and winnr().
17906 */
17907 static int
17908get_winnr(tp, argvar)
17909 tabpage_T *tp;
17910 typval_T *argvar;
17911{
17912 win_T *twin;
17913 int nr = 1;
17914 win_T *wp;
17915 char_u *arg;
17916
17917 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17918 if (argvar->v_type != VAR_UNKNOWN)
17919 {
17920 arg = get_tv_string_chk(argvar);
17921 if (arg == NULL)
17922 nr = 0; /* type error; errmsg already given */
17923 else if (STRCMP(arg, "$") == 0)
17924 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17925 else if (STRCMP(arg, "#") == 0)
17926 {
17927 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17928 if (twin == NULL)
17929 nr = 0;
17930 }
17931 else
17932 {
17933 EMSG2(_(e_invexpr2), arg);
17934 nr = 0;
17935 }
17936 }
17937
17938 if (nr > 0)
17939 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17940 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017941 {
17942 if (wp == NULL)
17943 {
17944 /* didn't find it in this tabpage */
17945 nr = 0;
17946 break;
17947 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017948 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017949 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017950 return nr;
17951}
17952#endif
17953
17954/*
17955 * "tabpagewinnr()" function
17956 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017957 static void
17958f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017959 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017960 typval_T *rettv;
17961{
17962 int nr = 1;
17963#ifdef FEAT_WINDOWS
17964 tabpage_T *tp;
17965
17966 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17967 if (tp == NULL)
17968 nr = 0;
17969 else
17970 nr = get_winnr(tp, &argvars[1]);
17971#endif
17972 rettv->vval.v_number = nr;
17973}
17974
17975
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017976/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017977 * "tagfiles()" function
17978 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017979 static void
17980f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017981 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017982 typval_T *rettv;
17983{
Bram Moolenaard9462e32011-04-11 21:35:11 +020017984 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017985 tagname_T tn;
17986 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017987
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017988 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017989 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017990 fname = alloc(MAXPATHL);
17991 if (fname == NULL)
17992 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017993
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017994 for (first = TRUE; ; first = FALSE)
17995 if (get_tagfname(&tn, first, fname) == FAIL
17996 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017997 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017998 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020017999 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018000}
18001
18002/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018003 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018004 */
18005 static void
18006f_taglist(argvars, rettv)
18007 typval_T *argvars;
18008 typval_T *rettv;
18009{
18010 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018011
18012 tag_pattern = get_tv_string(&argvars[0]);
18013
18014 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018015 if (*tag_pattern == NUL)
18016 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018017
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018018 if (rettv_list_alloc(rettv) == OK)
18019 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018020}
18021
18022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 * "tempname()" function
18024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018026f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018027 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029{
18030 static int x = 'A';
18031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018032 rettv->v_type = VAR_STRING;
18033 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034
18035 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18036 * names. Skip 'I' and 'O', they are used for shell redirection. */
18037 do
18038 {
18039 if (x == 'Z')
18040 x = '0';
18041 else if (x == '9')
18042 x = 'A';
18043 else
18044 {
18045#ifdef EBCDIC
18046 if (x == 'I')
18047 x = 'J';
18048 else if (x == 'R')
18049 x = 'S';
18050 else
18051#endif
18052 ++x;
18053 }
18054 } while (x == 'I' || x == 'O');
18055}
18056
18057/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018058 * "test(list)" function: Just checking the walls...
18059 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018060 static void
18061f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018062 typval_T *argvars UNUSED;
18063 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018064{
18065 /* Used for unit testing. Change the code below to your liking. */
18066#if 0
18067 listitem_T *li;
18068 list_T *l;
18069 char_u *bad, *good;
18070
18071 if (argvars[0].v_type != VAR_LIST)
18072 return;
18073 l = argvars[0].vval.v_list;
18074 if (l == NULL)
18075 return;
18076 li = l->lv_first;
18077 if (li == NULL)
18078 return;
18079 bad = get_tv_string(&li->li_tv);
18080 li = li->li_next;
18081 if (li == NULL)
18082 return;
18083 good = get_tv_string(&li->li_tv);
18084 rettv->vval.v_number = test_edit_score(bad, good);
18085#endif
18086}
18087
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018088#ifdef FEAT_FLOAT
18089/*
18090 * "tan()" function
18091 */
18092 static void
18093f_tan(argvars, rettv)
18094 typval_T *argvars;
18095 typval_T *rettv;
18096{
18097 float_T f;
18098
18099 rettv->v_type = VAR_FLOAT;
18100 if (get_float_arg(argvars, &f) == OK)
18101 rettv->vval.v_float = tan(f);
18102 else
18103 rettv->vval.v_float = 0.0;
18104}
18105
18106/*
18107 * "tanh()" function
18108 */
18109 static void
18110f_tanh(argvars, rettv)
18111 typval_T *argvars;
18112 typval_T *rettv;
18113{
18114 float_T f;
18115
18116 rettv->v_type = VAR_FLOAT;
18117 if (get_float_arg(argvars, &f) == OK)
18118 rettv->vval.v_float = tanh(f);
18119 else
18120 rettv->vval.v_float = 0.0;
18121}
18122#endif
18123
Bram Moolenaard52d9742005-08-21 22:20:28 +000018124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 * "tolower(string)" function
18126 */
18127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018128f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018129 typval_T *argvars;
18130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131{
18132 char_u *p;
18133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018134 p = vim_strsave(get_tv_string(&argvars[0]));
18135 rettv->v_type = VAR_STRING;
18136 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137
18138 if (p != NULL)
18139 while (*p != NUL)
18140 {
18141#ifdef FEAT_MBYTE
18142 int l;
18143
18144 if (enc_utf8)
18145 {
18146 int c, lc;
18147
18148 c = utf_ptr2char(p);
18149 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018150 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 /* TODO: reallocate string when byte count changes. */
18152 if (utf_char2len(lc) == l)
18153 utf_char2bytes(lc, p);
18154 p += l;
18155 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018156 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 p += l; /* skip multi-byte character */
18158 else
18159#endif
18160 {
18161 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18162 ++p;
18163 }
18164 }
18165}
18166
18167/*
18168 * "toupper(string)" function
18169 */
18170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018172 typval_T *argvars;
18173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018175 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018176 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
18179/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018180 * "tr(string, fromstr, tostr)" function
18181 */
18182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018184 typval_T *argvars;
18185 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018186{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018187 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018188 char_u *fromstr;
18189 char_u *tostr;
18190 char_u *p;
18191#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018192 int inlen;
18193 int fromlen;
18194 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018195 int idx;
18196 char_u *cpstr;
18197 int cplen;
18198 int first = TRUE;
18199#endif
18200 char_u buf[NUMBUFLEN];
18201 char_u buf2[NUMBUFLEN];
18202 garray_T ga;
18203
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018204 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018205 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18206 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018207
18208 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018209 rettv->v_type = VAR_STRING;
18210 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018211 if (fromstr == NULL || tostr == NULL)
18212 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018213 ga_init2(&ga, (int)sizeof(char), 80);
18214
18215#ifdef FEAT_MBYTE
18216 if (!has_mbyte)
18217#endif
18218 /* not multi-byte: fromstr and tostr must be the same length */
18219 if (STRLEN(fromstr) != STRLEN(tostr))
18220 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018221#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018222error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018223#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018224 EMSG2(_(e_invarg2), fromstr);
18225 ga_clear(&ga);
18226 return;
18227 }
18228
18229 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018230 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018231 {
18232#ifdef FEAT_MBYTE
18233 if (has_mbyte)
18234 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018235 inlen = (*mb_ptr2len)(in_str);
18236 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018237 cplen = inlen;
18238 idx = 0;
18239 for (p = fromstr; *p != NUL; p += fromlen)
18240 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018241 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018242 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018243 {
18244 for (p = tostr; *p != NUL; p += tolen)
18245 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018246 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018247 if (idx-- == 0)
18248 {
18249 cplen = tolen;
18250 cpstr = p;
18251 break;
18252 }
18253 }
18254 if (*p == NUL) /* tostr is shorter than fromstr */
18255 goto error;
18256 break;
18257 }
18258 ++idx;
18259 }
18260
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018261 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018262 {
18263 /* Check that fromstr and tostr have the same number of
18264 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018265 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018266 first = FALSE;
18267 for (p = tostr; *p != NUL; p += tolen)
18268 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018269 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018270 --idx;
18271 }
18272 if (idx != 0)
18273 goto error;
18274 }
18275
18276 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018277 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018278 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018279
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018280 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018281 }
18282 else
18283#endif
18284 {
18285 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018286 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018287 if (p != NULL)
18288 ga_append(&ga, tostr[p - fromstr]);
18289 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018290 ga_append(&ga, *in_str);
18291 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018292 }
18293 }
18294
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018295 /* add a terminating NUL */
18296 ga_grow(&ga, 1);
18297 ga_append(&ga, NUL);
18298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018299 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018300}
18301
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018302#ifdef FEAT_FLOAT
18303/*
18304 * "trunc({float})" function
18305 */
18306 static void
18307f_trunc(argvars, rettv)
18308 typval_T *argvars;
18309 typval_T *rettv;
18310{
18311 float_T f;
18312
18313 rettv->v_type = VAR_FLOAT;
18314 if (get_float_arg(argvars, &f) == OK)
18315 /* trunc() is not in C90, use floor() or ceil() instead. */
18316 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18317 else
18318 rettv->vval.v_float = 0.0;
18319}
18320#endif
18321
Bram Moolenaar8299df92004-07-10 09:47:34 +000018322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 * "type(expr)" function
18324 */
18325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018326f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018327 typval_T *argvars;
18328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018330 int n;
18331
18332 switch (argvars[0].v_type)
18333 {
18334 case VAR_NUMBER: n = 0; break;
18335 case VAR_STRING: n = 1; break;
18336 case VAR_FUNC: n = 2; break;
18337 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018338 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018339#ifdef FEAT_FLOAT
18340 case VAR_FLOAT: n = 5; break;
18341#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018342 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18343 }
18344 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345}
18346
18347/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018348 * "undofile(name)" function
18349 */
18350 static void
18351f_undofile(argvars, rettv)
18352 typval_T *argvars;
18353 typval_T *rettv;
18354{
18355 rettv->v_type = VAR_STRING;
18356#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018357 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018358 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018359
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018360 if (*fname == NUL)
18361 {
18362 /* If there is no file name there will be no undo file. */
18363 rettv->vval.v_string = NULL;
18364 }
18365 else
18366 {
18367 char_u *ffname = FullName_save(fname, FALSE);
18368
18369 if (ffname != NULL)
18370 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18371 vim_free(ffname);
18372 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018373 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018374#else
18375 rettv->vval.v_string = NULL;
18376#endif
18377}
18378
18379/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018380 * "undotree()" function
18381 */
18382 static void
18383f_undotree(argvars, rettv)
18384 typval_T *argvars UNUSED;
18385 typval_T *rettv;
18386{
18387 if (rettv_dict_alloc(rettv) == OK)
18388 {
18389 dict_T *dict = rettv->vval.v_dict;
18390 list_T *list;
18391
Bram Moolenaar730cde92010-06-27 05:18:54 +020018392 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018393 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018394 dict_add_nr_str(dict, "save_last",
18395 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018396 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18397 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018398 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018399
18400 list = list_alloc();
18401 if (list != NULL)
18402 {
18403 u_eval_tree(curbuf->b_u_oldhead, list);
18404 dict_add_list(dict, "entries", list);
18405 }
18406 }
18407}
18408
18409/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018410 * "values(dict)" function
18411 */
18412 static void
18413f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018414 typval_T *argvars;
18415 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018416{
18417 dict_list(argvars, rettv, 1);
18418}
18419
18420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 * "virtcol(string)" function
18422 */
18423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018424f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018425 typval_T *argvars;
18426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427{
18428 colnr_T vcol = 0;
18429 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018430 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018432 fp = var2fpos(&argvars[0], FALSE, &fnum);
18433 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18434 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 {
18436 getvvcol(curwin, fp, NULL, NULL, &vcol);
18437 ++vcol;
18438 }
18439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018440 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441}
18442
18443/*
18444 * "visualmode()" function
18445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018447f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018448 typval_T *argvars UNUSED;
18449 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450{
18451#ifdef FEAT_VISUAL
18452 char_u str[2];
18453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018454 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 str[0] = curbuf->b_visual_mode_eval;
18456 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018457 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458
18459 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018460 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462#endif
18463}
18464
18465/*
18466 * "winbufnr(nr)" function
18467 */
18468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018469f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018470 typval_T *argvars;
18471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472{
18473 win_T *wp;
18474
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018475 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018477 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480}
18481
18482/*
18483 * "wincol()" function
18484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018486f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018487 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489{
18490 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018491 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492}
18493
18494/*
18495 * "winheight(nr)" function
18496 */
18497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018498f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018499 typval_T *argvars;
18500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501{
18502 win_T *wp;
18503
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018504 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018506 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018508 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509}
18510
18511/*
18512 * "winline()" function
18513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018515f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018516 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518{
18519 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018520 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521}
18522
18523/*
18524 * "winnr()" function
18525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018527f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018528 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530{
18531 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018532
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018534 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018536 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537}
18538
18539/*
18540 * "winrestcmd()" function
18541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018543f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018544 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546{
18547#ifdef FEAT_WINDOWS
18548 win_T *wp;
18549 int winnr = 1;
18550 garray_T ga;
18551 char_u buf[50];
18552
18553 ga_init2(&ga, (int)sizeof(char), 70);
18554 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18555 {
18556 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18557 ga_concat(&ga, buf);
18558# ifdef FEAT_VERTSPLIT
18559 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18560 ga_concat(&ga, buf);
18561# endif
18562 ++winnr;
18563 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018564 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571}
18572
18573/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018574 * "winrestview()" function
18575 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018576 static void
18577f_winrestview(argvars, rettv)
18578 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018579 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018580{
18581 dict_T *dict;
18582
18583 if (argvars[0].v_type != VAR_DICT
18584 || (dict = argvars[0].vval.v_dict) == NULL)
18585 EMSG(_(e_invarg));
18586 else
18587 {
18588 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18589 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18590#ifdef FEAT_VIRTUALEDIT
18591 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18592#endif
18593 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018594 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018595
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018596 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018597#ifdef FEAT_DIFF
18598 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18599#endif
18600 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18601 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18602
18603 check_cursor();
Bram Moolenaarab984db2012-06-06 16:29:10 +020018604 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018605
18606 if (curwin->w_topline == 0)
18607 curwin->w_topline = 1;
18608 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18609 curwin->w_topline = curbuf->b_ml.ml_line_count;
18610#ifdef FEAT_DIFF
18611 check_topfill(curwin, TRUE);
18612#endif
18613 }
18614}
18615
18616/*
18617 * "winsaveview()" function
18618 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018619 static void
18620f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018621 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018622 typval_T *rettv;
18623{
18624 dict_T *dict;
18625
Bram Moolenaara800b422010-06-27 01:15:55 +020018626 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018627 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018628 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018629
18630 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18631 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18632#ifdef FEAT_VIRTUALEDIT
18633 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18634#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018635 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018636 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18637
18638 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18639#ifdef FEAT_DIFF
18640 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18641#endif
18642 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18643 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18644}
18645
18646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 * "winwidth(nr)" function
18648 */
18649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018650f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018651 typval_T *argvars;
18652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653{
18654 win_T *wp;
18655
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018656 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 else
18660#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664#endif
18665}
18666
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018668 * "writefile()" function
18669 */
18670 static void
18671f_writefile(argvars, rettv)
18672 typval_T *argvars;
18673 typval_T *rettv;
18674{
18675 int binary = FALSE;
18676 char_u *fname;
18677 FILE *fd;
18678 listitem_T *li;
18679 char_u *s;
18680 int ret = 0;
18681 int c;
18682
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018683 if (check_restricted() || check_secure())
18684 return;
18685
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018686 if (argvars[0].v_type != VAR_LIST)
18687 {
18688 EMSG2(_(e_listarg), "writefile()");
18689 return;
18690 }
18691 if (argvars[0].vval.v_list == NULL)
18692 return;
18693
18694 if (argvars[2].v_type != VAR_UNKNOWN
18695 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18696 binary = TRUE;
18697
18698 /* Always open the file in binary mode, library functions have a mind of
18699 * their own about CR-LF conversion. */
18700 fname = get_tv_string(&argvars[1]);
18701 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18702 {
18703 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18704 ret = -1;
18705 }
18706 else
18707 {
18708 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18709 li = li->li_next)
18710 {
18711 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18712 {
18713 if (*s == '\n')
18714 c = putc(NUL, fd);
18715 else
18716 c = putc(*s, fd);
18717 if (c == EOF)
18718 {
18719 ret = -1;
18720 break;
18721 }
18722 }
18723 if (!binary || li->li_next != NULL)
18724 if (putc('\n', fd) == EOF)
18725 {
18726 ret = -1;
18727 break;
18728 }
18729 if (ret < 0)
18730 {
18731 EMSG(_(e_write));
18732 break;
18733 }
18734 }
18735 fclose(fd);
18736 }
18737
18738 rettv->vval.v_number = ret;
18739}
18740
18741/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018742 * "xor(expr, expr)" function
18743 */
18744 static void
18745f_xor(argvars, rettv)
18746 typval_T *argvars;
18747 typval_T *rettv;
18748{
18749 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18750 ^ get_tv_number_chk(&argvars[1], NULL);
18751}
18752
18753
18754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018756 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 */
18758 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018759var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018761 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018762 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018764 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018766 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767
Bram Moolenaara5525202006-03-02 22:52:09 +000018768 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018769 if (varp->v_type == VAR_LIST)
18770 {
18771 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018772 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018773 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018774 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018775
18776 l = varp->vval.v_list;
18777 if (l == NULL)
18778 return NULL;
18779
18780 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018781 pos.lnum = list_find_nr(l, 0L, &error);
18782 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018783 return NULL; /* invalid line number */
18784
18785 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018786 pos.col = list_find_nr(l, 1L, &error);
18787 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018788 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018789 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018790
18791 /* We accept "$" for the column number: last column. */
18792 li = list_find(l, 1L);
18793 if (li != NULL && li->li_tv.v_type == VAR_STRING
18794 && li->li_tv.vval.v_string != NULL
18795 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18796 pos.col = len + 1;
18797
Bram Moolenaara5525202006-03-02 22:52:09 +000018798 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018799 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018800 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018801 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018802
Bram Moolenaara5525202006-03-02 22:52:09 +000018803#ifdef FEAT_VIRTUALEDIT
18804 /* Get the virtual offset. Defaults to zero. */
18805 pos.coladd = list_find_nr(l, 2L, &error);
18806 if (error)
18807 pos.coladd = 0;
18808#endif
18809
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018810 return &pos;
18811 }
18812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018813 name = get_tv_string_chk(varp);
18814 if (name == NULL)
18815 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018816 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018818#ifdef FEAT_VISUAL
18819 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18820 {
18821 if (VIsual_active)
18822 return &VIsual;
18823 return &curwin->w_cursor;
18824 }
18825#endif
18826 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018828 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18830 return NULL;
18831 return pp;
18832 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018833
18834#ifdef FEAT_VIRTUALEDIT
18835 pos.coladd = 0;
18836#endif
18837
Bram Moolenaar477933c2007-07-17 14:32:23 +000018838 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018839 {
18840 pos.col = 0;
18841 if (name[1] == '0') /* "w0": first visible line */
18842 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018843 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018844 pos.lnum = curwin->w_topline;
18845 return &pos;
18846 }
18847 else if (name[1] == '$') /* "w$": last visible line */
18848 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018849 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018850 pos.lnum = curwin->w_botline - 1;
18851 return &pos;
18852 }
18853 }
18854 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018856 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 {
18858 pos.lnum = curbuf->b_ml.ml_line_count;
18859 pos.col = 0;
18860 }
18861 else
18862 {
18863 pos.lnum = curwin->w_cursor.lnum;
18864 pos.col = (colnr_T)STRLEN(ml_get_curline());
18865 }
18866 return &pos;
18867 }
18868 return NULL;
18869}
18870
18871/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018872 * Convert list in "arg" into a position and optional file number.
18873 * When "fnump" is NULL there is no file number, only 3 items.
18874 * Note that the column is passed on as-is, the caller may want to decrement
18875 * it to use 1 for the first column.
18876 * Return FAIL when conversion is not possible, doesn't check the position for
18877 * validity.
18878 */
18879 static int
18880list2fpos(arg, posp, fnump)
18881 typval_T *arg;
18882 pos_T *posp;
18883 int *fnump;
18884{
18885 list_T *l = arg->vval.v_list;
18886 long i = 0;
18887 long n;
18888
Bram Moolenaarbde35262006-07-23 20:12:24 +000018889 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18890 * when "fnump" isn't NULL and "coladd" is optional. */
18891 if (arg->v_type != VAR_LIST
18892 || l == NULL
18893 || l->lv_len < (fnump == NULL ? 2 : 3)
18894 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018895 return FAIL;
18896
18897 if (fnump != NULL)
18898 {
18899 n = list_find_nr(l, i++, NULL); /* fnum */
18900 if (n < 0)
18901 return FAIL;
18902 if (n == 0)
18903 n = curbuf->b_fnum; /* current buffer */
18904 *fnump = n;
18905 }
18906
18907 n = list_find_nr(l, i++, NULL); /* lnum */
18908 if (n < 0)
18909 return FAIL;
18910 posp->lnum = n;
18911
18912 n = list_find_nr(l, i++, NULL); /* col */
18913 if (n < 0)
18914 return FAIL;
18915 posp->col = n;
18916
18917#ifdef FEAT_VIRTUALEDIT
18918 n = list_find_nr(l, i, NULL);
18919 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018920 posp->coladd = 0;
18921 else
18922 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018923#endif
18924
18925 return OK;
18926}
18927
18928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 * Get the length of an environment variable name.
18930 * Advance "arg" to the first character after the name.
18931 * Return 0 for error.
18932 */
18933 static int
18934get_env_len(arg)
18935 char_u **arg;
18936{
18937 char_u *p;
18938 int len;
18939
18940 for (p = *arg; vim_isIDc(*p); ++p)
18941 ;
18942 if (p == *arg) /* no name found */
18943 return 0;
18944
18945 len = (int)(p - *arg);
18946 *arg = p;
18947 return len;
18948}
18949
18950/*
18951 * Get the length of the name of a function or internal variable.
18952 * "arg" is advanced to the first non-white character after the name.
18953 * Return 0 if something is wrong.
18954 */
18955 static int
18956get_id_len(arg)
18957 char_u **arg;
18958{
18959 char_u *p;
18960 int len;
18961
18962 /* Find the end of the name. */
18963 for (p = *arg; eval_isnamec(*p); ++p)
18964 ;
18965 if (p == *arg) /* no name found */
18966 return 0;
18967
18968 len = (int)(p - *arg);
18969 *arg = skipwhite(p);
18970
18971 return len;
18972}
18973
18974/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018975 * Get the length of the name of a variable or function.
18976 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018978 * Return -1 if curly braces expansion failed.
18979 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 * If the name contains 'magic' {}'s, expand them and return the
18981 * expanded name in an allocated string via 'alias' - caller must free.
18982 */
18983 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018984get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 char_u **arg;
18986 char_u **alias;
18987 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018988 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989{
18990 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 char_u *p;
18992 char_u *expr_start;
18993 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994
18995 *alias = NULL; /* default to no alias */
18996
18997 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18998 && (*arg)[2] == (int)KE_SNR)
18999 {
19000 /* hard coded <SNR>, already translated */
19001 *arg += 3;
19002 return get_id_len(arg) + 3;
19003 }
19004 len = eval_fname_script(*arg);
19005 if (len > 0)
19006 {
19007 /* literal "<SID>", "s:" or "<SNR>" */
19008 *arg += len;
19009 }
19010
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019012 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019014 p = find_name_end(*arg, &expr_start, &expr_end,
19015 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 if (expr_start != NULL)
19017 {
19018 char_u *temp_string;
19019
19020 if (!evaluate)
19021 {
19022 len += (int)(p - *arg);
19023 *arg = skipwhite(p);
19024 return len;
19025 }
19026
19027 /*
19028 * Include any <SID> etc in the expanded string:
19029 * Thus the -len here.
19030 */
19031 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19032 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019033 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 *alias = temp_string;
19035 *arg = skipwhite(p);
19036 return (int)STRLEN(temp_string);
19037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038
19039 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019040 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 EMSG2(_(e_invexpr2), *arg);
19042
19043 return len;
19044}
19045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019046/*
19047 * Find the end of a variable or function name, taking care of magic braces.
19048 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19049 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019050 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051 * Return a pointer to just after the name. Equal to "arg" if there is no
19052 * valid name.
19053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019055find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 char_u *arg;
19057 char_u **expr_start;
19058 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019059 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019061 int mb_nest = 0;
19062 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 char_u *p;
19064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019065 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019067 *expr_start = NULL;
19068 *expr_end = NULL;
19069 }
19070
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019071 /* Quick check for valid starting character. */
19072 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19073 return arg;
19074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019075 for (p = arg; *p != NUL
19076 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019077 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019078 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019079 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019080 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019082 if (*p == '\'')
19083 {
19084 /* skip over 'string' to avoid counting [ and ] inside it. */
19085 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19086 ;
19087 if (*p == NUL)
19088 break;
19089 }
19090 else if (*p == '"')
19091 {
19092 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19093 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19094 if (*p == '\\' && p[1] != NUL)
19095 ++p;
19096 if (*p == NUL)
19097 break;
19098 }
19099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019100 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019102 if (*p == '[')
19103 ++br_nest;
19104 else if (*p == ']')
19105 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019108 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019110 if (*p == '{')
19111 {
19112 mb_nest++;
19113 if (expr_start != NULL && *expr_start == NULL)
19114 *expr_start = p;
19115 }
19116 else if (*p == '}')
19117 {
19118 mb_nest--;
19119 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19120 *expr_end = p;
19121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 }
19124
19125 return p;
19126}
19127
19128/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019129 * Expands out the 'magic' {}'s in a variable/function name.
19130 * Note that this can call itself recursively, to deal with
19131 * constructs like foo{bar}{baz}{bam}
19132 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19133 * "in_start" ^
19134 * "expr_start" ^
19135 * "expr_end" ^
19136 * "in_end" ^
19137 *
19138 * Returns a new allocated string, which the caller must free.
19139 * Returns NULL for failure.
19140 */
19141 static char_u *
19142make_expanded_name(in_start, expr_start, expr_end, in_end)
19143 char_u *in_start;
19144 char_u *expr_start;
19145 char_u *expr_end;
19146 char_u *in_end;
19147{
19148 char_u c1;
19149 char_u *retval = NULL;
19150 char_u *temp_result;
19151 char_u *nextcmd = NULL;
19152
19153 if (expr_end == NULL || in_end == NULL)
19154 return NULL;
19155 *expr_start = NUL;
19156 *expr_end = NUL;
19157 c1 = *in_end;
19158 *in_end = NUL;
19159
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019160 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019161 if (temp_result != NULL && nextcmd == NULL)
19162 {
19163 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19164 + (in_end - expr_end) + 1));
19165 if (retval != NULL)
19166 {
19167 STRCPY(retval, in_start);
19168 STRCAT(retval, temp_result);
19169 STRCAT(retval, expr_end + 1);
19170 }
19171 }
19172 vim_free(temp_result);
19173
19174 *in_end = c1; /* put char back for error messages */
19175 *expr_start = '{';
19176 *expr_end = '}';
19177
19178 if (retval != NULL)
19179 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019180 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019181 if (expr_start != NULL)
19182 {
19183 /* Further expansion! */
19184 temp_result = make_expanded_name(retval, expr_start,
19185 expr_end, temp_result);
19186 vim_free(retval);
19187 retval = temp_result;
19188 }
19189 }
19190
19191 return retval;
19192}
19193
19194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019196 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 */
19198 static int
19199eval_isnamec(c)
19200 int c;
19201{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019202 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19203}
19204
19205/*
19206 * Return TRUE if character "c" can be used as the first character in a
19207 * variable or function name (excluding '{' and '}').
19208 */
19209 static int
19210eval_isnamec1(c)
19211 int c;
19212{
19213 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214}
19215
19216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 * Set number v: variable to "val".
19218 */
19219 void
19220set_vim_var_nr(idx, val)
19221 int idx;
19222 long val;
19223{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019224 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225}
19226
19227/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019228 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 */
19230 long
19231get_vim_var_nr(idx)
19232 int idx;
19233{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019234 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235}
19236
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019237/*
19238 * Get string v: variable value. Uses a static buffer, can only be used once.
19239 */
19240 char_u *
19241get_vim_var_str(idx)
19242 int idx;
19243{
19244 return get_tv_string(&vimvars[idx].vv_tv);
19245}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019246
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019248 * Get List v: variable value. Caller must take care of reference count when
19249 * needed.
19250 */
19251 list_T *
19252get_vim_var_list(idx)
19253 int idx;
19254{
19255 return vimvars[idx].vv_list;
19256}
19257
19258/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019259 * Set v:char to character "c".
19260 */
19261 void
19262set_vim_var_char(c)
19263 int c;
19264{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019265 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019266
19267#ifdef FEAT_MBYTE
19268 if (has_mbyte)
19269 buf[(*mb_char2bytes)(c, buf)] = NUL;
19270 else
19271#endif
19272 {
19273 buf[0] = c;
19274 buf[1] = NUL;
19275 }
19276 set_vim_var_string(VV_CHAR, buf, -1);
19277}
19278
19279/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019280 * Set v:count to "count" and v:count1 to "count1".
19281 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282 */
19283 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019284set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 long count;
19286 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019287 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019289 if (set_prevcount)
19290 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019291 vimvars[VV_COUNT].vv_nr = count;
19292 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293}
19294
19295/*
19296 * Set string v: variable to a copy of "val".
19297 */
19298 void
19299set_vim_var_string(idx, val, len)
19300 int idx;
19301 char_u *val;
19302 int len; /* length of "val" to use or -1 (whole string) */
19303{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019304 /* Need to do this (at least) once, since we can't initialize a union.
19305 * Will always be invoked when "v:progname" is set. */
19306 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19307
Bram Moolenaare9a41262005-01-15 22:18:47 +000019308 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019310 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019312 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019314 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315}
19316
19317/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019318 * Set List v: variable to "val".
19319 */
19320 void
19321set_vim_var_list(idx, val)
19322 int idx;
19323 list_T *val;
19324{
19325 list_unref(vimvars[idx].vv_list);
19326 vimvars[idx].vv_list = val;
19327 if (val != NULL)
19328 ++val->lv_refcount;
19329}
19330
19331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 * Set v:register if needed.
19333 */
19334 void
19335set_reg_var(c)
19336 int c;
19337{
19338 char_u regname;
19339
19340 if (c == 0 || c == ' ')
19341 regname = '"';
19342 else
19343 regname = c;
19344 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019345 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 set_vim_var_string(VV_REG, &regname, 1);
19347}
19348
19349/*
19350 * Get or set v:exception. If "oldval" == NULL, return the current value.
19351 * Otherwise, restore the value to "oldval" and return NULL.
19352 * Must always be called in pairs to save and restore v:exception! Does not
19353 * take care of memory allocations.
19354 */
19355 char_u *
19356v_exception(oldval)
19357 char_u *oldval;
19358{
19359 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019360 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361
Bram Moolenaare9a41262005-01-15 22:18:47 +000019362 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 return NULL;
19364}
19365
19366/*
19367 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19368 * Otherwise, restore the value to "oldval" and return NULL.
19369 * Must always be called in pairs to save and restore v:throwpoint! Does not
19370 * take care of memory allocations.
19371 */
19372 char_u *
19373v_throwpoint(oldval)
19374 char_u *oldval;
19375{
19376 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019377 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378
Bram Moolenaare9a41262005-01-15 22:18:47 +000019379 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 return NULL;
19381}
19382
19383#if defined(FEAT_AUTOCMD) || defined(PROTO)
19384/*
19385 * Set v:cmdarg.
19386 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19387 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19388 * Must always be called in pairs!
19389 */
19390 char_u *
19391set_cmdarg(eap, oldarg)
19392 exarg_T *eap;
19393 char_u *oldarg;
19394{
19395 char_u *oldval;
19396 char_u *newval;
19397 unsigned len;
19398
Bram Moolenaare9a41262005-01-15 22:18:47 +000019399 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019400 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019402 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019403 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019404 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 }
19406
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019407 if (eap->force_bin == FORCE_BIN)
19408 len = 6;
19409 else if (eap->force_bin == FORCE_NOBIN)
19410 len = 8;
19411 else
19412 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019413
19414 if (eap->read_edit)
19415 len += 7;
19416
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019417 if (eap->force_ff != 0)
19418 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19419# ifdef FEAT_MBYTE
19420 if (eap->force_enc != 0)
19421 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019422 if (eap->bad_char != 0)
19423 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019424# endif
19425
19426 newval = alloc(len + 1);
19427 if (newval == NULL)
19428 return NULL;
19429
19430 if (eap->force_bin == FORCE_BIN)
19431 sprintf((char *)newval, " ++bin");
19432 else if (eap->force_bin == FORCE_NOBIN)
19433 sprintf((char *)newval, " ++nobin");
19434 else
19435 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019436
19437 if (eap->read_edit)
19438 STRCAT(newval, " ++edit");
19439
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019440 if (eap->force_ff != 0)
19441 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19442 eap->cmd + eap->force_ff);
19443# ifdef FEAT_MBYTE
19444 if (eap->force_enc != 0)
19445 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19446 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019447 if (eap->bad_char == BAD_KEEP)
19448 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19449 else if (eap->bad_char == BAD_DROP)
19450 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19451 else if (eap->bad_char != 0)
19452 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019453# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019454 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019455 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456}
19457#endif
19458
19459/*
19460 * Get the value of internal variable "name".
19461 * Return OK or FAIL.
19462 */
19463 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019464get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 char_u *name;
19466 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019467 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019468 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469{
19470 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019471 typval_T *tv = NULL;
19472 typval_T atv;
19473 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475
19476 /* truncate the name, so that we can use strcmp() */
19477 cc = name[len];
19478 name[len] = NUL;
19479
19480 /*
19481 * Check for "b:changedtick".
19482 */
19483 if (STRCMP(name, "b:changedtick") == 0)
19484 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019485 atv.v_type = VAR_NUMBER;
19486 atv.vval.v_number = curbuf->b_changedtick;
19487 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 }
19489
19490 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 * Check for user-defined variables.
19492 */
19493 else
19494 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019495 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019497 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 }
19499
Bram Moolenaare9a41262005-01-15 22:18:47 +000019500 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019502 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019503 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 ret = FAIL;
19505 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019506 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019507 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508
19509 name[len] = cc;
19510
19511 return ret;
19512}
19513
19514/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019515 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19516 * Also handle function call with Funcref variable: func(expr)
19517 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19518 */
19519 static int
19520handle_subscript(arg, rettv, evaluate, verbose)
19521 char_u **arg;
19522 typval_T *rettv;
19523 int evaluate; /* do more than finding the end */
19524 int verbose; /* give error messages */
19525{
19526 int ret = OK;
19527 dict_T *selfdict = NULL;
19528 char_u *s;
19529 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019530 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019531
19532 while (ret == OK
19533 && (**arg == '['
19534 || (**arg == '.' && rettv->v_type == VAR_DICT)
19535 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19536 && !vim_iswhite(*(*arg - 1)))
19537 {
19538 if (**arg == '(')
19539 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019540 /* need to copy the funcref so that we can clear rettv */
19541 functv = *rettv;
19542 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019543
19544 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019545 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019546 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019547 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19548 &len, evaluate, selfdict);
19549
19550 /* Clear the funcref afterwards, so that deleting it while
19551 * evaluating the arguments is possible (see test55). */
19552 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019553
19554 /* Stop the expression evaluation when immediately aborting on
19555 * error, or when an interrupt occurred or an exception was thrown
19556 * but not caught. */
19557 if (aborting())
19558 {
19559 if (ret == OK)
19560 clear_tv(rettv);
19561 ret = FAIL;
19562 }
19563 dict_unref(selfdict);
19564 selfdict = NULL;
19565 }
19566 else /* **arg == '[' || **arg == '.' */
19567 {
19568 dict_unref(selfdict);
19569 if (rettv->v_type == VAR_DICT)
19570 {
19571 selfdict = rettv->vval.v_dict;
19572 if (selfdict != NULL)
19573 ++selfdict->dv_refcount;
19574 }
19575 else
19576 selfdict = NULL;
19577 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19578 {
19579 clear_tv(rettv);
19580 ret = FAIL;
19581 }
19582 }
19583 }
19584 dict_unref(selfdict);
19585 return ret;
19586}
19587
19588/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019589 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019590 * value).
19591 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019592 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019593alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019594{
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019596}
19597
19598/*
19599 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600 * The string "s" must have been allocated, it is consumed.
19601 * Return NULL for out of memory, the variable otherwise.
19602 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019604alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 char_u *s;
19606{
Bram Moolenaar33570922005-01-25 22:26:29 +000019607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 rettv = alloc_tv();
19610 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 rettv->v_type = VAR_STRING;
19613 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 }
19615 else
19616 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019617 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618}
19619
19620/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019621 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019623 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626{
19627 if (varp != NULL)
19628 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019629 switch (varp->v_type)
19630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019631 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019632 func_unref(varp->vval.v_string);
19633 /*FALLTHROUGH*/
19634 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019635 vim_free(varp->vval.v_string);
19636 break;
19637 case VAR_LIST:
19638 list_unref(varp->vval.v_list);
19639 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019640 case VAR_DICT:
19641 dict_unref(varp->vval.v_dict);
19642 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019643 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019644#ifdef FEAT_FLOAT
19645 case VAR_FLOAT:
19646#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019647 case VAR_UNKNOWN:
19648 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019649 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019650 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019651 break;
19652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 vim_free(varp);
19654 }
19655}
19656
19657/*
19658 * Free the memory for a variable value and set the value to NULL or 0.
19659 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019660 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019661clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663{
19664 if (varp != NULL)
19665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019666 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019668 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019669 func_unref(varp->vval.v_string);
19670 /*FALLTHROUGH*/
19671 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019672 vim_free(varp->vval.v_string);
19673 varp->vval.v_string = NULL;
19674 break;
19675 case VAR_LIST:
19676 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019677 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019678 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019679 case VAR_DICT:
19680 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019681 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019682 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019684 varp->vval.v_number = 0;
19685 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019686#ifdef FEAT_FLOAT
19687 case VAR_FLOAT:
19688 varp->vval.v_float = 0.0;
19689 break;
19690#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691 case VAR_UNKNOWN:
19692 break;
19693 default:
19694 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019696 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 }
19698}
19699
19700/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019701 * Set the value of a variable to NULL without freeing items.
19702 */
19703 static void
19704init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019705 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019706{
19707 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019709}
19710
19711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 * Get the number value of a variable.
19713 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019714 * For incompatible types, return 0.
19715 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19716 * caller of incompatible types: it sets *denote to TRUE if "denote"
19717 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 */
19719 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019721 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019723 int error = FALSE;
19724
19725 return get_tv_number_chk(varp, &error); /* return 0L on error */
19726}
19727
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019728 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019729get_tv_number_chk(varp, denote)
19730 typval_T *varp;
19731 int *denote;
19732{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019733 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019735 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019737 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019738 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019739#ifdef FEAT_FLOAT
19740 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019741 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019742 break;
19743#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019744 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019745 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019746 break;
19747 case VAR_STRING:
19748 if (varp->vval.v_string != NULL)
19749 vim_str2nr(varp->vval.v_string, NULL, NULL,
19750 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019751 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019752 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019753 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019754 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019755 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019756 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019757 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019758 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019759 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019760 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019762 if (denote == NULL) /* useful for values that must be unsigned */
19763 n = -1;
19764 else
19765 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019766 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767}
19768
19769/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019770 * Get the lnum from the first argument.
19771 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019772 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 */
19774 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019775get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019776 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777{
Bram Moolenaar33570922005-01-25 22:26:29 +000019778 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 linenr_T lnum;
19780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019781 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 if (lnum == 0) /* no valid number, try using line() */
19783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784 rettv.v_type = VAR_NUMBER;
19785 f_line(argvars, &rettv);
19786 lnum = rettv.vval.v_number;
19787 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 }
19789 return lnum;
19790}
19791
19792/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019793 * Get the lnum from the first argument.
19794 * Also accepts "$", then "buf" is used.
19795 * Returns 0 on error.
19796 */
19797 static linenr_T
19798get_tv_lnum_buf(argvars, buf)
19799 typval_T *argvars;
19800 buf_T *buf;
19801{
19802 if (argvars[0].v_type == VAR_STRING
19803 && argvars[0].vval.v_string != NULL
19804 && argvars[0].vval.v_string[0] == '$'
19805 && buf != NULL)
19806 return buf->b_ml.ml_line_count;
19807 return get_tv_number_chk(&argvars[0], NULL);
19808}
19809
19810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 * Get the string value of a variable.
19812 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019813 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19814 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 * If the String variable has never been set, return an empty string.
19816 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019817 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19818 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 */
19820 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019821get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019822 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823{
19824 static char_u mybuf[NUMBUFLEN];
19825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827}
19828
19829 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019831 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019832 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019834 char_u *res = get_tv_string_buf_chk(varp, buf);
19835
19836 return res != NULL ? res : (char_u *)"";
19837}
19838
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019839 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019840get_tv_string_chk(varp)
19841 typval_T *varp;
19842{
19843 static char_u mybuf[NUMBUFLEN];
19844
19845 return get_tv_string_buf_chk(varp, mybuf);
19846}
19847
19848 static char_u *
19849get_tv_string_buf_chk(varp, buf)
19850 typval_T *varp;
19851 char_u *buf;
19852{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019853 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019855 case VAR_NUMBER:
19856 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19857 return buf;
19858 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019859 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019860 break;
19861 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019862 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019863 break;
19864 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019865 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019866 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019867#ifdef FEAT_FLOAT
19868 case VAR_FLOAT:
19869 EMSG(_("E806: using Float as a String"));
19870 break;
19871#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019872 case VAR_STRING:
19873 if (varp->vval.v_string != NULL)
19874 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019875 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019876 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019877 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019880 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881}
19882
19883/*
19884 * Find variable "name" in the list of variables.
19885 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019886 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019887 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019888 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019890 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019891find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019893 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019896 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897
Bram Moolenaara7043832005-01-21 11:56:39 +000019898 ht = find_var_ht(name, &varname);
19899 if (htp != NULL)
19900 *htp = ht;
19901 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019903 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904}
19905
19906/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019907 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019908 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019910 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019911find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019912 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019913 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019914 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019915{
Bram Moolenaar33570922005-01-25 22:26:29 +000019916 hashitem_T *hi;
19917
19918 if (*varname == NUL)
19919 {
19920 /* Must be something like "s:", otherwise "ht" would be NULL. */
19921 switch (varname[-2])
19922 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019923 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019924 case 'g': return &globvars_var;
19925 case 'v': return &vimvars_var;
19926 case 'b': return &curbuf->b_bufvar;
19927 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019928#ifdef FEAT_WINDOWS
19929 case 't': return &curtab->tp_winvar;
19930#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019931 case 'l': return current_funccal == NULL
19932 ? NULL : &current_funccal->l_vars_var;
19933 case 'a': return current_funccal == NULL
19934 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019935 }
19936 return NULL;
19937 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019938
19939 hi = hash_find(ht, varname);
19940 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019941 {
19942 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019943 * worked find the variable again. Don't auto-load a script if it was
19944 * loaded already, otherwise it would be loaded every time when
19945 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019946 if (ht == &globvarht && !writing)
19947 {
19948 /* Note: script_autoload() may make "hi" invalid. It must either
19949 * be obtained again or not used. */
19950 if (!script_autoload(varname, FALSE) || aborting())
19951 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019952 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010019953 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019954 if (HASHITEM_EMPTY(hi))
19955 return NULL;
19956 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019957 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019958}
19959
19960/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019961 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019962 * Set "varname" to the start of name without ':'.
19963 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019964 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019965find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 char_u *name;
19967 char_u **varname;
19968{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019969 hashitem_T *hi;
19970
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 if (name[1] != ':')
19972 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019973 /* The name must not start with a colon or #. */
19974 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 return NULL;
19976 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019977
19978 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019979 hi = hash_find(&compat_hashtab, name);
19980 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019981 return &compat_hashtab;
19982
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019984 return &globvarht; /* global variable */
19985 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 }
19987 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019988 if (*name == 'g') /* global variable */
19989 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019990 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19991 */
19992 if (vim_strchr(name + 2, ':') != NULL
19993 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019994 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019996 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019998 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019999#ifdef FEAT_WINDOWS
20000 if (*name == 't') /* tab page variable */
20001 return &curtab->tp_vars.dv_hashtab;
20002#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020003 if (*name == 'v') /* v: variable */
20004 return &vimvarht;
20005 if (*name == 'a' && current_funccal != NULL) /* function argument */
20006 return &current_funccal->l_avars.dv_hashtab;
20007 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20008 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 if (*name == 's' /* script variable */
20010 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20011 return &SCRIPT_VARS(current_SID);
20012 return NULL;
20013}
20014
20015/*
20016 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020017 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 * Returns NULL when it doesn't exist.
20019 */
20020 char_u *
20021get_var_value(name)
20022 char_u *name;
20023{
Bram Moolenaar33570922005-01-25 22:26:29 +000020024 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025
Bram Moolenaara7043832005-01-21 11:56:39 +000020026 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 if (v == NULL)
20028 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020029 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030}
20031
20032/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020033 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 * sourcing this script and when executing functions defined in the script.
20035 */
20036 void
20037new_script_vars(id)
20038 scid_T id;
20039{
Bram Moolenaara7043832005-01-21 11:56:39 +000020040 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 hashtab_T *ht;
20042 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020043
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20045 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020046 /* Re-allocating ga_data means that an ht_array pointing to
20047 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020048 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020049 for (i = 1; i <= ga_scripts.ga_len; ++i)
20050 {
20051 ht = &SCRIPT_VARS(i);
20052 if (ht->ht_mask == HT_INIT_SIZE - 1)
20053 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020054 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020056 }
20057
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 while (ga_scripts.ga_len < id)
20059 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020060 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020061 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020062 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 }
20065 }
20066}
20067
20068/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020069 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20070 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 */
20072 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020073init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 dict_T *dict;
20075 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020076 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077{
Bram Moolenaar33570922005-01-25 22:26:29 +000020078 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020079 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020080 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020081 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020082 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020083 dict_var->di_tv.vval.v_dict = dict;
20084 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020085 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020086 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20087 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088}
20089
20090/*
20091 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020092 * Frees all allocated variables and the value they contain.
20093 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 */
20095 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020096vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 hashtab_T *ht;
20098{
20099 vars_clear_ext(ht, TRUE);
20100}
20101
20102/*
20103 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20104 */
20105 static void
20106vars_clear_ext(ht, free_val)
20107 hashtab_T *ht;
20108 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109{
Bram Moolenaara7043832005-01-21 11:56:39 +000020110 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 hashitem_T *hi;
20112 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020115 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020116 for (hi = ht->ht_array; todo > 0; ++hi)
20117 {
20118 if (!HASHITEM_EMPTY(hi))
20119 {
20120 --todo;
20121
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020123 * ht_array might change then. hash_clear() takes care of it
20124 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020125 v = HI2DI(hi);
20126 if (free_val)
20127 clear_tv(&v->di_tv);
20128 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20129 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020130 }
20131 }
20132 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020133 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134}
20135
Bram Moolenaara7043832005-01-21 11:56:39 +000020136/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020137 * Delete a variable from hashtab "ht" at item "hi".
20138 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020141delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 hashtab_T *ht;
20143 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144{
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020146
20147 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 clear_tv(&di->di_tv);
20149 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150}
20151
20152/*
20153 * List the value of one internal variable.
20154 */
20155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020156list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020157 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020159 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020161 char_u *tofree;
20162 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020163 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020164
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020165 current_copyID += COPYID_INC;
20166 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020167 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020168 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020169 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170}
20171
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020173list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 char_u *prefix;
20175 char_u *name;
20176 int type;
20177 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020178 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179{
Bram Moolenaar31859182007-08-14 20:41:13 +000020180 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20181 msg_start();
20182 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 if (name != NULL) /* "a:" vars don't have a name stored */
20184 msg_puts(name);
20185 msg_putchar(' ');
20186 msg_advance(22);
20187 if (type == VAR_NUMBER)
20188 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020189 else if (type == VAR_FUNC)
20190 msg_putchar('*');
20191 else if (type == VAR_LIST)
20192 {
20193 msg_putchar('[');
20194 if (*string == '[')
20195 ++string;
20196 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020197 else if (type == VAR_DICT)
20198 {
20199 msg_putchar('{');
20200 if (*string == '{')
20201 ++string;
20202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 else
20204 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020205
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020207
20208 if (type == VAR_FUNC)
20209 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020210 if (*first)
20211 {
20212 msg_clr_eos();
20213 *first = FALSE;
20214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215}
20216
20217/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 * If the variable already exists, the value is updated.
20220 * Otherwise the variable is created.
20221 */
20222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020223set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020225 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227{
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020232 ht = find_var_ht(name, &varname);
20233 if (ht == NULL || *varname == NUL)
20234 {
20235 EMSG2(_(e_illvar), name);
20236 return;
20237 }
20238 v = find_var_in_ht(ht, varname, TRUE);
20239
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020240 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20241 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020242
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020246 if (var_check_ro(v->di_flags, name)
20247 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020248 return;
20249 if (v->di_tv.v_type != tv->v_type
20250 && !((v->di_tv.v_type == VAR_STRING
20251 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020252 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020253 || tv->v_type == VAR_NUMBER))
20254#ifdef FEAT_FLOAT
20255 && !((v->di_tv.v_type == VAR_NUMBER
20256 || v->di_tv.v_type == VAR_FLOAT)
20257 && (tv->v_type == VAR_NUMBER
20258 || tv->v_type == VAR_FLOAT))
20259#endif
20260 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020261 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020262 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020263 return;
20264 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020265
20266 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020267 * Handle setting internal v: variables separately: we don't change
20268 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 */
20270 if (ht == &vimvarht)
20271 {
20272 if (v->di_tv.v_type == VAR_STRING)
20273 {
20274 vim_free(v->di_tv.vval.v_string);
20275 if (copy || tv->v_type != VAR_STRING)
20276 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20277 else
20278 {
20279 /* Take over the string to avoid an extra alloc/free. */
20280 v->di_tv.vval.v_string = tv->vval.v_string;
20281 tv->vval.v_string = NULL;
20282 }
20283 }
20284 else if (v->di_tv.v_type != VAR_NUMBER)
20285 EMSG2(_(e_intern2), "set_var()");
20286 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020287 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020288 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020289 if (STRCMP(varname, "searchforward") == 0)
20290 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20291 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 return;
20293 }
20294
20295 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 }
20297 else /* add a new variable */
20298 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020299 /* Can't add "v:" variable. */
20300 if (ht == &vimvarht)
20301 {
20302 EMSG2(_(e_illvar), name);
20303 return;
20304 }
20305
Bram Moolenaar92124a32005-06-17 22:03:40 +000020306 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020307 if (!valid_varname(varname))
20308 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020309
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020310 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20311 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020312 if (v == NULL)
20313 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020314 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020317 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 return;
20319 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020320 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020322
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020323 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020325 else
20326 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020327 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020328 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020329 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331}
20332
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020333/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020334 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020335 * Also give an error message.
20336 */
20337 static int
20338var_check_ro(flags, name)
20339 int flags;
20340 char_u *name;
20341{
20342 if (flags & DI_FLAGS_RO)
20343 {
20344 EMSG2(_(e_readonlyvar), name);
20345 return TRUE;
20346 }
20347 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20348 {
20349 EMSG2(_(e_readonlysbx), name);
20350 return TRUE;
20351 }
20352 return FALSE;
20353}
20354
20355/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020356 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20357 * Also give an error message.
20358 */
20359 static int
20360var_check_fixed(flags, name)
20361 int flags;
20362 char_u *name;
20363{
20364 if (flags & DI_FLAGS_FIX)
20365 {
20366 EMSG2(_("E795: Cannot delete variable %s"), name);
20367 return TRUE;
20368 }
20369 return FALSE;
20370}
20371
20372/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020373 * Check if a funcref is assigned to a valid variable name.
20374 * Return TRUE and give an error if not.
20375 */
20376 static int
20377var_check_func_name(name, new_var)
20378 char_u *name; /* points to start of variable name */
20379 int new_var; /* TRUE when creating the variable */
20380{
20381 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20382 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20383 ? name[2] : name[0]))
20384 {
20385 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20386 name);
20387 return TRUE;
20388 }
20389 /* Don't allow hiding a function. When "v" is not NULL we might be
20390 * assigning another function to the same var, the type is checked
20391 * below. */
20392 if (new_var && function_exists(name))
20393 {
20394 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20395 name);
20396 return TRUE;
20397 }
20398 return FALSE;
20399}
20400
20401/*
20402 * Check if a variable name is valid.
20403 * Return FALSE and give an error if not.
20404 */
20405 static int
20406valid_varname(varname)
20407 char_u *varname;
20408{
20409 char_u *p;
20410
20411 for (p = varname; *p != NUL; ++p)
20412 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20413 && *p != AUTOLOAD_CHAR)
20414 {
20415 EMSG2(_(e_illvar), varname);
20416 return FALSE;
20417 }
20418 return TRUE;
20419}
20420
20421/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020422 * Return TRUE if typeval "tv" is set to be locked (immutable).
20423 * Also give an error message, using "name".
20424 */
20425 static int
20426tv_check_lock(lock, name)
20427 int lock;
20428 char_u *name;
20429{
20430 if (lock & VAR_LOCKED)
20431 {
20432 EMSG2(_("E741: Value is locked: %s"),
20433 name == NULL ? (char_u *)_("Unknown") : name);
20434 return TRUE;
20435 }
20436 if (lock & VAR_FIXED)
20437 {
20438 EMSG2(_("E742: Cannot change value of %s"),
20439 name == NULL ? (char_u *)_("Unknown") : name);
20440 return TRUE;
20441 }
20442 return FALSE;
20443}
20444
20445/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020446 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020447 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020448 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020449 * It is OK for "from" and "to" to point to the same item. This is used to
20450 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020451 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020452 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020453copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 typval_T *from;
20455 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020457 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020458 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020459 switch (from->v_type)
20460 {
20461 case VAR_NUMBER:
20462 to->vval.v_number = from->vval.v_number;
20463 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020464#ifdef FEAT_FLOAT
20465 case VAR_FLOAT:
20466 to->vval.v_float = from->vval.v_float;
20467 break;
20468#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020469 case VAR_STRING:
20470 case VAR_FUNC:
20471 if (from->vval.v_string == NULL)
20472 to->vval.v_string = NULL;
20473 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020474 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020475 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020476 if (from->v_type == VAR_FUNC)
20477 func_ref(to->vval.v_string);
20478 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020479 break;
20480 case VAR_LIST:
20481 if (from->vval.v_list == NULL)
20482 to->vval.v_list = NULL;
20483 else
20484 {
20485 to->vval.v_list = from->vval.v_list;
20486 ++to->vval.v_list->lv_refcount;
20487 }
20488 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020489 case VAR_DICT:
20490 if (from->vval.v_dict == NULL)
20491 to->vval.v_dict = NULL;
20492 else
20493 {
20494 to->vval.v_dict = from->vval.v_dict;
20495 ++to->vval.v_dict->dv_refcount;
20496 }
20497 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020498 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020499 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020500 break;
20501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502}
20503
20504/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020505 * Make a copy of an item.
20506 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020507 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20508 * reference to an already copied list/dict can be used.
20509 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020510 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020511 static int
20512item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020513 typval_T *from;
20514 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020515 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020516 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020517{
20518 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020519 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020520
Bram Moolenaar33570922005-01-25 22:26:29 +000020521 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020522 {
20523 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020524 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020525 }
20526 ++recurse;
20527
20528 switch (from->v_type)
20529 {
20530 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020531#ifdef FEAT_FLOAT
20532 case VAR_FLOAT:
20533#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020534 case VAR_STRING:
20535 case VAR_FUNC:
20536 copy_tv(from, to);
20537 break;
20538 case VAR_LIST:
20539 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020540 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020541 if (from->vval.v_list == NULL)
20542 to->vval.v_list = NULL;
20543 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20544 {
20545 /* use the copy made earlier */
20546 to->vval.v_list = from->vval.v_list->lv_copylist;
20547 ++to->vval.v_list->lv_refcount;
20548 }
20549 else
20550 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20551 if (to->vval.v_list == NULL)
20552 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020553 break;
20554 case VAR_DICT:
20555 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020556 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020557 if (from->vval.v_dict == NULL)
20558 to->vval.v_dict = NULL;
20559 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20560 {
20561 /* use the copy made earlier */
20562 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20563 ++to->vval.v_dict->dv_refcount;
20564 }
20565 else
20566 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20567 if (to->vval.v_dict == NULL)
20568 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020569 break;
20570 default:
20571 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020572 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020573 }
20574 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020575 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020576}
20577
20578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 * ":echo expr1 ..." print each argument separated with a space, add a
20580 * newline at the end.
20581 * ":echon expr1 ..." print each argument plain.
20582 */
20583 void
20584ex_echo(eap)
20585 exarg_T *eap;
20586{
20587 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020588 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020589 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 char_u *p;
20591 int needclr = TRUE;
20592 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020593 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594
20595 if (eap->skip)
20596 ++emsg_skip;
20597 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20598 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020599 /* If eval1() causes an error message the text from the command may
20600 * still need to be cleared. E.g., "echo 22,44". */
20601 need_clr_eos = needclr;
20602
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020604 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 {
20606 /*
20607 * Report the invalid expression unless the expression evaluation
20608 * has been cancelled due to an aborting error, an interrupt, or an
20609 * exception.
20610 */
20611 if (!aborting())
20612 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020613 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 break;
20615 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020616 need_clr_eos = FALSE;
20617
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 if (!eap->skip)
20619 {
20620 if (atstart)
20621 {
20622 atstart = FALSE;
20623 /* Call msg_start() after eval1(), evaluating the expression
20624 * may cause a message to appear. */
20625 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020626 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020627 /* Mark the saved text as finishing the line, so that what
20628 * follows is displayed on a new line when scrolling back
20629 * at the more prompt. */
20630 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 }
20634 else if (eap->cmdidx == CMD_echo)
20635 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020636 current_copyID += COPYID_INC;
20637 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020638 if (p != NULL)
20639 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020641 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020643 if (*p != TAB && needclr)
20644 {
20645 /* remove any text still there from the command */
20646 msg_clr_eos();
20647 needclr = FALSE;
20648 }
20649 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 }
20651 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020652 {
20653#ifdef FEAT_MBYTE
20654 if (has_mbyte)
20655 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020656 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020657
20658 (void)msg_outtrans_len_attr(p, i, echo_attr);
20659 p += i - 1;
20660 }
20661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020663 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020666 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020668 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 arg = skipwhite(arg);
20670 }
20671 eap->nextcmd = check_nextcmd(arg);
20672
20673 if (eap->skip)
20674 --emsg_skip;
20675 else
20676 {
20677 /* remove text that may still be there from the command */
20678 if (needclr)
20679 msg_clr_eos();
20680 if (eap->cmdidx == CMD_echo)
20681 msg_end();
20682 }
20683}
20684
20685/*
20686 * ":echohl {name}".
20687 */
20688 void
20689ex_echohl(eap)
20690 exarg_T *eap;
20691{
20692 int id;
20693
20694 id = syn_name2id(eap->arg);
20695 if (id == 0)
20696 echo_attr = 0;
20697 else
20698 echo_attr = syn_id2attr(id);
20699}
20700
20701/*
20702 * ":execute expr1 ..." execute the result of an expression.
20703 * ":echomsg expr1 ..." Print a message
20704 * ":echoerr expr1 ..." Print an error
20705 * Each gets spaces around each argument and a newline at the end for
20706 * echo commands
20707 */
20708 void
20709ex_execute(eap)
20710 exarg_T *eap;
20711{
20712 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020713 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 int ret = OK;
20715 char_u *p;
20716 garray_T ga;
20717 int len;
20718 int save_did_emsg;
20719
20720 ga_init2(&ga, 1, 80);
20721
20722 if (eap->skip)
20723 ++emsg_skip;
20724 while (*arg != NUL && *arg != '|' && *arg != '\n')
20725 {
20726 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020727 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 {
20729 /*
20730 * Report the invalid expression unless the expression evaluation
20731 * has been cancelled due to an aborting error, an interrupt, or an
20732 * exception.
20733 */
20734 if (!aborting())
20735 EMSG2(_(e_invexpr2), p);
20736 ret = FAIL;
20737 break;
20738 }
20739
20740 if (!eap->skip)
20741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 len = (int)STRLEN(p);
20744 if (ga_grow(&ga, len + 2) == FAIL)
20745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020746 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 ret = FAIL;
20748 break;
20749 }
20750 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 ga.ga_len += len;
20754 }
20755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020756 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 arg = skipwhite(arg);
20758 }
20759
20760 if (ret != FAIL && ga.ga_data != NULL)
20761 {
20762 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020765 out_flush();
20766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 else if (eap->cmdidx == CMD_echoerr)
20768 {
20769 /* We don't want to abort following commands, restore did_emsg. */
20770 save_did_emsg = did_emsg;
20771 EMSG((char_u *)ga.ga_data);
20772 if (!force_abort)
20773 did_emsg = save_did_emsg;
20774 }
20775 else if (eap->cmdidx == CMD_execute)
20776 do_cmdline((char_u *)ga.ga_data,
20777 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20778 }
20779
20780 ga_clear(&ga);
20781
20782 if (eap->skip)
20783 --emsg_skip;
20784
20785 eap->nextcmd = check_nextcmd(arg);
20786}
20787
20788/*
20789 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20790 * "arg" points to the "&" or '+' when called, to "option" when returning.
20791 * Returns NULL when no option name found. Otherwise pointer to the char
20792 * after the option name.
20793 */
20794 static char_u *
20795find_option_end(arg, opt_flags)
20796 char_u **arg;
20797 int *opt_flags;
20798{
20799 char_u *p = *arg;
20800
20801 ++p;
20802 if (*p == 'g' && p[1] == ':')
20803 {
20804 *opt_flags = OPT_GLOBAL;
20805 p += 2;
20806 }
20807 else if (*p == 'l' && p[1] == ':')
20808 {
20809 *opt_flags = OPT_LOCAL;
20810 p += 2;
20811 }
20812 else
20813 *opt_flags = 0;
20814
20815 if (!ASCII_ISALPHA(*p))
20816 return NULL;
20817 *arg = p;
20818
20819 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20820 p += 4; /* termcap option */
20821 else
20822 while (ASCII_ISALPHA(*p))
20823 ++p;
20824 return p;
20825}
20826
20827/*
20828 * ":function"
20829 */
20830 void
20831ex_function(eap)
20832 exarg_T *eap;
20833{
20834 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020835 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 int j;
20837 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 char_u *name = NULL;
20840 char_u *p;
20841 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020842 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 garray_T newargs;
20844 garray_T newlines;
20845 int varargs = FALSE;
20846 int mustend = FALSE;
20847 int flags = 0;
20848 ufunc_T *fp;
20849 int indent;
20850 int nesting;
20851 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020852 dictitem_T *v;
20853 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020854 static int func_nr = 0; /* number for nameless function */
20855 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020856 hashtab_T *ht;
20857 int todo;
20858 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020859 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860
20861 /*
20862 * ":function" without argument: list functions.
20863 */
20864 if (ends_excmd(*eap->arg))
20865 {
20866 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020867 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020868 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020869 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020870 {
20871 if (!HASHITEM_EMPTY(hi))
20872 {
20873 --todo;
20874 fp = HI2UF(hi);
20875 if (!isdigit(*fp->uf_name))
20876 list_func_head(fp, FALSE);
20877 }
20878 }
20879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 eap->nextcmd = check_nextcmd(eap->arg);
20881 return;
20882 }
20883
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020884 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020885 * ":function /pat": list functions matching pattern.
20886 */
20887 if (*eap->arg == '/')
20888 {
20889 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20890 if (!eap->skip)
20891 {
20892 regmatch_T regmatch;
20893
20894 c = *p;
20895 *p = NUL;
20896 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20897 *p = c;
20898 if (regmatch.regprog != NULL)
20899 {
20900 regmatch.rm_ic = p_ic;
20901
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020902 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020903 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20904 {
20905 if (!HASHITEM_EMPTY(hi))
20906 {
20907 --todo;
20908 fp = HI2UF(hi);
20909 if (!isdigit(*fp->uf_name)
20910 && vim_regexec(&regmatch, fp->uf_name, 0))
20911 list_func_head(fp, FALSE);
20912 }
20913 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020914 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020915 }
20916 }
20917 if (*p == '/')
20918 ++p;
20919 eap->nextcmd = check_nextcmd(p);
20920 return;
20921 }
20922
20923 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020924 * Get the function name. There are these situations:
20925 * func normal function name
20926 * "name" == func, "fudi.fd_dict" == NULL
20927 * dict.func new dictionary entry
20928 * "name" == NULL, "fudi.fd_dict" set,
20929 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20930 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020931 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020932 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20933 * dict.func existing dict entry that's not a Funcref
20934 * "name" == NULL, "fudi.fd_dict" set,
20935 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020938 name = trans_function_name(&p, eap->skip, 0, &fudi);
20939 paren = (vim_strchr(p, '(') != NULL);
20940 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 {
20942 /*
20943 * Return on an invalid expression in braces, unless the expression
20944 * evaluation has been cancelled due to an aborting error, an
20945 * interrupt, or an exception.
20946 */
20947 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020949 if (!eap->skip && fudi.fd_newkey != NULL)
20950 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020951 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 else
20955 eap->skip = TRUE;
20956 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020957
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 /* An error in a function call during evaluation of an expression in magic
20959 * braces should not cause the function not to be defined. */
20960 saved_did_emsg = did_emsg;
20961 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962
20963 /*
20964 * ":function func" with only function name: list function.
20965 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020966 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 {
20968 if (!ends_excmd(*skipwhite(p)))
20969 {
20970 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020971 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 }
20973 eap->nextcmd = check_nextcmd(p);
20974 if (eap->nextcmd != NULL)
20975 *p = NUL;
20976 if (!eap->skip && !got_int)
20977 {
20978 fp = find_func(name);
20979 if (fp != NULL)
20980 {
20981 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020982 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020984 if (FUNCLINE(fp, j) == NULL)
20985 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 msg_putchar('\n');
20987 msg_outnum((long)(j + 1));
20988 if (j < 9)
20989 msg_putchar(' ');
20990 if (j < 99)
20991 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020992 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 out_flush(); /* show a line at a time */
20994 ui_breakcheck();
20995 }
20996 if (!got_int)
20997 {
20998 msg_putchar('\n');
20999 msg_puts((char_u *)" endfunction");
21000 }
21001 }
21002 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021003 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021005 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 }
21007
21008 /*
21009 * ":function name(arg1, arg2)" Define function.
21010 */
21011 p = skipwhite(p);
21012 if (*p != '(')
21013 {
21014 if (!eap->skip)
21015 {
21016 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021017 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 }
21019 /* attempt to continue by skipping some text */
21020 if (vim_strchr(p, '(') != NULL)
21021 p = vim_strchr(p, '(');
21022 }
21023 p = skipwhite(p + 1);
21024
21025 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21026 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21027
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021028 if (!eap->skip)
21029 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021030 /* Check the name of the function. Unless it's a dictionary function
21031 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021032 if (name != NULL)
21033 arg = name;
21034 else
21035 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021036 if (arg != NULL && (fudi.fd_di == NULL
21037 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021038 {
21039 if (*arg == K_SPECIAL)
21040 j = 3;
21041 else
21042 j = 0;
21043 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21044 : eval_isnamec(arg[j])))
21045 ++j;
21046 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021047 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021048 }
21049 }
21050
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 /*
21052 * Isolate the arguments: "arg1, arg2, ...)"
21053 */
21054 while (*p != ')')
21055 {
21056 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21057 {
21058 varargs = TRUE;
21059 p += 3;
21060 mustend = TRUE;
21061 }
21062 else
21063 {
21064 arg = p;
21065 while (ASCII_ISALNUM(*p) || *p == '_')
21066 ++p;
21067 if (arg == p || isdigit(*arg)
21068 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21069 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21070 {
21071 if (!eap->skip)
21072 EMSG2(_("E125: Illegal argument: %s"), arg);
21073 break;
21074 }
21075 if (ga_grow(&newargs, 1) == FAIL)
21076 goto erret;
21077 c = *p;
21078 *p = NUL;
21079 arg = vim_strsave(arg);
21080 if (arg == NULL)
21081 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021082
21083 /* Check for duplicate argument name. */
21084 for (i = 0; i < newargs.ga_len; ++i)
21085 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21086 {
21087 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21088 goto erret;
21089 }
21090
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21092 *p = c;
21093 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 if (*p == ',')
21095 ++p;
21096 else
21097 mustend = TRUE;
21098 }
21099 p = skipwhite(p);
21100 if (mustend && *p != ')')
21101 {
21102 if (!eap->skip)
21103 EMSG2(_(e_invarg2), eap->arg);
21104 break;
21105 }
21106 }
21107 ++p; /* skip the ')' */
21108
Bram Moolenaare9a41262005-01-15 22:18:47 +000021109 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 for (;;)
21111 {
21112 p = skipwhite(p);
21113 if (STRNCMP(p, "range", 5) == 0)
21114 {
21115 flags |= FC_RANGE;
21116 p += 5;
21117 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021118 else if (STRNCMP(p, "dict", 4) == 0)
21119 {
21120 flags |= FC_DICT;
21121 p += 4;
21122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 else if (STRNCMP(p, "abort", 5) == 0)
21124 {
21125 flags |= FC_ABORT;
21126 p += 5;
21127 }
21128 else
21129 break;
21130 }
21131
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021132 /* When there is a line break use what follows for the function body.
21133 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21134 if (*p == '\n')
21135 line_arg = p + 1;
21136 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 EMSG(_(e_trailing));
21138
21139 /*
21140 * Read the body of the function, until ":endfunction" is found.
21141 */
21142 if (KeyTyped)
21143 {
21144 /* Check if the function already exists, don't let the user type the
21145 * whole function before telling him it doesn't work! For a script we
21146 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021147 if (!eap->skip && !eap->forceit)
21148 {
21149 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21150 EMSG(_(e_funcdict));
21151 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021152 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021155 if (!eap->skip && did_emsg)
21156 goto erret;
21157
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 msg_putchar('\n'); /* don't overwrite the function name */
21159 cmdline_row = msg_row;
21160 }
21161
21162 indent = 2;
21163 nesting = 0;
21164 for (;;)
21165 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021166 if (KeyTyped)
21167 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021169 sourcing_lnum_off = sourcing_lnum;
21170
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021171 if (line_arg != NULL)
21172 {
21173 /* Use eap->arg, split up in parts by line breaks. */
21174 theline = line_arg;
21175 p = vim_strchr(theline, '\n');
21176 if (p == NULL)
21177 line_arg += STRLEN(line_arg);
21178 else
21179 {
21180 *p = NUL;
21181 line_arg = p + 1;
21182 }
21183 }
21184 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 theline = getcmdline(':', 0L, indent);
21186 else
21187 theline = eap->getline(':', eap->cookie, indent);
21188 if (KeyTyped)
21189 lines_left = Rows - 1;
21190 if (theline == NULL)
21191 {
21192 EMSG(_("E126: Missing :endfunction"));
21193 goto erret;
21194 }
21195
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021196 /* Detect line continuation: sourcing_lnum increased more than one. */
21197 if (sourcing_lnum > sourcing_lnum_off + 1)
21198 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21199 else
21200 sourcing_lnum_off = 0;
21201
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 if (skip_until != NULL)
21203 {
21204 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21205 * don't check for ":endfunc". */
21206 if (STRCMP(theline, skip_until) == 0)
21207 {
21208 vim_free(skip_until);
21209 skip_until = NULL;
21210 }
21211 }
21212 else
21213 {
21214 /* skip ':' and blanks*/
21215 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21216 ;
21217
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021218 /* Check for "endfunction". */
21219 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021221 if (line_arg == NULL)
21222 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 break;
21224 }
21225
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021226 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 * at "end". */
21228 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21229 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021230 else if (STRNCMP(p, "if", 2) == 0
21231 || STRNCMP(p, "wh", 2) == 0
21232 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 || STRNCMP(p, "try", 3) == 0)
21234 indent += 2;
21235
21236 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021237 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021239 if (*p == '!')
21240 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 p += eval_fname_script(p);
21242 if (ASCII_ISALPHA(*p))
21243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021244 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 if (*skipwhite(p) == '(')
21246 {
21247 ++nesting;
21248 indent += 2;
21249 }
21250 }
21251 }
21252
21253 /* Check for ":append" or ":insert". */
21254 p = skip_range(p, NULL);
21255 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21256 || (p[0] == 'i'
21257 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21258 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21259 skip_until = vim_strsave((char_u *)".");
21260
21261 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21262 arg = skipwhite(skiptowhite(p));
21263 if (arg[0] == '<' && arg[1] =='<'
21264 && ((p[0] == 'p' && p[1] == 'y'
21265 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21266 || (p[0] == 'p' && p[1] == 'e'
21267 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21268 || (p[0] == 't' && p[1] == 'c'
21269 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021270 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21271 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21273 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021274 || (p[0] == 'm' && p[1] == 'z'
21275 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 ))
21277 {
21278 /* ":python <<" continues until a dot, like ":append" */
21279 p = skipwhite(arg + 2);
21280 if (*p == NUL)
21281 skip_until = vim_strsave((char_u *)".");
21282 else
21283 skip_until = vim_strsave(p);
21284 }
21285 }
21286
21287 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021288 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021289 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021290 if (line_arg == NULL)
21291 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021293 }
21294
21295 /* Copy the line to newly allocated memory. get_one_sourceline()
21296 * allocates 250 bytes per line, this saves 80% on average. The cost
21297 * is an extra alloc/free. */
21298 p = vim_strsave(theline);
21299 if (p != NULL)
21300 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021301 if (line_arg == NULL)
21302 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021303 theline = p;
21304 }
21305
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021306 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21307
21308 /* Add NULL lines for continuation lines, so that the line count is
21309 * equal to the index in the growarray. */
21310 while (sourcing_lnum_off-- > 0)
21311 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021312
21313 /* Check for end of eap->arg. */
21314 if (line_arg != NULL && *line_arg == NUL)
21315 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 }
21317
21318 /* Don't define the function when skipping commands or when an error was
21319 * detected. */
21320 if (eap->skip || did_emsg)
21321 goto erret;
21322
21323 /*
21324 * If there are no errors, add the function
21325 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021326 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021327 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021328 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021329 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021330 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021331 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021332 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021333 goto erret;
21334 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021335
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021336 fp = find_func(name);
21337 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021339 if (!eap->forceit)
21340 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021341 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021342 goto erret;
21343 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021344 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021345 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021346 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021347 name);
21348 goto erret;
21349 }
21350 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021351 ga_clear_strings(&(fp->uf_args));
21352 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021353 vim_free(name);
21354 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 }
21357 else
21358 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021359 char numbuf[20];
21360
21361 fp = NULL;
21362 if (fudi.fd_newkey == NULL && !eap->forceit)
21363 {
21364 EMSG(_(e_funcdict));
21365 goto erret;
21366 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021367 if (fudi.fd_di == NULL)
21368 {
21369 /* Can't add a function to a locked dictionary */
21370 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21371 goto erret;
21372 }
21373 /* Can't change an existing function if it is locked */
21374 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21375 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021376
21377 /* Give the function a sequential number. Can only be used with a
21378 * Funcref! */
21379 vim_free(name);
21380 sprintf(numbuf, "%d", ++func_nr);
21381 name = vim_strsave((char_u *)numbuf);
21382 if (name == NULL)
21383 goto erret;
21384 }
21385
21386 if (fp == NULL)
21387 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021388 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021389 {
21390 int slen, plen;
21391 char_u *scriptname;
21392
21393 /* Check that the autoload name matches the script name. */
21394 j = FAIL;
21395 if (sourcing_name != NULL)
21396 {
21397 scriptname = autoload_name(name);
21398 if (scriptname != NULL)
21399 {
21400 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021401 plen = (int)STRLEN(p);
21402 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021403 if (slen > plen && fnamecmp(p,
21404 sourcing_name + slen - plen) == 0)
21405 j = OK;
21406 vim_free(scriptname);
21407 }
21408 }
21409 if (j == FAIL)
21410 {
21411 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21412 goto erret;
21413 }
21414 }
21415
21416 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 if (fp == NULL)
21418 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021419
21420 if (fudi.fd_dict != NULL)
21421 {
21422 if (fudi.fd_di == NULL)
21423 {
21424 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021425 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021426 if (fudi.fd_di == NULL)
21427 {
21428 vim_free(fp);
21429 goto erret;
21430 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021431 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21432 {
21433 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021434 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021435 goto erret;
21436 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021437 }
21438 else
21439 /* overwrite existing dict entry */
21440 clear_tv(&fudi.fd_di->di_tv);
21441 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021442 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021443 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021444 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021445
21446 /* behave like "dict" was used */
21447 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021448 }
21449
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021451 STRCPY(fp->uf_name, name);
21452 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021454 fp->uf_args = newargs;
21455 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021456#ifdef FEAT_PROFILE
21457 fp->uf_tml_count = NULL;
21458 fp->uf_tml_total = NULL;
21459 fp->uf_tml_self = NULL;
21460 fp->uf_profiling = FALSE;
21461 if (prof_def_func())
21462 func_do_profile(fp);
21463#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021464 fp->uf_varargs = varargs;
21465 fp->uf_flags = flags;
21466 fp->uf_calls = 0;
21467 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021468 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469
21470erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 ga_clear_strings(&newargs);
21472 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021473ret_free:
21474 vim_free(skip_until);
21475 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478}
21479
21480/*
21481 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021482 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021484 * flags:
21485 * TFN_INT: internal function name OK
21486 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 * Advances "pp" to just after the function name (if no error).
21488 */
21489 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021490trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 char_u **pp;
21492 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021493 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021496 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 char_u *start;
21498 char_u *end;
21499 int lead;
21500 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021502 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021503
21504 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021507
21508 /* Check for hard coded <SNR>: already translated function ID (from a user
21509 * command). */
21510 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21511 && (*pp)[2] == (int)KE_SNR)
21512 {
21513 *pp += 3;
21514 len = get_id_len(pp) + 3;
21515 return vim_strnsave(start, len);
21516 }
21517
21518 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21519 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021521 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021523
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021524 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21525 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021526 if (end == start)
21527 {
21528 if (!skip)
21529 EMSG(_("E129: Function name required"));
21530 goto theend;
21531 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021532 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021533 {
21534 /*
21535 * Report an invalid expression in braces, unless the expression
21536 * evaluation has been cancelled due to an aborting error, an
21537 * interrupt, or an exception.
21538 */
21539 if (!aborting())
21540 {
21541 if (end != NULL)
21542 EMSG2(_(e_invarg2), start);
21543 }
21544 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021545 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021546 goto theend;
21547 }
21548
21549 if (lv.ll_tv != NULL)
21550 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021551 if (fdp != NULL)
21552 {
21553 fdp->fd_dict = lv.ll_dict;
21554 fdp->fd_newkey = lv.ll_newkey;
21555 lv.ll_newkey = NULL;
21556 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021557 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021558 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21559 {
21560 name = vim_strsave(lv.ll_tv->vval.v_string);
21561 *pp = end;
21562 }
21563 else
21564 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021565 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21566 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021567 EMSG(_(e_funcref));
21568 else
21569 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021570 name = NULL;
21571 }
21572 goto theend;
21573 }
21574
21575 if (lv.ll_name == NULL)
21576 {
21577 /* Error found, but continue after the function name. */
21578 *pp = end;
21579 goto theend;
21580 }
21581
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021582 /* Check if the name is a Funcref. If so, use the value. */
21583 if (lv.ll_exp_name != NULL)
21584 {
21585 len = (int)STRLEN(lv.ll_exp_name);
21586 name = deref_func_name(lv.ll_exp_name, &len);
21587 if (name == lv.ll_exp_name)
21588 name = NULL;
21589 }
21590 else
21591 {
21592 len = (int)(end - *pp);
21593 name = deref_func_name(*pp, &len);
21594 if (name == *pp)
21595 name = NULL;
21596 }
21597 if (name != NULL)
21598 {
21599 name = vim_strsave(name);
21600 *pp = end;
21601 goto theend;
21602 }
21603
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021604 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021605 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021606 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021607 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21608 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21609 {
21610 /* When there was "s:" already or the name expanded to get a
21611 * leading "s:" then remove it. */
21612 lv.ll_name += 2;
21613 len -= 2;
21614 lead = 2;
21615 }
21616 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021617 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021618 {
21619 if (lead == 2) /* skip over "s:" */
21620 lv.ll_name += 2;
21621 len = (int)(end - lv.ll_name);
21622 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021623
21624 /*
21625 * Copy the function name to allocated memory.
21626 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21627 * Accept <SNR>123_name() outside a script.
21628 */
21629 if (skip)
21630 lead = 0; /* do nothing */
21631 else if (lead > 0)
21632 {
21633 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021634 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21635 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021636 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021637 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021638 if (current_SID <= 0)
21639 {
21640 EMSG(_(e_usingsid));
21641 goto theend;
21642 }
21643 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21644 lead += (int)STRLEN(sid_buf);
21645 }
21646 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021647 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021648 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021649 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021650 goto theend;
21651 }
21652 name = alloc((unsigned)(len + lead + 1));
21653 if (name != NULL)
21654 {
21655 if (lead > 0)
21656 {
21657 name[0] = K_SPECIAL;
21658 name[1] = KS_EXTRA;
21659 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021660 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021661 STRCPY(name + 3, sid_buf);
21662 }
21663 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21664 name[len + lead] = NUL;
21665 }
21666 *pp = end;
21667
21668theend:
21669 clear_lval(&lv);
21670 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671}
21672
21673/*
21674 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21675 * Return 2 if "p" starts with "s:".
21676 * Return 0 otherwise.
21677 */
21678 static int
21679eval_fname_script(p)
21680 char_u *p;
21681{
21682 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21683 || STRNICMP(p + 1, "SNR>", 4) == 0))
21684 return 5;
21685 if (p[0] == 's' && p[1] == ':')
21686 return 2;
21687 return 0;
21688}
21689
21690/*
21691 * Return TRUE if "p" starts with "<SID>" or "s:".
21692 * Only works if eval_fname_script() returned non-zero for "p"!
21693 */
21694 static int
21695eval_fname_sid(p)
21696 char_u *p;
21697{
21698 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21699}
21700
21701/*
21702 * List the head of the function: "name(arg1, arg2)".
21703 */
21704 static void
21705list_func_head(fp, indent)
21706 ufunc_T *fp;
21707 int indent;
21708{
21709 int j;
21710
21711 msg_start();
21712 if (indent)
21713 MSG_PUTS(" ");
21714 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021715 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 {
21717 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021718 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 }
21720 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021721 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021723 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 {
21725 if (j)
21726 MSG_PUTS(", ");
21727 msg_puts(FUNCARG(fp, j));
21728 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021729 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 {
21731 if (j)
21732 MSG_PUTS(", ");
21733 MSG_PUTS("...");
21734 }
21735 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021736 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021737 if (p_verbose > 0)
21738 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739}
21740
21741/*
21742 * Find a function by name, return pointer to it in ufuncs.
21743 * Return NULL for unknown function.
21744 */
21745 static ufunc_T *
21746find_func(name)
21747 char_u *name;
21748{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021749 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021751 hi = hash_find(&func_hashtab, name);
21752 if (!HASHITEM_EMPTY(hi))
21753 return HI2UF(hi);
21754 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755}
21756
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021757#if defined(EXITFREE) || defined(PROTO)
21758 void
21759free_all_functions()
21760{
21761 hashitem_T *hi;
21762
21763 /* Need to start all over every time, because func_free() may change the
21764 * hash table. */
21765 while (func_hashtab.ht_used > 0)
21766 for (hi = func_hashtab.ht_array; ; ++hi)
21767 if (!HASHITEM_EMPTY(hi))
21768 {
21769 func_free(HI2UF(hi));
21770 break;
21771 }
21772}
21773#endif
21774
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021775/*
21776 * Return TRUE if a function "name" exists.
21777 */
21778 static int
21779function_exists(name)
21780 char_u *name;
21781{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021782 char_u *nm = name;
21783 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021784 int n = FALSE;
21785
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021786 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021787 nm = skipwhite(nm);
21788
21789 /* Only accept "funcname", "funcname ", "funcname (..." and
21790 * "funcname(...", not "funcname!...". */
21791 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021792 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021793 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021794 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021795 else
21796 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021797 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021798 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021799 return n;
21800}
21801
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021802/*
21803 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021804 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021805 */
21806 static int
21807builtin_function(name)
21808 char_u *name;
21809{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021810 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21811 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021812}
21813
Bram Moolenaar05159a02005-02-26 23:04:13 +000021814#if defined(FEAT_PROFILE) || defined(PROTO)
21815/*
21816 * Start profiling function "fp".
21817 */
21818 static void
21819func_do_profile(fp)
21820 ufunc_T *fp;
21821{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021822 int len = fp->uf_lines.ga_len;
21823
21824 if (len == 0)
21825 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021826 fp->uf_tm_count = 0;
21827 profile_zero(&fp->uf_tm_self);
21828 profile_zero(&fp->uf_tm_total);
21829 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021830 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021831 if (fp->uf_tml_total == NULL)
21832 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021833 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021834 if (fp->uf_tml_self == NULL)
21835 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021836 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021837 fp->uf_tml_idx = -1;
21838 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21839 || fp->uf_tml_self == NULL)
21840 return; /* out of memory */
21841
21842 fp->uf_profiling = TRUE;
21843}
21844
21845/*
21846 * Dump the profiling results for all functions in file "fd".
21847 */
21848 void
21849func_dump_profile(fd)
21850 FILE *fd;
21851{
21852 hashitem_T *hi;
21853 int todo;
21854 ufunc_T *fp;
21855 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021856 ufunc_T **sorttab;
21857 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021858
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021859 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021860 if (todo == 0)
21861 return; /* nothing to dump */
21862
Bram Moolenaar73830342005-02-28 22:48:19 +000021863 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21864
Bram Moolenaar05159a02005-02-26 23:04:13 +000021865 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21866 {
21867 if (!HASHITEM_EMPTY(hi))
21868 {
21869 --todo;
21870 fp = HI2UF(hi);
21871 if (fp->uf_profiling)
21872 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021873 if (sorttab != NULL)
21874 sorttab[st_len++] = fp;
21875
Bram Moolenaar05159a02005-02-26 23:04:13 +000021876 if (fp->uf_name[0] == K_SPECIAL)
21877 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21878 else
21879 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21880 if (fp->uf_tm_count == 1)
21881 fprintf(fd, "Called 1 time\n");
21882 else
21883 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21884 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21885 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21886 fprintf(fd, "\n");
21887 fprintf(fd, "count total (s) self (s)\n");
21888
21889 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21890 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021891 if (FUNCLINE(fp, i) == NULL)
21892 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021893 prof_func_line(fd, fp->uf_tml_count[i],
21894 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021895 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21896 }
21897 fprintf(fd, "\n");
21898 }
21899 }
21900 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021901
21902 if (sorttab != NULL && st_len > 0)
21903 {
21904 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21905 prof_total_cmp);
21906 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21907 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21908 prof_self_cmp);
21909 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21910 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021911
21912 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021913}
Bram Moolenaar73830342005-02-28 22:48:19 +000021914
21915 static void
21916prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21917 FILE *fd;
21918 ufunc_T **sorttab;
21919 int st_len;
21920 char *title;
21921 int prefer_self; /* when equal print only self time */
21922{
21923 int i;
21924 ufunc_T *fp;
21925
21926 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21927 fprintf(fd, "count total (s) self (s) function\n");
21928 for (i = 0; i < 20 && i < st_len; ++i)
21929 {
21930 fp = sorttab[i];
21931 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21932 prefer_self);
21933 if (fp->uf_name[0] == K_SPECIAL)
21934 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21935 else
21936 fprintf(fd, " %s()\n", fp->uf_name);
21937 }
21938 fprintf(fd, "\n");
21939}
21940
21941/*
21942 * Print the count and times for one function or function line.
21943 */
21944 static void
21945prof_func_line(fd, count, total, self, prefer_self)
21946 FILE *fd;
21947 int count;
21948 proftime_T *total;
21949 proftime_T *self;
21950 int prefer_self; /* when equal print only self time */
21951{
21952 if (count > 0)
21953 {
21954 fprintf(fd, "%5d ", count);
21955 if (prefer_self && profile_equal(total, self))
21956 fprintf(fd, " ");
21957 else
21958 fprintf(fd, "%s ", profile_msg(total));
21959 if (!prefer_self && profile_equal(total, self))
21960 fprintf(fd, " ");
21961 else
21962 fprintf(fd, "%s ", profile_msg(self));
21963 }
21964 else
21965 fprintf(fd, " ");
21966}
21967
21968/*
21969 * Compare function for total time sorting.
21970 */
21971 static int
21972#ifdef __BORLANDC__
21973_RTLENTRYF
21974#endif
21975prof_total_cmp(s1, s2)
21976 const void *s1;
21977 const void *s2;
21978{
21979 ufunc_T *p1, *p2;
21980
21981 p1 = *(ufunc_T **)s1;
21982 p2 = *(ufunc_T **)s2;
21983 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21984}
21985
21986/*
21987 * Compare function for self time sorting.
21988 */
21989 static int
21990#ifdef __BORLANDC__
21991_RTLENTRYF
21992#endif
21993prof_self_cmp(s1, s2)
21994 const void *s1;
21995 const void *s2;
21996{
21997 ufunc_T *p1, *p2;
21998
21999 p1 = *(ufunc_T **)s1;
22000 p2 = *(ufunc_T **)s2;
22001 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22002}
22003
Bram Moolenaar05159a02005-02-26 23:04:13 +000022004#endif
22005
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022006/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022007 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022008 * Return TRUE if a package was loaded.
22009 */
22010 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022011script_autoload(name, reload)
22012 char_u *name;
22013 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022014{
22015 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022016 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022017 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022018 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022019
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022020 /* Return quickly when autoload disabled. */
22021 if (no_autoload)
22022 return FALSE;
22023
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022024 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022025 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022026 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022027 return FALSE;
22028
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022029 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022030
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022031 /* Find the name in the list of previously loaded package names. Skip
22032 * "autoload/", it's always the same. */
22033 for (i = 0; i < ga_loaded.ga_len; ++i)
22034 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22035 break;
22036 if (!reload && i < ga_loaded.ga_len)
22037 ret = FALSE; /* was loaded already */
22038 else
22039 {
22040 /* Remember the name if it wasn't loaded already. */
22041 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22042 {
22043 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22044 tofree = NULL;
22045 }
22046
22047 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022048 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022049 ret = TRUE;
22050 }
22051
22052 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022053 return ret;
22054}
22055
22056/*
22057 * Return the autoload script name for a function or variable name.
22058 * Returns NULL when out of memory.
22059 */
22060 static char_u *
22061autoload_name(name)
22062 char_u *name;
22063{
22064 char_u *p;
22065 char_u *scriptname;
22066
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022067 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022068 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22069 if (scriptname == NULL)
22070 return FALSE;
22071 STRCPY(scriptname, "autoload/");
22072 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022073 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022074 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022075 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022076 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022077 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022078}
22079
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22081
22082/*
22083 * Function given to ExpandGeneric() to obtain the list of user defined
22084 * function names.
22085 */
22086 char_u *
22087get_user_func_name(xp, idx)
22088 expand_T *xp;
22089 int idx;
22090{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022091 static long_u done;
22092 static hashitem_T *hi;
22093 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094
22095 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022097 done = 0;
22098 hi = func_hashtab.ht_array;
22099 }
22100 if (done < func_hashtab.ht_used)
22101 {
22102 if (done++ > 0)
22103 ++hi;
22104 while (HASHITEM_EMPTY(hi))
22105 ++hi;
22106 fp = HI2UF(hi);
22107
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022108 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022109 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022110
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022111 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22112 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113
22114 cat_func_name(IObuff, fp);
22115 if (xp->xp_context != EXPAND_USER_FUNC)
22116 {
22117 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022118 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 STRCAT(IObuff, ")");
22120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121 return IObuff;
22122 }
22123 return NULL;
22124}
22125
22126#endif /* FEAT_CMDL_COMPL */
22127
22128/*
22129 * Copy the function name of "fp" to buffer "buf".
22130 * "buf" must be able to hold the function name plus three bytes.
22131 * Takes care of script-local function names.
22132 */
22133 static void
22134cat_func_name(buf, fp)
22135 char_u *buf;
22136 ufunc_T *fp;
22137{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022138 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 {
22140 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022141 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142 }
22143 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022144 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145}
22146
22147/*
22148 * ":delfunction {name}"
22149 */
22150 void
22151ex_delfunction(eap)
22152 exarg_T *eap;
22153{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022154 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 char_u *p;
22156 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022157 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158
22159 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022160 name = trans_function_name(&p, eap->skip, 0, &fudi);
22161 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022163 {
22164 if (fudi.fd_dict != NULL && !eap->skip)
22165 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 if (!ends_excmd(*skipwhite(p)))
22169 {
22170 vim_free(name);
22171 EMSG(_(e_trailing));
22172 return;
22173 }
22174 eap->nextcmd = check_nextcmd(p);
22175 if (eap->nextcmd != NULL)
22176 *p = NUL;
22177
22178 if (!eap->skip)
22179 fp = find_func(name);
22180 vim_free(name);
22181
22182 if (!eap->skip)
22183 {
22184 if (fp == NULL)
22185 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022186 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 return;
22188 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022189 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190 {
22191 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22192 return;
22193 }
22194
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022195 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022197 /* Delete the dict item that refers to the function, it will
22198 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022199 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022201 else
22202 func_free(fp);
22203 }
22204}
22205
22206/*
22207 * Free a function and remove it from the list of functions.
22208 */
22209 static void
22210func_free(fp)
22211 ufunc_T *fp;
22212{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022213 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022214
22215 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022216 ga_clear_strings(&(fp->uf_args));
22217 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022218#ifdef FEAT_PROFILE
22219 vim_free(fp->uf_tml_count);
22220 vim_free(fp->uf_tml_total);
22221 vim_free(fp->uf_tml_self);
22222#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022223
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022224 /* remove the function from the function hashtable */
22225 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22226 if (HASHITEM_EMPTY(hi))
22227 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022228 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022229 hash_remove(&func_hashtab, hi);
22230
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022231 vim_free(fp);
22232}
22233
22234/*
22235 * Unreference a Function: decrement the reference count and free it when it
22236 * becomes zero. Only for numbered functions.
22237 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022238 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022239func_unref(name)
22240 char_u *name;
22241{
22242 ufunc_T *fp;
22243
22244 if (name != NULL && isdigit(*name))
22245 {
22246 fp = find_func(name);
22247 if (fp == NULL)
22248 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022249 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022250 {
22251 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022252 * when "uf_calls" becomes zero. */
22253 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022254 func_free(fp);
22255 }
22256 }
22257}
22258
22259/*
22260 * Count a reference to a Function.
22261 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022262 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022263func_ref(name)
22264 char_u *name;
22265{
22266 ufunc_T *fp;
22267
22268 if (name != NULL && isdigit(*name))
22269 {
22270 fp = find_func(name);
22271 if (fp == NULL)
22272 EMSG2(_(e_intern2), "func_ref()");
22273 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022274 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 }
22276}
22277
22278/*
22279 * Call a user function.
22280 */
22281 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022282call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 ufunc_T *fp; /* pointer to function */
22284 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022285 typval_T *argvars; /* arguments */
22286 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 linenr_T firstline; /* first line of range */
22288 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022289 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290{
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 char_u *save_sourcing_name;
22292 linenr_T save_sourcing_lnum;
22293 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022294 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022295 int save_did_emsg;
22296 static int depth = 0;
22297 dictitem_T *v;
22298 int fixvar_idx = 0; /* index in fixvar[] */
22299 int i;
22300 int ai;
22301 char_u numbuf[NUMBUFLEN];
22302 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022303#ifdef FEAT_PROFILE
22304 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022305 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307
22308 /* If depth of calling is getting too high, don't execute the function */
22309 if (depth >= p_mfd)
22310 {
22311 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022312 rettv->v_type = VAR_NUMBER;
22313 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 return;
22315 }
22316 ++depth;
22317
22318 line_breakcheck(); /* check for CTRL-C hit */
22319
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022320 fc = (funccall_T *)alloc(sizeof(funccall_T));
22321 fc->caller = current_funccal;
22322 current_funccal = fc;
22323 fc->func = fp;
22324 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022325 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022326 fc->linenr = 0;
22327 fc->returned = FALSE;
22328 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022330 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22331 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332
Bram Moolenaar33570922005-01-25 22:26:29 +000022333 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022334 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022335 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22336 * each argument variable and saves a lot of time.
22337 */
22338 /*
22339 * Init l: variables.
22340 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022341 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022342 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022343 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022344 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22345 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022346 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022347 name = v->di_key;
22348 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022349 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022350 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022351 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022352 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022353 v->di_tv.vval.v_dict = selfdict;
22354 ++selfdict->dv_refcount;
22355 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022356
Bram Moolenaar33570922005-01-25 22:26:29 +000022357 /*
22358 * Init a: variables.
22359 * Set a:0 to "argcount".
22360 * Set a:000 to a list with room for the "..." arguments.
22361 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022362 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022363 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022364 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022365 /* Use "name" to avoid a warning from some compiler that checks the
22366 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022367 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022368 name = v->di_key;
22369 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022370 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022371 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022372 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022373 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022374 v->di_tv.vval.v_list = &fc->l_varlist;
22375 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22376 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22377 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022378
22379 /*
22380 * Set a:firstline to "firstline" and a:lastline to "lastline".
22381 * Set a:name to named arguments.
22382 * Set a:N to the "..." arguments.
22383 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022384 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022385 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022386 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022387 (varnumber_T)lastline);
22388 for (i = 0; i < argcount; ++i)
22389 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022390 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022391 if (ai < 0)
22392 /* named argument a:name */
22393 name = FUNCARG(fp, i);
22394 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022395 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022396 /* "..." argument a:1, a:2, etc. */
22397 sprintf((char *)numbuf, "%d", ai + 1);
22398 name = numbuf;
22399 }
22400 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22401 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022402 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22404 }
22405 else
22406 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022407 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22408 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022409 if (v == NULL)
22410 break;
22411 v->di_flags = DI_FLAGS_RO;
22412 }
22413 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022414 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022415
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022416 /* Note: the values are copied directly to avoid alloc/free.
22417 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022418 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022419 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022420
22421 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22422 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022423 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22424 fc->l_listitems[ai].li_tv = argvars[i];
22425 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022426 }
22427 }
22428
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 /* Don't redraw while executing the function. */
22430 ++RedrawingDisabled;
22431 save_sourcing_name = sourcing_name;
22432 save_sourcing_lnum = sourcing_lnum;
22433 sourcing_lnum = 1;
22434 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022435 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 if (sourcing_name != NULL)
22437 {
22438 if (save_sourcing_name != NULL
22439 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22440 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22441 else
22442 STRCPY(sourcing_name, "function ");
22443 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22444
22445 if (p_verbose >= 12)
22446 {
22447 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022448 verbose_enter_scroll();
22449
Bram Moolenaar555b2802005-05-19 21:08:39 +000022450 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 if (p_verbose >= 14)
22452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022454 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022455 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022456 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457
22458 msg_puts((char_u *)"(");
22459 for (i = 0; i < argcount; ++i)
22460 {
22461 if (i > 0)
22462 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022463 if (argvars[i].v_type == VAR_NUMBER)
22464 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 else
22466 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022467 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22468 if (s != NULL)
22469 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022470 if (vim_strsize(s) > MSG_BUF_CLEN)
22471 {
22472 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22473 s = buf;
22474 }
22475 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022476 vim_free(tofree);
22477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 }
22479 }
22480 msg_puts((char_u *)")");
22481 }
22482 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022483
22484 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 --no_wait_return;
22486 }
22487 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022488#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022489 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022490 {
22491 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22492 func_do_profile(fp);
22493 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022494 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022495 {
22496 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022497 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022498 profile_zero(&fp->uf_tm_children);
22499 }
22500 script_prof_save(&wait_start);
22501 }
22502#endif
22503
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022505 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 save_did_emsg = did_emsg;
22507 did_emsg = FALSE;
22508
22509 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022510 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22512
22513 --RedrawingDisabled;
22514
22515 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022516 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022518 clear_tv(rettv);
22519 rettv->v_type = VAR_NUMBER;
22520 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 }
22522
Bram Moolenaar05159a02005-02-26 23:04:13 +000022523#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022524 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022525 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022526 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022527 profile_end(&call_start);
22528 profile_sub_wait(&wait_start, &call_start);
22529 profile_add(&fp->uf_tm_total, &call_start);
22530 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022531 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022532 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022533 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22534 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022535 }
22536 }
22537#endif
22538
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 /* when being verbose, mention the return value */
22540 if (p_verbose >= 12)
22541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022543 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022546 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022547 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022548 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022549 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022552 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022553 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022554 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022555 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022556
Bram Moolenaar555b2802005-05-19 21:08:39 +000022557 /* The value may be very long. Skip the middle part, so that we
22558 * have some idea how it starts and ends. smsg() would always
22559 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022560 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022561 if (s != NULL)
22562 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022563 if (vim_strsize(s) > MSG_BUF_CLEN)
22564 {
22565 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22566 s = buf;
22567 }
22568 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022569 vim_free(tofree);
22570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 }
22572 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022573
22574 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575 --no_wait_return;
22576 }
22577
22578 vim_free(sourcing_name);
22579 sourcing_name = save_sourcing_name;
22580 sourcing_lnum = save_sourcing_lnum;
22581 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022582#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022583 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022584 script_prof_restore(&wait_start);
22585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586
22587 if (p_verbose >= 12 && sourcing_name != NULL)
22588 {
22589 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022590 verbose_enter_scroll();
22591
Bram Moolenaar555b2802005-05-19 21:08:39 +000022592 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022594
22595 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 --no_wait_return;
22597 }
22598
22599 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022600 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022602
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022603 /* If the a:000 list and the l: and a: dicts are not referenced we can
22604 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022605 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22606 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22607 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22608 {
22609 free_funccal(fc, FALSE);
22610 }
22611 else
22612 {
22613 hashitem_T *hi;
22614 listitem_T *li;
22615 int todo;
22616
22617 /* "fc" is still in use. This can happen when returning "a:000" or
22618 * assigning "l:" to a global variable.
22619 * Link "fc" in the list for garbage collection later. */
22620 fc->caller = previous_funccal;
22621 previous_funccal = fc;
22622
22623 /* Make a copy of the a: variables, since we didn't do that above. */
22624 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22625 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22626 {
22627 if (!HASHITEM_EMPTY(hi))
22628 {
22629 --todo;
22630 v = HI2DI(hi);
22631 copy_tv(&v->di_tv, &v->di_tv);
22632 }
22633 }
22634
22635 /* Make a copy of the a:000 items, since we didn't do that above. */
22636 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22637 copy_tv(&li->li_tv, &li->li_tv);
22638 }
22639}
22640
22641/*
22642 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022643 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022644 */
22645 static int
22646can_free_funccal(fc, copyID)
22647 funccall_T *fc;
22648 int copyID;
22649{
22650 return (fc->l_varlist.lv_copyID != copyID
22651 && fc->l_vars.dv_copyID != copyID
22652 && fc->l_avars.dv_copyID != copyID);
22653}
22654
22655/*
22656 * Free "fc" and what it contains.
22657 */
22658 static void
22659free_funccal(fc, free_val)
22660 funccall_T *fc;
22661 int free_val; /* a: vars were allocated */
22662{
22663 listitem_T *li;
22664
22665 /* The a: variables typevals may not have been allocated, only free the
22666 * allocated variables. */
22667 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22668
22669 /* free all l: variables */
22670 vars_clear(&fc->l_vars.dv_hashtab);
22671
22672 /* Free the a:000 variables if they were allocated. */
22673 if (free_val)
22674 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22675 clear_tv(&li->li_tv);
22676
22677 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678}
22679
22680/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022681 * Add a number variable "name" to dict "dp" with value "nr".
22682 */
22683 static void
22684add_nr_var(dp, v, name, nr)
22685 dict_T *dp;
22686 dictitem_T *v;
22687 char *name;
22688 varnumber_T nr;
22689{
22690 STRCPY(v->di_key, name);
22691 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22692 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22693 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022694 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 v->di_tv.vval.v_number = nr;
22696}
22697
22698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 * ":return [expr]"
22700 */
22701 void
22702ex_return(eap)
22703 exarg_T *eap;
22704{
22705 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022706 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 int returning = FALSE;
22708
22709 if (current_funccal == NULL)
22710 {
22711 EMSG(_("E133: :return not inside a function"));
22712 return;
22713 }
22714
22715 if (eap->skip)
22716 ++emsg_skip;
22717
22718 eap->nextcmd = NULL;
22719 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022720 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 {
22722 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022723 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022725 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 }
22727 /* It's safer to return also on error. */
22728 else if (!eap->skip)
22729 {
22730 /*
22731 * Return unless the expression evaluation has been cancelled due to an
22732 * aborting error, an interrupt, or an exception.
22733 */
22734 if (!aborting())
22735 returning = do_return(eap, FALSE, TRUE, NULL);
22736 }
22737
22738 /* When skipping or the return gets pending, advance to the next command
22739 * in this line (!returning). Otherwise, ignore the rest of the line.
22740 * Following lines will be ignored by get_func_line(). */
22741 if (returning)
22742 eap->nextcmd = NULL;
22743 else if (eap->nextcmd == NULL) /* no argument */
22744 eap->nextcmd = check_nextcmd(arg);
22745
22746 if (eap->skip)
22747 --emsg_skip;
22748}
22749
22750/*
22751 * Return from a function. Possibly makes the return pending. Also called
22752 * for a pending return at the ":endtry" or after returning from an extra
22753 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022755 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 * FALSE when the return gets pending.
22757 */
22758 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022759do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 exarg_T *eap;
22761 int reanimate;
22762 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022763 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764{
22765 int idx;
22766 struct condstack *cstack = eap->cstack;
22767
22768 if (reanimate)
22769 /* Undo the return. */
22770 current_funccal->returned = FALSE;
22771
22772 /*
22773 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22774 * not in its finally clause (which then is to be executed next) is found.
22775 * In this case, make the ":return" pending for execution at the ":endtry".
22776 * Otherwise, return normally.
22777 */
22778 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22779 if (idx >= 0)
22780 {
22781 cstack->cs_pending[idx] = CSTP_RETURN;
22782
22783 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022784 /* A pending return again gets pending. "rettv" points to an
22785 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022787 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 else
22789 {
22790 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022791 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022793 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022795 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 {
22797 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022798 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022799 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 else
22801 EMSG(_(e_outofmem));
22802 }
22803 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022804 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805
22806 if (reanimate)
22807 {
22808 /* The pending return value could be overwritten by a ":return"
22809 * without argument in a finally clause; reset the default
22810 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022811 current_funccal->rettv->v_type = VAR_NUMBER;
22812 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813 }
22814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022815 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 }
22817 else
22818 {
22819 current_funccal->returned = TRUE;
22820
22821 /* If the return is carried out now, store the return value. For
22822 * a return immediately after reanimation, the value is already
22823 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022824 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022826 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022827 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022829 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 }
22831 }
22832
22833 return idx < 0;
22834}
22835
22836/*
22837 * Free the variable with a pending return value.
22838 */
22839 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022840discard_pending_return(rettv)
22841 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842{
Bram Moolenaar33570922005-01-25 22:26:29 +000022843 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844}
22845
22846/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022847 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 * is an allocated string. Used by report_pending() for verbose messages.
22849 */
22850 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022851get_return_cmd(rettv)
22852 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022854 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022855 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022856 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022858 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022859 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022860 if (s == NULL)
22861 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022862
22863 STRCPY(IObuff, ":return ");
22864 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22865 if (STRLEN(s) + 8 >= IOSIZE)
22866 STRCPY(IObuff + IOSIZE - 4, "...");
22867 vim_free(tofree);
22868 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869}
22870
22871/*
22872 * Get next function line.
22873 * Called by do_cmdline() to get the next line.
22874 * Returns allocated string, or NULL for end of function.
22875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 char_u *
22877get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022878 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022880 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881{
Bram Moolenaar33570922005-01-25 22:26:29 +000022882 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022883 ufunc_T *fp = fcp->func;
22884 char_u *retval;
22885 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886
22887 /* If breakpoints have been added/deleted need to check for it. */
22888 if (fcp->dbg_tick != debug_tick)
22889 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022890 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 sourcing_lnum);
22892 fcp->dbg_tick = debug_tick;
22893 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022894#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022895 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022896 func_line_end(cookie);
22897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898
Bram Moolenaar05159a02005-02-26 23:04:13 +000022899 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022900 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22901 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 retval = NULL;
22903 else
22904 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022905 /* Skip NULL lines (continuation lines). */
22906 while (fcp->linenr < gap->ga_len
22907 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22908 ++fcp->linenr;
22909 if (fcp->linenr >= gap->ga_len)
22910 retval = NULL;
22911 else
22912 {
22913 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22914 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022915#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022916 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022917 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022918#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 }
22921
22922 /* Did we encounter a breakpoint? */
22923 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22924 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022925 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022927 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 sourcing_lnum);
22929 fcp->dbg_tick = debug_tick;
22930 }
22931
22932 return retval;
22933}
22934
Bram Moolenaar05159a02005-02-26 23:04:13 +000022935#if defined(FEAT_PROFILE) || defined(PROTO)
22936/*
22937 * Called when starting to read a function line.
22938 * "sourcing_lnum" must be correct!
22939 * When skipping lines it may not actually be executed, but we won't find out
22940 * until later and we need to store the time now.
22941 */
22942 void
22943func_line_start(cookie)
22944 void *cookie;
22945{
22946 funccall_T *fcp = (funccall_T *)cookie;
22947 ufunc_T *fp = fcp->func;
22948
22949 if (fp->uf_profiling && sourcing_lnum >= 1
22950 && sourcing_lnum <= fp->uf_lines.ga_len)
22951 {
22952 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022953 /* Skip continuation lines. */
22954 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22955 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022956 fp->uf_tml_execed = FALSE;
22957 profile_start(&fp->uf_tml_start);
22958 profile_zero(&fp->uf_tml_children);
22959 profile_get_wait(&fp->uf_tml_wait);
22960 }
22961}
22962
22963/*
22964 * Called when actually executing a function line.
22965 */
22966 void
22967func_line_exec(cookie)
22968 void *cookie;
22969{
22970 funccall_T *fcp = (funccall_T *)cookie;
22971 ufunc_T *fp = fcp->func;
22972
22973 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22974 fp->uf_tml_execed = TRUE;
22975}
22976
22977/*
22978 * Called when done with a function line.
22979 */
22980 void
22981func_line_end(cookie)
22982 void *cookie;
22983{
22984 funccall_T *fcp = (funccall_T *)cookie;
22985 ufunc_T *fp = fcp->func;
22986
22987 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22988 {
22989 if (fp->uf_tml_execed)
22990 {
22991 ++fp->uf_tml_count[fp->uf_tml_idx];
22992 profile_end(&fp->uf_tml_start);
22993 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022994 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022995 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22996 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022997 }
22998 fp->uf_tml_idx = -1;
22999 }
23000}
23001#endif
23002
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003/*
23004 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023005 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 */
23007 int
23008func_has_ended(cookie)
23009 void *cookie;
23010{
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012
23013 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23014 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023015 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 || fcp->returned);
23017}
23018
23019/*
23020 * return TRUE if cookie indicates a function which "abort"s on errors.
23021 */
23022 int
23023func_has_abort(cookie)
23024 void *cookie;
23025{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023026 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027}
23028
23029#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23030typedef enum
23031{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023032 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23033 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23034 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035} var_flavour_T;
23036
23037static var_flavour_T var_flavour __ARGS((char_u *varname));
23038
23039 static var_flavour_T
23040var_flavour(varname)
23041 char_u *varname;
23042{
23043 char_u *p = varname;
23044
23045 if (ASCII_ISUPPER(*p))
23046 {
23047 while (*(++p))
23048 if (ASCII_ISLOWER(*p))
23049 return VAR_FLAVOUR_SESSION;
23050 return VAR_FLAVOUR_VIMINFO;
23051 }
23052 else
23053 return VAR_FLAVOUR_DEFAULT;
23054}
23055#endif
23056
23057#if defined(FEAT_VIMINFO) || defined(PROTO)
23058/*
23059 * Restore global vars that start with a capital from the viminfo file
23060 */
23061 int
23062read_viminfo_varlist(virp, writing)
23063 vir_T *virp;
23064 int writing;
23065{
23066 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023067 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023068 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069
23070 if (!writing && (find_viminfo_parameter('!') != NULL))
23071 {
23072 tab = vim_strchr(virp->vir_line + 1, '\t');
23073 if (tab != NULL)
23074 {
23075 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023076 switch (*tab)
23077 {
23078 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023079#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023080 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023081#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023082 case 'D': type = VAR_DICT; break;
23083 case 'L': type = VAR_LIST; break;
23084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085
23086 tab = vim_strchr(tab, '\t');
23087 if (tab != NULL)
23088 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023089 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023090 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023091 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023093#ifdef FEAT_FLOAT
23094 else if (type == VAR_FLOAT)
23095 (void)string2float(tab + 1, &tv.vval.v_float);
23096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023098 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023099 if (type == VAR_DICT || type == VAR_LIST)
23100 {
23101 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23102
23103 if (etv == NULL)
23104 /* Failed to parse back the dict or list, use it as a
23105 * string. */
23106 tv.v_type = VAR_STRING;
23107 else
23108 {
23109 vim_free(tv.vval.v_string);
23110 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023111 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023112 }
23113 }
23114
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023115 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023116
23117 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023118 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023119 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23120 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 }
23122 }
23123 }
23124
23125 return viminfo_readline(virp);
23126}
23127
23128/*
23129 * Write global vars that start with a capital to the viminfo file
23130 */
23131 void
23132write_viminfo_varlist(fp)
23133 FILE *fp;
23134{
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 hashitem_T *hi;
23136 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023137 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023138 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023139 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023140 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023141 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142
23143 if (find_viminfo_parameter('!') == NULL)
23144 return;
23145
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023146 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023148 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023149 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023151 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023153 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023154 this_var = HI2DI(hi);
23155 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023156 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023157 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023158 {
23159 case VAR_STRING: s = "STR"; break;
23160 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023161#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023162 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023163#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023164 case VAR_DICT: s = "DIC"; break;
23165 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023166 default: continue;
23167 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023169 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023170 if (p != NULL)
23171 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023172 vim_free(tofree);
23173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 }
23175 }
23176}
23177#endif
23178
23179#if defined(FEAT_SESSION) || defined(PROTO)
23180 int
23181store_session_globals(fd)
23182 FILE *fd;
23183{
Bram Moolenaar33570922005-01-25 22:26:29 +000023184 hashitem_T *hi;
23185 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023186 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 char_u *p, *t;
23188
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023189 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023190 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023192 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023194 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023195 this_var = HI2DI(hi);
23196 if ((this_var->di_tv.v_type == VAR_NUMBER
23197 || this_var->di_tv.v_type == VAR_STRING)
23198 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023199 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023200 /* Escape special characters with a backslash. Turn a LF and
23201 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023202 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023203 (char_u *)"\\\"\n\r");
23204 if (p == NULL) /* out of memory */
23205 break;
23206 for (t = p; *t != NUL; ++t)
23207 if (*t == '\n')
23208 *t = 'n';
23209 else if (*t == '\r')
23210 *t = 'r';
23211 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023212 this_var->di_key,
23213 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23214 : ' ',
23215 p,
23216 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23217 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023218 || put_eol(fd) == FAIL)
23219 {
23220 vim_free(p);
23221 return FAIL;
23222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223 vim_free(p);
23224 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023225#ifdef FEAT_FLOAT
23226 else if (this_var->di_tv.v_type == VAR_FLOAT
23227 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23228 {
23229 float_T f = this_var->di_tv.vval.v_float;
23230 int sign = ' ';
23231
23232 if (f < 0)
23233 {
23234 f = -f;
23235 sign = '-';
23236 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023237 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023238 this_var->di_key, sign, f) < 0)
23239 || put_eol(fd) == FAIL)
23240 return FAIL;
23241 }
23242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 }
23244 }
23245 return OK;
23246}
23247#endif
23248
Bram Moolenaar661b1822005-07-28 22:36:45 +000023249/*
23250 * Display script name where an item was last set.
23251 * Should only be invoked when 'verbose' is non-zero.
23252 */
23253 void
23254last_set_msg(scriptID)
23255 scid_T scriptID;
23256{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023257 char_u *p;
23258
Bram Moolenaar661b1822005-07-28 22:36:45 +000023259 if (scriptID != 0)
23260 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023261 p = home_replace_save(NULL, get_scriptname(scriptID));
23262 if (p != NULL)
23263 {
23264 verbose_enter();
23265 MSG_PUTS(_("\n\tLast set from "));
23266 MSG_PUTS(p);
23267 vim_free(p);
23268 verbose_leave();
23269 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023270 }
23271}
23272
Bram Moolenaard812df62008-11-09 12:46:09 +000023273/*
23274 * List v:oldfiles in a nice way.
23275 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023276 void
23277ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023278 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023279{
23280 list_T *l = vimvars[VV_OLDFILES].vv_list;
23281 listitem_T *li;
23282 int nr = 0;
23283
23284 if (l == NULL)
23285 msg((char_u *)_("No old files"));
23286 else
23287 {
23288 msg_start();
23289 msg_scroll = TRUE;
23290 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23291 {
23292 msg_outnum((long)++nr);
23293 MSG_PUTS(": ");
23294 msg_outtrans(get_tv_string(&li->li_tv));
23295 msg_putchar('\n');
23296 out_flush(); /* output one line at a time */
23297 ui_breakcheck();
23298 }
23299 /* Assume "got_int" was set to truncate the listing. */
23300 got_int = FALSE;
23301
23302#ifdef FEAT_BROWSE_CMD
23303 if (cmdmod.browse)
23304 {
23305 quit_more = FALSE;
23306 nr = prompt_for_number(FALSE);
23307 msg_starthere();
23308 if (nr > 0)
23309 {
23310 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23311 (long)nr);
23312
23313 if (p != NULL)
23314 {
23315 p = expand_env_save(p);
23316 eap->arg = p;
23317 eap->cmdidx = CMD_edit;
23318 cmdmod.browse = FALSE;
23319 do_exedit(eap, NULL);
23320 vim_free(p);
23321 }
23322 }
23323 }
23324#endif
23325 }
23326}
23327
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328#endif /* FEAT_EVAL */
23329
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023331#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332
23333#ifdef WIN3264
23334/*
23335 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23336 */
23337static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23338static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23339static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23340
23341/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023342 * Get the short path (8.3) for the filename in "fnamep".
23343 * Only works for a valid file name.
23344 * When the path gets longer "fnamep" is changed and the allocated buffer
23345 * is put in "bufp".
23346 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23347 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 */
23349 static int
23350get_short_pathname(fnamep, bufp, fnamelen)
23351 char_u **fnamep;
23352 char_u **bufp;
23353 int *fnamelen;
23354{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023355 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 char_u *newbuf;
23357
23358 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359 l = GetShortPathName(*fnamep, *fnamep, len);
23360 if (l > len - 1)
23361 {
23362 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023363 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 newbuf = vim_strnsave(*fnamep, l);
23365 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367
23368 vim_free(*bufp);
23369 *fnamep = *bufp = newbuf;
23370
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023371 /* Really should always succeed, as the buffer is big enough. */
23372 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373 }
23374
23375 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023376 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377}
23378
23379/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023380 * Get the short path (8.3) for the filename in "fname". The converted
23381 * path is returned in "bufp".
23382 *
23383 * Some of the directories specified in "fname" may not exist. This function
23384 * will shorten the existing directories at the beginning of the path and then
23385 * append the remaining non-existing path.
23386 *
23387 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023388 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023389 * bufp - Pointer to an allocated buffer for the filename.
23390 * fnamelen - Length of the filename pointed to by fname
23391 *
23392 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393 */
23394 static int
23395shortpath_for_invalid_fname(fname, bufp, fnamelen)
23396 char_u **fname;
23397 char_u **bufp;
23398 int *fnamelen;
23399{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023400 char_u *short_fname, *save_fname, *pbuf_unused;
23401 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023403 int old_len, len;
23404 int new_len, sfx_len;
23405 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406
23407 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023408 old_len = *fnamelen;
23409 save_fname = vim_strnsave(*fname, old_len);
23410 pbuf_unused = NULL;
23411 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023413 endp = save_fname + old_len - 1; /* Find the end of the copy */
23414 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023416 /*
23417 * Try shortening the supplied path till it succeeds by removing one
23418 * directory at a time from the tail of the path.
23419 */
23420 len = 0;
23421 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023423 /* go back one path-separator */
23424 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23425 --endp;
23426 if (endp <= save_fname)
23427 break; /* processed the complete path */
23428
23429 /*
23430 * Replace the path separator with a NUL and try to shorten the
23431 * resulting path.
23432 */
23433 ch = *endp;
23434 *endp = 0;
23435 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023436 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023437 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23438 {
23439 retval = FAIL;
23440 goto theend;
23441 }
23442 *endp = ch; /* preserve the string */
23443
23444 if (len > 0)
23445 break; /* successfully shortened the path */
23446
23447 /* failed to shorten the path. Skip the path separator */
23448 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 }
23450
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023451 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023452 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023453 /*
23454 * Succeeded in shortening the path. Now concatenate the shortened
23455 * path with the remaining path at the tail.
23456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023458 /* Compute the length of the new path. */
23459 sfx_len = (int)(save_endp - endp) + 1;
23460 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023462 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023463 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023464 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023466 /* There is not enough space in the currently allocated string,
23467 * copy it to a buffer big enough. */
23468 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023470 {
23471 retval = FAIL;
23472 goto theend;
23473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 }
23475 else
23476 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023477 /* Transfer short_fname to the main buffer (it's big enough),
23478 * unless get_short_pathname() did its work in-place. */
23479 *fname = *bufp = save_fname;
23480 if (short_fname != save_fname)
23481 vim_strncpy(save_fname, short_fname, len);
23482 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023484
23485 /* concat the not-shortened part of the path */
23486 vim_strncpy(*fname + len, endp, sfx_len);
23487 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023489
23490theend:
23491 vim_free(pbuf_unused);
23492 vim_free(save_fname);
23493
23494 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495}
23496
23497/*
23498 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023499 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 */
23501 static int
23502shortpath_for_partial(fnamep, bufp, fnamelen)
23503 char_u **fnamep;
23504 char_u **bufp;
23505 int *fnamelen;
23506{
23507 int sepcount, len, tflen;
23508 char_u *p;
23509 char_u *pbuf, *tfname;
23510 int hasTilde;
23511
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023512 /* Count up the path separators from the RHS.. so we know which part
23513 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023515 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 if (vim_ispathsep(*p))
23517 ++sepcount;
23518
23519 /* Need full path first (use expand_env() to remove a "~/") */
23520 hasTilde = (**fnamep == '~');
23521 if (hasTilde)
23522 pbuf = tfname = expand_env_save(*fnamep);
23523 else
23524 pbuf = tfname = FullName_save(*fnamep, FALSE);
23525
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023526 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023528 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23529 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530
23531 if (len == 0)
23532 {
23533 /* Don't have a valid filename, so shorten the rest of the
23534 * path if we can. This CAN give us invalid 8.3 filenames, but
23535 * there's not a lot of point in guessing what it might be.
23536 */
23537 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023538 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23539 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 }
23541
23542 /* Count the paths backward to find the beginning of the desired string. */
23543 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023544 {
23545#ifdef FEAT_MBYTE
23546 if (has_mbyte)
23547 p -= mb_head_off(tfname, p);
23548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 if (vim_ispathsep(*p))
23550 {
23551 if (sepcount == 0 || (hasTilde && sepcount == 1))
23552 break;
23553 else
23554 sepcount --;
23555 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 if (hasTilde)
23558 {
23559 --p;
23560 if (p >= tfname)
23561 *p = '~';
23562 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 }
23565 else
23566 ++p;
23567
23568 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23569 vim_free(*bufp);
23570 *fnamelen = (int)STRLEN(p);
23571 *bufp = pbuf;
23572 *fnamep = p;
23573
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023574 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575}
23576#endif /* WIN3264 */
23577
23578/*
23579 * Adjust a filename, according to a string of modifiers.
23580 * *fnamep must be NUL terminated when called. When returning, the length is
23581 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023582 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 * When there is an error, *fnamep is set to NULL.
23584 */
23585 int
23586modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23587 char_u *src; /* string with modifiers */
23588 int *usedlen; /* characters after src that are used */
23589 char_u **fnamep; /* file name so far */
23590 char_u **bufp; /* buffer for allocated file name or NULL */
23591 int *fnamelen; /* length of fnamep */
23592{
23593 int valid = 0;
23594 char_u *tail;
23595 char_u *s, *p, *pbuf;
23596 char_u dirname[MAXPATHL];
23597 int c;
23598 int has_fullname = 0;
23599#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023600 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 int has_shortname = 0;
23602#endif
23603
23604repeat:
23605 /* ":p" - full path/file_name */
23606 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23607 {
23608 has_fullname = 1;
23609
23610 valid |= VALID_PATH;
23611 *usedlen += 2;
23612
23613 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23614 if ((*fnamep)[0] == '~'
23615#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23616 && ((*fnamep)[1] == '/'
23617# ifdef BACKSLASH_IN_FILENAME
23618 || (*fnamep)[1] == '\\'
23619# endif
23620 || (*fnamep)[1] == NUL)
23621
23622#endif
23623 )
23624 {
23625 *fnamep = expand_env_save(*fnamep);
23626 vim_free(*bufp); /* free any allocated file name */
23627 *bufp = *fnamep;
23628 if (*fnamep == NULL)
23629 return -1;
23630 }
23631
23632 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023633 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 {
23635 if (vim_ispathsep(*p)
23636 && p[1] == '.'
23637 && (p[2] == NUL
23638 || vim_ispathsep(p[2])
23639 || (p[2] == '.'
23640 && (p[3] == NUL || vim_ispathsep(p[3])))))
23641 break;
23642 }
23643
23644 /* FullName_save() is slow, don't use it when not needed. */
23645 if (*p != NUL || !vim_isAbsName(*fnamep))
23646 {
23647 *fnamep = FullName_save(*fnamep, *p != NUL);
23648 vim_free(*bufp); /* free any allocated file name */
23649 *bufp = *fnamep;
23650 if (*fnamep == NULL)
23651 return -1;
23652 }
23653
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023654#ifdef WIN3264
23655# if _WIN32_WINNT >= 0x0500
23656 if (vim_strchr(*fnamep, '~') != NULL)
23657 {
23658 /* Expand 8.3 filename to full path. Needed to make sure the same
23659 * file does not have two different names.
23660 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23661 p = alloc(_MAX_PATH + 1);
23662 if (p != NULL)
23663 {
23664 if (GetLongPathName(*fnamep, p, MAXPATHL))
23665 {
23666 vim_free(*bufp);
23667 *bufp = *fnamep = p;
23668 }
23669 else
23670 vim_free(p);
23671 }
23672 }
23673# endif
23674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 /* Append a path separator to a directory. */
23676 if (mch_isdir(*fnamep))
23677 {
23678 /* Make room for one or two extra characters. */
23679 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23680 vim_free(*bufp); /* free any allocated file name */
23681 *bufp = *fnamep;
23682 if (*fnamep == NULL)
23683 return -1;
23684 add_pathsep(*fnamep);
23685 }
23686 }
23687
23688 /* ":." - path relative to the current directory */
23689 /* ":~" - path relative to the home directory */
23690 /* ":8" - shortname path - postponed till after */
23691 while (src[*usedlen] == ':'
23692 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23693 {
23694 *usedlen += 2;
23695 if (c == '8')
23696 {
23697#ifdef WIN3264
23698 has_shortname = 1; /* Postpone this. */
23699#endif
23700 continue;
23701 }
23702 pbuf = NULL;
23703 /* Need full path first (use expand_env() to remove a "~/") */
23704 if (!has_fullname)
23705 {
23706 if (c == '.' && **fnamep == '~')
23707 p = pbuf = expand_env_save(*fnamep);
23708 else
23709 p = pbuf = FullName_save(*fnamep, FALSE);
23710 }
23711 else
23712 p = *fnamep;
23713
23714 has_fullname = 0;
23715
23716 if (p != NULL)
23717 {
23718 if (c == '.')
23719 {
23720 mch_dirname(dirname, MAXPATHL);
23721 s = shorten_fname(p, dirname);
23722 if (s != NULL)
23723 {
23724 *fnamep = s;
23725 if (pbuf != NULL)
23726 {
23727 vim_free(*bufp); /* free any allocated file name */
23728 *bufp = pbuf;
23729 pbuf = NULL;
23730 }
23731 }
23732 }
23733 else
23734 {
23735 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23736 /* Only replace it when it starts with '~' */
23737 if (*dirname == '~')
23738 {
23739 s = vim_strsave(dirname);
23740 if (s != NULL)
23741 {
23742 *fnamep = s;
23743 vim_free(*bufp);
23744 *bufp = s;
23745 }
23746 }
23747 }
23748 vim_free(pbuf);
23749 }
23750 }
23751
23752 tail = gettail(*fnamep);
23753 *fnamelen = (int)STRLEN(*fnamep);
23754
23755 /* ":h" - head, remove "/file_name", can be repeated */
23756 /* Don't remove the first "/" or "c:\" */
23757 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23758 {
23759 valid |= VALID_HEAD;
23760 *usedlen += 2;
23761 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023762 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023763 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 *fnamelen = (int)(tail - *fnamep);
23765#ifdef VMS
23766 if (*fnamelen > 0)
23767 *fnamelen += 1; /* the path separator is part of the path */
23768#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023769 if (*fnamelen == 0)
23770 {
23771 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23772 p = vim_strsave((char_u *)".");
23773 if (p == NULL)
23774 return -1;
23775 vim_free(*bufp);
23776 *bufp = *fnamep = tail = p;
23777 *fnamelen = 1;
23778 }
23779 else
23780 {
23781 while (tail > s && !after_pathsep(s, tail))
23782 mb_ptr_back(*fnamep, tail);
23783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 }
23785
23786 /* ":8" - shortname */
23787 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23788 {
23789 *usedlen += 2;
23790#ifdef WIN3264
23791 has_shortname = 1;
23792#endif
23793 }
23794
23795#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023796 /*
23797 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 */
23799 if (has_shortname)
23800 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023801 /* Copy the string if it is shortened by :h and when it wasn't copied
23802 * yet, because we are going to change it in place. Avoids changing
23803 * the buffer name for "%:8". */
23804 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 {
23806 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023807 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808 return -1;
23809 vim_free(*bufp);
23810 *bufp = *fnamep = p;
23811 }
23812
23813 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023814 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815 if (!has_fullname && !vim_isAbsName(*fnamep))
23816 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023817 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 return -1;
23819 }
23820 else
23821 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023822 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823
Bram Moolenaardc935552011-08-17 15:23:23 +020023824 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023826 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 return -1;
23828
23829 if (l == 0)
23830 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023831 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023833 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 return -1;
23835 }
23836 *fnamelen = l;
23837 }
23838 }
23839#endif /* WIN3264 */
23840
23841 /* ":t" - tail, just the basename */
23842 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23843 {
23844 *usedlen += 2;
23845 *fnamelen -= (int)(tail - *fnamep);
23846 *fnamep = tail;
23847 }
23848
23849 /* ":e" - extension, can be repeated */
23850 /* ":r" - root, without extension, can be repeated */
23851 while (src[*usedlen] == ':'
23852 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23853 {
23854 /* find a '.' in the tail:
23855 * - for second :e: before the current fname
23856 * - otherwise: The last '.'
23857 */
23858 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23859 s = *fnamep - 2;
23860 else
23861 s = *fnamep + *fnamelen - 1;
23862 for ( ; s > tail; --s)
23863 if (s[0] == '.')
23864 break;
23865 if (src[*usedlen + 1] == 'e') /* :e */
23866 {
23867 if (s > tail)
23868 {
23869 *fnamelen += (int)(*fnamep - (s + 1));
23870 *fnamep = s + 1;
23871#ifdef VMS
23872 /* cut version from the extension */
23873 s = *fnamep + *fnamelen - 1;
23874 for ( ; s > *fnamep; --s)
23875 if (s[0] == ';')
23876 break;
23877 if (s > *fnamep)
23878 *fnamelen = s - *fnamep;
23879#endif
23880 }
23881 else if (*fnamep <= tail)
23882 *fnamelen = 0;
23883 }
23884 else /* :r */
23885 {
23886 if (s > tail) /* remove one extension */
23887 *fnamelen = (int)(s - *fnamep);
23888 }
23889 *usedlen += 2;
23890 }
23891
23892 /* ":s?pat?foo?" - substitute */
23893 /* ":gs?pat?foo?" - global substitute */
23894 if (src[*usedlen] == ':'
23895 && (src[*usedlen + 1] == 's'
23896 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23897 {
23898 char_u *str;
23899 char_u *pat;
23900 char_u *sub;
23901 int sep;
23902 char_u *flags;
23903 int didit = FALSE;
23904
23905 flags = (char_u *)"";
23906 s = src + *usedlen + 2;
23907 if (src[*usedlen + 1] == 'g')
23908 {
23909 flags = (char_u *)"g";
23910 ++s;
23911 }
23912
23913 sep = *s++;
23914 if (sep)
23915 {
23916 /* find end of pattern */
23917 p = vim_strchr(s, sep);
23918 if (p != NULL)
23919 {
23920 pat = vim_strnsave(s, (int)(p - s));
23921 if (pat != NULL)
23922 {
23923 s = p + 1;
23924 /* find end of substitution */
23925 p = vim_strchr(s, sep);
23926 if (p != NULL)
23927 {
23928 sub = vim_strnsave(s, (int)(p - s));
23929 str = vim_strnsave(*fnamep, *fnamelen);
23930 if (sub != NULL && str != NULL)
23931 {
23932 *usedlen = (int)(p + 1 - src);
23933 s = do_string_sub(str, pat, sub, flags);
23934 if (s != NULL)
23935 {
23936 *fnamep = s;
23937 *fnamelen = (int)STRLEN(s);
23938 vim_free(*bufp);
23939 *bufp = s;
23940 didit = TRUE;
23941 }
23942 }
23943 vim_free(sub);
23944 vim_free(str);
23945 }
23946 vim_free(pat);
23947 }
23948 }
23949 /* after using ":s", repeat all the modifiers */
23950 if (didit)
23951 goto repeat;
23952 }
23953 }
23954
23955 return valid;
23956}
23957
23958/*
23959 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23960 * "flags" can be "g" to do a global substitute.
23961 * Returns an allocated string, NULL for error.
23962 */
23963 char_u *
23964do_string_sub(str, pat, sub, flags)
23965 char_u *str;
23966 char_u *pat;
23967 char_u *sub;
23968 char_u *flags;
23969{
23970 int sublen;
23971 regmatch_T regmatch;
23972 int i;
23973 int do_all;
23974 char_u *tail;
23975 garray_T ga;
23976 char_u *ret;
23977 char_u *save_cpo;
23978
23979 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23980 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023981 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982
23983 ga_init2(&ga, 1, 200);
23984
23985 do_all = (flags[0] == 'g');
23986
23987 regmatch.rm_ic = p_ic;
23988 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23989 if (regmatch.regprog != NULL)
23990 {
23991 tail = str;
23992 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23993 {
23994 /*
23995 * Get some space for a temporary buffer to do the substitution
23996 * into. It will contain:
23997 * - The text up to where the match is.
23998 * - The substituted text.
23999 * - The text after the match.
24000 */
24001 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24002 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24003 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24004 {
24005 ga_clear(&ga);
24006 break;
24007 }
24008
24009 /* copy the text up to where the match is */
24010 i = (int)(regmatch.startp[0] - tail);
24011 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24012 /* add the substituted text */
24013 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24014 + ga.ga_len + i, TRUE, TRUE, FALSE);
24015 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 /* avoid getting stuck on a match with an empty string */
24017 if (tail == regmatch.endp[0])
24018 {
24019 if (*tail == NUL)
24020 break;
24021 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24022 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 }
24024 else
24025 {
24026 tail = regmatch.endp[0];
24027 if (*tail == NUL)
24028 break;
24029 }
24030 if (!do_all)
24031 break;
24032 }
24033
24034 if (ga.ga_data != NULL)
24035 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24036
24037 vim_free(regmatch.regprog);
24038 }
24039
24040 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24041 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024042 if (p_cpo == empty_option)
24043 p_cpo = save_cpo;
24044 else
24045 /* Darn, evaluating {sub} expression changed the value. */
24046 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047
24048 return ret;
24049}
24050
24051#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */